Download the PHP package miradnan/laravel-model-caching without Composer
On this page you can find all versions of the php package miradnan/laravel-model-caching. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download miradnan/laravel-model-caching
More information about miradnan/laravel-model-caching
Files in miradnan/laravel-model-caching
Package laravel-model-caching
Short Description Adding cache for Laravel Eloquent queries
License MIT
Homepage https://github.com/miradnan/laravel-model-caching
Informations about the package laravel-model-caching
Laravel Eloquent Query & Table Cache
Laravel Query & Table Cache. This package helps adding caching to queries and tables at model level. It directly runs at Eloquent level, making use of cache before retrieving the data from the DB.
This package adds caching support for all query methods.
Each model that will accept query-by-query caching will have to use the miradnan\QueryCache\Traits\QueryCacheable
trait.
Showcase
Query Cache has the ability to track the SQL used and use it as a key in the cache storage, making the caching query-by-query a breeze.
In the above example, both queries have different keys in the cache storage, thus it doesn't matter what query we handle. By default, caching is disabled unless specifying a value for $cacheFor
. As long as $cacheFor
is existent and is greater than 0
, all queries will be cached.
It is also possible to enable caching for specific queries. This is the recommended way because it is easier to manage each query.
Cache Tags & Cache Invalidation
Some caching stores accept tags. This is really useful if you plan on tagging your cached queries and invalidate only some of the queries when needed.
Be careful tho - specifying cache tags does not change the behaviour of key storage. For example, the following two queries, altough the use the same tag, they have different keys stored in the caching database.
Relationship Caching
Relationships are just another queries. They can be intercepted and modified before the database is hit with the query. The following example needs the Order
model (or the model associated with the orders
relationship) to include the QueryCacheable
trait.
Cache Keys
The package automatically generate the keys needed to store the data in the cache store. However, prefixing them might be useful if the cache store is used by other applications and/or models and you want to manage the keys better to avoid collisions.
If no prefix is specified, the string leqc
is going to be used.
Cache Drivers
By default, the trait uses the default cache driver. If you want to force a specific one, you can do so by calling cacheDriver()
:
Disable caching
If you enabled caching (either by model variable or by the cacheFor
scope), you can also opt to disable it mid-builder.
Equivalent Methods and Variables
You can use the methods provided in this documentation query-by-query, or you can set defaults for each one in the model; using the methods query-by-query will overwrite the defaults.
While settings defaults is not mandatory (excepting for $cacheFor
that will enable caching on all queries), it can be useful to avoid using the chained methods on each query.
Implement the caching method to your own Builder class
Since this package modifies the newBaseQueryBuilder()
in the model, having multiple traits that
modify this function will lead to an overlap.
This can happen in case you are creating your own Builder class for another database drivers or simply to ease out your app query builder for more flexibility.
To solve this, all you have to do is to add the \miradnan\QueryCache\Traits\QueryCacheModule
trait and the \miradnan\QueryCache\Contracts\QueryCacheModuleInterface
interface to your Builder
class. Make sure that the model will no longer use the original QueryCacheable
trait.
Generating your own key
This is how the default key generation function looks like:
In some cases, like implementing your own Builder for MongoDB for example, you might not want to use the toSql()
and use your own
method of generating per-sql key. You can do so by overwriting the MyCustomBuilder
class generatePlainCacheKey()
with your own one.
It is, however, highly recommended to use the most of the variables provided by the function to avoid cache overlapping issues.
Implementing cache for other functions than get()
Since all of the Laravel Eloquent functions are based on it, the builder that comes with this package replaces only the get()
one:
In case that you want to cache your own methods from your custom builder or, for instance, your count()
method doesn't rely on get()
, you can replace it using this syntax:
In fact, you can also replace any eloquent method within your builder if you use $this->shouldAvoidCache()
check and retrieve the cached data using getFromQueryCache()
method, passing the method name as string, and, optionally, an array of columns that defaults to ['*']
.
Notice that the getFromQueryCache()
method accepts a method name and a $columns
parameter. If your method doesn't implement the $columns
, don't pass it.
Note that some functions like getQueryCacheCallback()
may come with an $id
parameter.
The default behaviour of the package doesn't use it, since the query builder uses ->get()
by default that accepts only columns.
However, if your builder replaces functions like find()
, $id
is needed and you will also have to replace the getQueryCacheCallback()
like so:
All versions of laravel-model-caching with dependencies
illuminate/database Version ^6.0|^7.0
illuminate/support Version ^6.0|^7.0