Repository navigation
[6.x] Speed up reads from the file cache store - #15837
ryanmitchell wants to merge 1 commit into
Conversation
Laravel's FileStore::get() checks the file exists before reading it and reads the clock twice, even for items stored forever. The Stache reads far more often than it writes and almost never stores items that expire, so open the file directly and only check the clock for items that can expire. Keep the shared lock, and forget expired or corrupt files as Laravel does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thanks Ryan! I've decided not to merge this into Statamic. I had a few different agents benchmark it locally, and they found a smaller saving than the PR describes: about 2–4µs per cache hit on macOS rather than ~10µs. A typical page does a few hundred Stache reads, so that comes out to well under a millisecond per request, and sites using static caching won't see any difference. Cache misses actually got a bit slower (~1.75µs → ~3.1µs), because the suppressed Weighed against that, we'd be maintaining our own copy of Laravel's read path across 12.x and 13.x. Future fixes to Skipping the redundant |
Statamic replaces the
filecache driver with its ownStatamic\Extensions\FileStore, which the Stache uses on the default setup. Reads go through Laravel'sFileStore::get(), which:is_file()before opening it, andCarbon::now()twice per read to check expiry, even for items stored forever.The Stache reads far more often than it writes, and almost everything it stores never expires. This PR overrides
get()to open the file directly (a failed open is a cache miss) and only read the clock for items that can actually expire.Everything else matches Laravel's behaviour:
serializable_classesis respected on Laravel versions that support it.It applies to all keys in the store, not just Stache keys, since the behaviour is the same as Laravel's.
Benchmark
The saving is a fixed overhead of roughly 10µs per read. It's a smaller share when unserializing large entries dominates, and a bigger one for requests that read many items from the Stache.
Tests
The existing
FileStoreTestnow also covers reading values (includingfalse,nulland missing keys, andmany()), expiry, corrupt files and allowed classes, each for bothstache::keys and other keys.🤖 Generated with Claude Code