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

johntang created an issue. See original summary.

johntang’s picture

Issue summary: View changes
Lendude’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 8.x-3.x-dev » 8.3.x-dev
Component: Miscellaneous » cache system

@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.

Berdir’s picture

Status: Active » Postponed (maintainer needs more info)

I 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.

johntang’s picture

I 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.

johntang’s picture

Status: Postponed (maintainer needs more info) » Fixed
Berdir’s picture

Yeah, 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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.