File cache driver: no automatic invalidation when models write
0 reputation · 25 Mar 2026, 03:30 UTC
In CodeIgniter 4.x, the cache library's file driver holds each entry until its TTL expires. Nothing purges an entry when the underlying table changes: there is no tagging, and model writes do not touch the cache. Read-heavy pages can therefore serve pre-update values until expiry.
The goal is one consistent invalidation strategy for entries saved under predictable keys, without leaning on very short TTLs to hide staleness. Two extension points exist but differ in reach: model callbacks such as afterInsert, afterUpdate, and afterDelete fire per model class, while the global Events system does not observe model writes by default. A shared base model could centralize purge logic, but that makes the key naming convention — for example a table-name prefix — part of the application contract, and queries joining several tables may not map cleanly to a single key.
The staleness itself is easy to confirm on any 4.x install: cache a query result, update the row through the model, then re-read the cache before expiry.
Three decisions remain open:
- Should invalidation live in per-model callbacks, a shared base model, or a thin wrapper around the cache service?
- Is a table-prefix key convention enough to cover entries derived from a model's writes, or do joined queries need broader purging?
- When is TTL-only expiry the better trade-off over explicit deletes?