Skip to content

Commit

Permalink
Merge pull request #9 from rennokki/hotfix/expose-id-on-query-cache
Browse files Browse the repository at this point in the history
[hotfix] Expose $id on query cache
  • Loading branch information
rennokki authored Dec 14, 2019
2 parents 3e6ae1a + 2b98797 commit d676fd4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,38 @@ class MyCustomBuilder
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:

```php
class MyCustomBuilder
{
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], $id = null)
{
return function () use ($method, $columns, $id) {
$this->avoidCache = true;

// the function for find() caching
// accepts different params
if ($method === 'find') {
return $this->find($id, $columns);
}

return $this->{$method}($columns);
};
}

public function find($id, $columns = ['*'])
{
// implementing the same logic
if (! $this->shouldAvoidCache()) {
return $this->getFromQueryCache('find', $columns, $id);
}

return parent::find($id, $columns);
}
}
```
10 changes: 10 additions & 0 deletions src/Contracts/QueryCacheModuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ interface QueryCacheModuleInterface
* @return string
*/
public function generatePlainCacheKey(string $method = 'get', $id = null, $appends = null): string;

/**
* Get the query cache callback.
*
* @param string $method
* @param array $columns
* @param string|null $id
* @return \Closure
*/
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], $id = null);
}
8 changes: 5 additions & 3 deletions src/Traits/QueryCacheModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,18 @@ trait QueryCacheModule
* Get the cache from the current query.
*
* @param array $columns
* @param string|null $id
* @return array
*/
public function getFromQueryCache(string $method = 'get', $columns = ['*'])
public function getFromQueryCache(string $method = 'get', $columns = ['*'], $id = null)
{
if (is_null($this->columns)) {
$this->columns = $columns;
}

$key = $this->getCacheKey('get');
$cache = $this->getCache();
$callback = $this->getQueryCacheCallback($method, $columns);
$callback = $this->getQueryCacheCallback($method, $columns, $id);
$time = $this->getCacheTime();

if ($time instanceof DateTime || $time > 0) {
Expand All @@ -81,9 +82,10 @@ public function getFromQueryCache(string $method = 'get', $columns = ['*'])
*
* @param string $method
* @param array $columns
* @param string|null $id
* @return \Closure
*/
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'])
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], $id = null)
{
return function () use ($method, $columns) {
$this->avoidCache = true;
Expand Down

0 comments on commit d676fd4

Please sign in to comment.