2

Say I have this type of code to build a custom block in Drupal 8.

 public function build() {

  //code that does a Guzzle call to retrieve data setting $links to contain that data



  $build['article_links'] = array(
    '#theme' => 'them_stuff',
    '#article_links' => $links,
    '#cache' => array('max-age' => 600),

  );


  return $build;
  }

Currently I don't have the '#cache' => array('max-age' => 600), and so every time to page loads the Guzzle call is run. If I add '#cache' => array('max-age' => 600), does that mean that the Guzzle call won't be run until the cache is invalidated?

3

Blocks are cached by default, so not specifiying anything should mean your guzzle call should only run once.

max-age 600 then means that the cache is not valid indefinitely but only 600s.

Note that anonymous page cache currently doesn't respect that setting. You have to disable that if you need this to update every 600s.

  • So for each person who visits the page, their first visit is pulled from the Guzzle call, and then cached after a second visit. I guess I need to incorporate some caching of that Guzzle call into the database, so that any Guzzle call is only run every hour or so. – StevenPatz Aug 23 '16 at 14:38
  • unless you specifiy it to be per user with a cache context, it should only vary by the default contexts (user.permissions, which is a hash based on the permissions a user has. all authenticated and anon users will use the same cache, as will users that have the same cache. if that is still too many requests, you need to use the cache api yourself – Berdir Aug 23 '16 at 15:00

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.