The use case seem pretty common for example on event website. You want your old event not to appear on the "current event" listing when their date is in the past.
You will configure a filter like that:

The problem is that anonymous user still see the old events because the caches are note invalidate.
A workaround is to clear custom tag-cache on cron run https://bkosborne.com/blog/keeping-view-upcoming-events-fresh-drupal-8
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | 2968485-13.patch | 696 bytes | abramm |
Comments
Comment #2
trichers commentedI have the same problem in all Drupal 8 Sites we´ve made. Is there a chance to get es solution in the core?
Comment #3
gagarine commentedTheir is a module that try to fix this issue https://www.drupal.org/project/cache_control_override
Then you can set a time-based cache in your views and it will works!
Of course that should be fixed in core..
Comment #10
luksakI have been using
max-ageextracted form the views results. It is quite cumbersome, but it works. You can either use the preprocessor of the view or an entity view mode.It would be great to have this in core, but i can't imagine how complex a generic solution for this would be. A few thoughts:
We could use the offset set in each date filter and calculate the timestamps for each row when it wouldn't be included in the results anymore.
We can the use the minimum timestamp we get per row as the
max-ageMaybe that should rather live in contrib? It serves the particular use case of an event calendar. What do others think about this?
But this is even more complex: How do we handle rows that are currently not in the results, but will be in the future? (Eg. a calendar that only shows events in the current month)
So for that use case a max-age on the view would make more sense and core can already do that.
And then there are daterange fields :)
Comment #13
abrammJust faced the same issue and did a quick research.
As mentioned by Lukas von Blarer, there's no reliable way of determining max-age automatically, especially if the minimum/maximum value is not in current results.
The safest option for now is to set
max-agevalue0if the offset-based filtering is used; contrib or custom modules may override this behavior if it's possible to calculatemax-agefor a specific case (e.g. in case of events calendar).Here's a simple patch implementing this.
Comment #15
darvanenI thought this was a good idea from #10:
However, according to the helpful folks on Drupal Slack... views cache metadata isn't compiled at runtime but saved in config, so unless that changes this approach won't work.
I don't think disabling cache entirely is the answer though.
Comment #16
darvanenHere's a start on a contrib module: https://www.drupal.org/project/vcp4dates
Feedback very welcome.
Edit: A friend asked me why do it in a contrib module instead of a patch, my answer:
Comment #19
darvanenJust released first beta of https://www.drupal.org/project/vcp4dates
I finally figured out how to support multiple filters and archive views.
Comment #20
berdirThe most recent release of https://www.drupal.org/project/views_custom_cache_tag also provides limited support for this, but it's manual, not automated. Essentially, it's a combination of tag and time based cache invalidation including support for strtotime(). We commonly set our event views to have current day granularity, so events of the current day still show up as upcoming events for the whole day.
Now you can use views_custom_cache_tag with a custom expiration "tomorrow midnight" (so the calculated expiration will be seconds until midnight) *and* also use a node type specific cache tag for example.