grafite / query-cache
A database query caching system with invalidation automation.
Requires
- php: ^8.2
- illuminate/collections: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
Requires (Dev)
- laravel/legacy-factories: ^1.4
- laravel/pint: ^1.10
- livewire/livewire: dev-main
- mockery/mockery: ^1.6
- orchestra/testbench: ^10.0|^11.0
- phpunit/phpunit: ^11
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
QueryCache - A general query result caching system for Eloquent models.
QueryCache is a general query result caching system for Eloquent models. It's optimized to be as automated as possible, and can work with Redis and SQLite as caching.
Author(s):
- Matt Lantz (@mattylantz, mattlantz at gmail dot com)
Requirements
- PHP 8.2+
Compatibility and Support
| Laravel Version | Package Tag | Supported |
|---|---|---|
| ^11.x - ^12.x | 1.x | yes |
Installation
Start a new Laravel project:
composer create-project laravel/laravel your-project-name
Then run the following to add Support
composer require "grafite/query-cache"
Documentation
https://documentation.grafite.ca/docs/utilities-querycache
Transactions
Queries run inside a database transaction bypass the cache, since they may see uncommitted data. Cache flushes triggered by model writes inside a transaction are deferred until it commits, and all writes in that transaction share a single flush (a rollback flushes nothing). Set skip_in_transactions (QUERY_CACHE_SKIP_IN_TRANSACTIONS) to false to cache inside transactions, e.g. when your test suite wraps every test in one.
Memoization limit
Repeated identical queries within a request are served from memory. memoize_limit (QUERY_CACHE_MEMOIZE_LIMIT, default 1000) caps how many results are held, evicting the oldest first, so long-running commands stay bounded. Results with more than memoize_max_rows (QUERY_CACHE_MEMOIZE_MAX_ROWS, default 500) rows are not memoized. A model write only forgets the memoized results for that model's tags.
Bulk reads
chunk(), chunkById(), lazy() and each() run one get() per page, so each page gets written to the cache. Skip the cache for bulk iteration:
Post::dontCache()->chunk(1000, function ($posts) { // ... });
pluck() and exists() / doesntExist() are cached like get().
Invalidation mode
By default ('invalidation' => 'tags'), a model write flushes its cache tag. On Redis that deletes every cached query for the model, so the cost grows with the number of cached queries. On stores without tags (file, database) it flushes the entire cache store.
Set QUERY_CACHE_INVALIDATION=versioned to give each tag a version that is part of every cache key instead. A write just bumps the version, which is O(1) on any store and never touches unrelated cache entries. Tradeoffs:
- The first query for a tag in each request does one extra cache read for its version.
- Superseded entries aren't deleted. They expire with their TTL, and
cacheForever()is capped atttlin this mode. On Redis, use an LRUmaxmemory-policy. Cache::tags([...])->flush()has no effect on query cache entries. UseModel::flushQueryCache()instead.
Upgrading
Hashed cache keys now use xxh128 instead of md5, so existing cached queries will miss once after upgrading.
License
Support is open-sourced software licensed under the MIT license
Bug Reporting and Feature Requests
Please add as many details as possible regarding submission of issues and feature requests
Disclaimer
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
