Skip to content

Commit d676fd4

Browse files
authored
Merge pull request #9 from rennokki/hotfix/expose-id-on-query-cache
[hotfix] Expose $id on query cache
2 parents 3e6ae1a + 2b98797 commit d676fd4

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,38 @@ class MyCustomBuilder
253253
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 `['*']`.
254254

255255
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.
256+
257+
Note that some functions like `getQueryCacheCallback()` may come with an `$id` parameter.
258+
The default behaviour of the package doesn't use it, since the query builder uses `->get()` by default that accepts only columns.
259+
260+
However, if your builder replaces functions like `find()`, `$id` is needed and you will also have to replace the `getQueryCacheCallback()` like so:
261+
262+
```php
263+
class MyCustomBuilder
264+
{
265+
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], $id = null)
266+
{
267+
return function () use ($method, $columns, $id) {
268+
$this->avoidCache = true;
269+
270+
// the function for find() caching
271+
// accepts different params
272+
if ($method === 'find') {
273+
return $this->find($id, $columns);
274+
}
275+
276+
return $this->{$method}($columns);
277+
};
278+
}
279+
280+
public function find($id, $columns = ['*'])
281+
{
282+
// implementing the same logic
283+
if (! $this->shouldAvoidCache()) {
284+
return $this->getFromQueryCache('find', $columns, $id);
285+
}
286+
287+
return parent::find($id, $columns);
288+
}
289+
}
290+
```

src/Contracts/QueryCacheModuleInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@ interface QueryCacheModuleInterface
1313
* @return string
1414
*/
1515
public function generatePlainCacheKey(string $method = 'get', $id = null, $appends = null): string;
16+
17+
/**
18+
* Get the query cache callback.
19+
*
20+
* @param string $method
21+
* @param array $columns
22+
* @param string|null $id
23+
* @return \Closure
24+
*/
25+
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], $id = null);
1626
}

src/Traits/QueryCacheModule.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,18 @@ trait QueryCacheModule
5656
* Get the cache from the current query.
5757
*
5858
* @param array $columns
59+
* @param string|null $id
5960
* @return array
6061
*/
61-
public function getFromQueryCache(string $method = 'get', $columns = ['*'])
62+
public function getFromQueryCache(string $method = 'get', $columns = ['*'], $id = null)
6263
{
6364
if (is_null($this->columns)) {
6465
$this->columns = $columns;
6566
}
6667

6768
$key = $this->getCacheKey('get');
6869
$cache = $this->getCache();
69-
$callback = $this->getQueryCacheCallback($method, $columns);
70+
$callback = $this->getQueryCacheCallback($method, $columns, $id);
7071
$time = $this->getCacheTime();
7172

7273
if ($time instanceof DateTime || $time > 0) {
@@ -81,9 +82,10 @@ public function getFromQueryCache(string $method = 'get', $columns = ['*'])
8182
*
8283
* @param string $method
8384
* @param array $columns
85+
* @param string|null $id
8486
* @return \Closure
8587
*/
86-
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'])
88+
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], $id = null)
8789
{
8890
return function () use ($method, $columns) {
8991
$this->avoidCache = true;

0 commit comments

Comments
 (0)