QueryCache

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.

Installation

First, publish the configuration file:

php artisan vendor:publish --provider="Grafite\QueryCache\QueryCacheServiceProvider" --tag="config"

Next, add the following to your .env file to configure the package:

QUERY_CACHE_TTL=604800 // 7 days
QUERY_CACHE_FLUSH_ON_UPDATE=true
QUERY_CACHE_DRIVER=redis
QUERY_CACHE_PREFIX=qc
QUERY_CACHE_PLAIN_TEXT_KEYS=false

Next, add the QueryCacheable trait to any Eloquent model you wish to cache queries for.

With the trait added you have access to the following methods:

static bool flushQueryCache(array $tags = [])
static bool flushQueryCacheWithTag(string $string)
static \Illuminate\Database\Query\Builder|static cacheFor(\DateTime|int|null $time)
static \Illuminate\Database\Query\Builder|static cacheForever()
static \Illuminate\Database\Query\Builder|static dontCache()
static \Illuminate\Database\Query\Builder|static doNotCache()
static \Illuminate\Database\Query\Builder|static cacheDriver(string $cacheDriver)
static \Illuminate\Database\Query\Builder|static cacheBaseTags(array $tags = [])

Important Details

By default, QueryCache will flush the cache for a model when it is created, updated, or deleted. This behavior can be disabled by setting QUERY_CACHE_FLUSH_ON_UPDATE to false in your .env file.

If you need to purge the cache manually, you can use the flushQueryCache method on your model.