You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Laravel Eloquent Query Cache (LEQC; Le QC; Le Query Cache) is a package that brings the `remember()` functionality that has been removed from Laravel a long time ago.
15
-
This package helps adding caching functionalities directly on the Eloquent level, making use of cache before retrieving the data from the DB.
16
-
17
-
This package adds caching support for **all** query methods.
12
+
Laravel Eloquent Query Cache brings back the `remember()` functionality that has been removed from Laravel a long time ago.
13
+
It adds caching functionalities directly on the Eloquent level, making use of cache within your database queries.
18
14
19
15
## Installing the package
16
+
20
17
Hop into your console and install the package via Composer:
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.
59
+
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 you specify a value for `$cacheFor`. As long as `$cacheFor` is existent and is greater than `0`, all queries will be cached.
61
60
62
-
It is also possible to enable caching for specific queries. This is the recommended way because it is easier to manage each query.
61
+
It is also possible to enable caching for specific queries by not specifying `$cacheFor` and calling `cacheFor()` within your queries:
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.
126
141
127
142
```php
128
143
$user = User::with(['orders' => function ($query) {
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 you enabled caching (either by model variable or by the `cacheFor` scope), you can also opt to disable it mid-builder.
178
+
179
+
If you enabled caching (either by model variable or by the `cacheFor` scope), you can also opt to disable it within your query builder chains:
180
+
154
181
```php
155
182
$uncachedBooks = Book::dontCache()->get();
156
183
$uncachedBooks = Book::doNotCache()->get(); // same thing
157
184
```
158
185
159
186
## Equivalent Methods and Variables
187
+
160
188
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.
161
189
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.
162
190
@@ -174,6 +202,7 @@ class Book extends Model
174
202
```
175
203
176
204
## Implement the caching method to your own Builder class
205
+
177
206
Since this package modifies the `newBaseQueryBuilder()` in the model, having multiple traits that
This is how the default key generation function looks like:
249
+
219
250
```php
220
251
public function generatePlainCacheKey(string $method = 'get', $id = null, $appends = null): string
221
252
{
@@ -251,7 +282,9 @@ class MyCustomBuilder implements QueryCacheModuleInterface
251
282
```
252
283
253
284
## Implementing cache for other functions than get()
285
+
254
286
Since all of the Laravel Eloquent functions are based on it, the builder that comes with this package replaces only the `get()` one:
287
+
255
288
```php
256
289
class Builder
257
290
{
@@ -267,6 +300,7 @@ class Builder
267
300
```
268
301
269
302
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:
0 commit comments