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.
- Matt Lantz (@mattylantz, mattlantz at gmail dot com)
- PHP 8.2+
| Laravel Version | Package Tag | Supported |
|---|---|---|
| ^11.x - ^12.x | 1.x | yes |
Start a new Laravel project:
composer create-project laravel/laravel your-project-nameThen run the following to add Support
composer require "grafite/query-cache"https://documentation.grafite.ca/docs/utilities-querycache
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.
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.
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().
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.
Hashed cache keys now use xxh128 instead of md5, so existing cached queries will miss once after upgrading.
Support is open-sourced software licensed under the MIT license
Please add as many details as possible regarding submission of issues and feature requests
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.
