I am working on a custom module to pull tweets. Is it necessary for me to cache my request to Twitter's API, if I have both Drupal 8's Internal Page Cache and Dynamic Page Cache enabled? Can I just rely on the Page Cache?

For instance, if I set some data to cache for every 3 minutes, but the page cache is set for every 5 minutes, my custom data cache time will be ignored, and my data will be cached every five minutes, correct?

For reference, I meant to set my custom cache like so:

$cid = 'my_custom_module_id';
if ($cache = \Drupal::cache()->get($cid)) {
  $mydata = $cache->data;
} else {
  $mydata = [DO MY API CALL STUFF!]
  \Drupal::cache()->set($cid, $mydata, 180);

Depends. Do you display this on a specific page? Or on multiple pages? Is it a page, a block... ? Do you have many authenticated users?

If it's a single page callback or a block that's always the same, then it's possibly fine to not cache it yourself. That only makes sense when you call your code on many different pages.

If you also have a lot of users, especially with different roles, multiple languages or or cache contexts that might vary but always the same API call, then it might make sense again to cache it yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.