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
Copy file name to clipboardExpand all lines: README.md
+112Lines changed: 112 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -141,3 +141,115 @@ class Book extends Model
141
141
public $cacheDriver = 'dynamodb'; // equivalent of ->cacheDriver('dynamodb');
142
142
}
143
143
```
144
+
145
+
## Implement the caching method to your own Builder class
146
+
Since this package modifies the `newBaseQueryBuilder()` in the model, having multiple traits that
147
+
modify this function will lead to an overlap.
148
+
149
+
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.
150
+
151
+
To solve this, all you have to do is to add the `\Rennokki\QueryCache\Traits\QueryCacheModule` trait and the `\Rennokki\QueryCache\Contracts\QueryCacheModuleInterface` interface to your `Builder` class. Make sure that the model will no longer use the original `QueryCacheable` trait.
152
+
153
+
```php
154
+
use Rennokki\QueryCache\Traits\QueryCacheModule;
155
+
use Illuminate\Database\Query\Builder as BaseBuilder; // the base laravel builder
156
+
use Rennokki\QueryCache\Contract\QueryCacheModuleInterface;
157
+
158
+
// MyCustomBuilder.php
159
+
class MyCustomBuilder implements QueryCacheModuleInterface
160
+
{
161
+
use QueryCacheModule;
162
+
163
+
// the rest of the logic here.
164
+
}
165
+
166
+
// MyBuilderTrait.php
167
+
trait MyBuilderTrait
168
+
{
169
+
protected function newBaseQueryBuilder()
170
+
{
171
+
return new MyCustomBuilder(
172
+
//
173
+
);
174
+
}
175
+
}
176
+
177
+
// app/CustomModel.php
178
+
class CustomModel extends Model
179
+
{
180
+
use MyBuilderTrait;
181
+
}
182
+
183
+
CustomModel::cacheFor(30)->customGetMethod();
184
+
```
185
+
186
+
## Generating your own key
187
+
This is how the default key generation function looks like:
188
+
```php
189
+
public function generatePlainCacheKey(string $method = 'get', $id = null, $appends = null): string
190
+
{
191
+
$name = $this->connection->getName();
192
+
193
+
// Count has no Sql, that's why it can't be used ->toSql()
## Implementing cache for other functions than get()
223
+
Since all of the Laravel Eloquent functions are based on it, the builder that comes with this package replaces only the `get()` one:
224
+
```php
225
+
class Builder
226
+
{
227
+
public function get($columns = ['*'])
228
+
{
229
+
if (! $this->shouldAvoidCache()) {
230
+
return $this->getFromQueryCache('get', $columns);
231
+
}
232
+
233
+
return parent::get($columns);
234
+
}
235
+
}
236
+
```
237
+
238
+
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:
239
+
```php
240
+
class MyCustomBuilder
241
+
{
242
+
public function count()
243
+
{
244
+
if (! $this->shouldAvoidCache()) {
245
+
return $this->getFromQueryCache('count');
246
+
}
247
+
248
+
return parent::count();
249
+
}
250
+
}
251
+
```
252
+
253
+
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 `['*']`.
254
+
255
+
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.
0 commit comments