My site using Drupal 8 last version (8.2.5) and we have the views which contains the filter based on user's location (time-zone). For example, the users are coming from UK will get the time in UK timezone, and the Users are coming from the US, will get the time in the US. The problem is, when anonymous users from UK accessing to the site, it's display as US time instead, because the page has cached from US when the users from US visited.
How can I customize Drupal 8 cache for this requirement? My web server enabled the opcache and I and a custom module already.
Updated: Following by a post from https://www.drupal.org/node/1884796 I tried to created a custom cache backend but seems not working.
module.services.yml
services:
cache.backend.bettingsafe:
class: Drupal\bettingsafe\Cache\TimeZoneCache
module\src\Cache\TimeZoneCache.php, I implemented CacheBackendInterface, and on the get() and get() function, I changed:
$cid = $cid . '::' . $TIMEZONE;
for storage special key-value for cache. And the function is working, but it's very long responding and stop my httpd.
Comments
Comment #2
johntang CreditAttribution: johntang commentedComment #3
Lendude@johntang interesting question.
Moving to the right queue. Moving to the cache system queue because this doesn't seem to be limited to Views in scope.
Comment #4
BerdirI don't understand what kind of filter your view contains that varies by timezone. There is a timezone cache context that is included when things display dates, that should be the case for your view as well. If you can, share the view or at least include descriptions and screenshots.
Comment #5
johntang CreditAttribution: johntang commentedI resolved it by copy the Core Page Cache Module to my owner module, then disable Page Cache Module.
getCacheId(Request $request)
this function should be return a custom key is reach my goal.
Comment #6
johntang CreditAttribution: johntang commentedComment #7
BerdirYeah, that's a possibility, as an alternative, you could also implement a service provider that just alters that service with a subclass that overrides just the getCacheId(). Otherwise you'd have to keep an aye out for any other changes and bugfixes in future core versions in the page_cache module and copy them to yours.