3

At the top of every page of page of my organization's site resides the current hours of operation and current "now open"/"now closed" status, using Views block and Office Hours module).

At this time of night on a Wednesday when logged in, I correctly see that the hours are reflecting tomorrow's hours and that the organization is now closed:

enter image description here

However when logged out, I see that the current day's hours (now over) and "NOW OPEN". It's six hours after closing time and the 5 minute page cache time in performance settings does not seem to be respected for anonymous page views:

enter image description here

What can I do to force an anonymous cache clear (overkill?) or invalidation of a block for anonymous users, perhaps on a schedule? I'm aware that the built-in Page Cache module may be caching full rendered pages for anonymous users while Dynamic Page Cache module makes it usable for logged in users. Shouldn't Page Cache respect the page cache maximum age set in the Performance settings page? Most people viewing the current hours at the top of every page are anonymous users.

For now I'll manually clear the cache, making the status update properly (not ideal).

UPDATE

While the website is served primarily to anonymous users, I found this helpful advise:

"Websites that serve personalized content to anonymous users (dynamic, per-session, e.g. a shopping cart) will want to disable the Internal Page Cache module. This module assumes pages are identical for all anonymous users. Those websites can still take advantage of the Dynamic Page Cache module though, or can alternatively do their personalization using JavaScript + AJAX."

This seems to have resolved immediate issues. I'll explore JavaScript + AJAX in the future. After disabling Internal Page Cache, a custom Lazy builder block + BigPipe has good potential for pages that need to change immediately with every page load.

  • Thank you for the quick insight everyone. I'll test answers as soon as possible today. – Christopher Stevens Oct 27 '16 at 12:53
4

This sounds like a great place to use Drupal 8's lazy builder:

http://tech.dichtlog.nl/php/2015/08/03/lazy-builder-callback.html

http://www.qed42.com/blog/lazy-builders-drupal8

This allows the rest of the page to be cached, but your lazy builder service would return whether business is now open or now closed.

  • Nice idea, but actually doesn't work. lazy builder/bigpipe is for anonymous users only. – Berdir Oct 27 '16 at 19:43
  • @Berdir Most visitors to the website are not logged in. However, it seems to be the case that lazy builder+BigPipe enabled is not a working solution yet (unless I'm missing something) for anonymous users. I'm using the Icecream example from this post. It works very well when logged in, but that doesn't solve my issue. I'll keep playing around and then try a JavaScript solution next... Note: I have Performance settings: page cache set to one minute (anonymous users only seeing chocolate icecream for an hour+) – Christopher Stevens Nov 9 '16 at 20:19
  • Update: It seems that if I have "Page cache maximum age" set to "<no caching>" (followed by cache being cleared), pages in are still being long-term-cached for anonymous users. Maybe there is a different issue here... – Christopher Stevens Nov 9 '16 at 20:25
  • 1
    Last update: See "UPDATE" at the bottom of my original post for a working resolution for immediate needs. – Christopher Stevens Nov 9 '16 at 20:44
4

At the top of the issue queue of Office Hours is this issue:

Fix javascript in D8-version of office_hours module

The problem is, you need this issue resolved, because for anonymous users javascript has to work.

If you want to build your own module, follow @oknate advice and use a lazy builder in a custom block plugin and install BigPipe to get the javascript part for anonymous users.

  • 1
    Javascript is by far the easiest solution. Anonymous page cache doesn't support lazy builders and big pipe doens't either (there is an issue to support it, but that's not for cached pages, just for the first request for an anonymous user). Dynamic max-age would be better than lazy builder actually, since it is not dynamic, just timed. But anonymous page cache also doesn't respect max-age, unfortunately. The only thing that it respects is the expires header on the response object, so you could try to set that on the response object, with a response event. – Berdir Oct 27 '16 at 19:46
1

A quick and dirty solution would be to use the Cleaner module to force the cache to clear when cron runs. You could schedule cron runs to occur at open/close times. It's not an ideal solution because Cleaner clears the entire cache, resulting in slower performance. But it's a quick fix until you have time to develop the lazy builder/bigpipe solution mentioned above.

  • While I wouldn't call this a final solution, I feel it deserves an upvote as it has taken the stress out of current hours for this small site while I test out the bigpipe+lazy builder. Thanks! – Christopher Stevens Oct 27 '16 at 22:52
1

Have you tried implementing hook_block_view_BASE_BLOCK_ID_alter?

Here is the code to exclued from cache.

function hook_block_view_BASE_BLOCK_ID_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) { $build['#cache']['max-age'] = 0; }

0

Try setting "cache max-age" in your block definition. As per short description provided on Cache API Page for max-age:

For time-sensitive caching, i.e. time dependencies

For example of how to use/set cache in block check Drupal 8 Block Cache.

Hope this helps.

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.