So we have built a rather large site for a client, which has multiple regions, locales, user privileges, etc. Some time ago the default cache of Craft couldn't cope anymore with keeping track. The cache clearing tasks always stalled so cache wouldn't be deleted anymore.
This triggered us to change the way we cache our pages. Unfortunately, the template cache table in the database is getting to its limits as we haven't found a good way to clear the old caches.
How it works:
We set 'cacheElementQueries' => false,
Every page gets its own cache (unique cachekey) which consists of:
- Is the user logged in yes or no?
- Which region is the user in? (one of 3 possibilities)
- Which user type does the user have? (any combination of 3 types possible)
- The page url
- The last updated date of the entry (not always used)
We cache everything globally and cache some parts of pages separately, for example: the sidebar of a product page remains the same but the content changes.
We use a combination of coldcache and cacheflag plugins.
Issue:
As we create a lot of caches and a lot of them with a certain date in the key, the database table gets cluttered and grows to big in size (max limit for our DB is 1GB and the cache table is reaching 500MB).
The question
How can we delete old caches from the DB table?
The only two options I can think of now are:
- Set cache duration to a certain time interval (but than all caches are deleted which will make the site slow every interval).
- Create a plugin that runs every month with a CRON job and checks all of the cache keys and deletes everyone but the latest.
Any other ideas out there?