Random seed and page cache (and more)

Created on 3 October 2022, over 1 year ago
Updated 20 February 2023, over 1 year ago

I noticed a problem combining this module and page caching. Fixing it led to some other findings and fixes.

Problem/Motivation

We have a views page with a random seed. The page goes into the page cache and is cached without an expiry time. The idea being that a cache tag should be invalidated when necessary.

The module does invalidate a cache tag when a new seed is generated. But:

  • the cache tag is never set on the view in the first place
  • a new seed is never generated because the page is served from the page cache

Steps to reproduce

Add a views page with random seed sorting. Set the seed reset time to e.g. 10 minutes. Enable page caching. Wait for the seed to expire. The page is not invalidated in the page cache.

Proposed resolution

I have introduced setting a cache tag on the view and then a hook_cron implementation that'll invalidate cache tags based on expired seeds.

To avoid having the cron job load all views to find those using views_random_seed, I have reused the seed name as the cache tag. This way, I can locate the views via the stored seeds in the key/value table.

Some other issues identified

Seeds are never deleted from the key/value store. Not when a view no longer uses random seed, not when a view is deleted, and not even when the module is uninstalled.

I added a hook_uninstall() and added cleanup in the cron jobs as well.

Then I got a bit confused about the dual use of the seed as both a seed and a timestamp used for deciding if it's time to regenerate.

I could probably live with me being confused. But then I noticed that if you use PostgreSQL, the seed is reduced to a value between 0 and 1. That makes it unfit as a timestamp, as it would always be considered expired.

So, I introduced a new Seed class consisting of the seed value and a dedicated timestamp for its creation.

Finally, while at it, I fixed some code standard issues and introduced dependency injection for the remaining services that lacked it.

Remaining tasks

None that I'm aware of. Besides review etc.

User interface changes

None.

API changes

  • The name of the cache tag has changed. But the cache tag was never set in the first place.
  • The seed has changed its format.

I don't think any of them has been advertised as a real API, but some people could have created custom code based on them.

Data model changes

The seed has changed its value from a single int to an int (the seed value) and a created timestamp. Both wrapped in a new class.

πŸ› Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

πŸ‡©πŸ‡°Denmark arnested

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡΅πŸ‡ΉPortugal introfini

    In a setup with more than one webhead, the page stopped working after this module was installed. With this patch, the error was fixed.

    I'm sorry I can't tell you more, but it only happened in production and the error had to do with cache (cache in redis). That is how I thought this patch might help and it did.

Production build 0.69.0 2024