From 4238a27ee8748e0ea13a827c3a3f504c8a70f852 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Sat, 14 Dec 2019 21:02:57 +0200 Subject: [PATCH 1/2] Exposing the $id. --- src/Contracts/QueryCacheModuleInterface.php | 10 ++++++++++ src/Traits/QueryCacheModule.php | 8 +++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Contracts/QueryCacheModuleInterface.php b/src/Contracts/QueryCacheModuleInterface.php index ac4812a..447b750 100644 --- a/src/Contracts/QueryCacheModuleInterface.php +++ b/src/Contracts/QueryCacheModuleInterface.php @@ -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); } diff --git a/src/Traits/QueryCacheModule.php b/src/Traits/QueryCacheModule.php index 7511996..533575a 100644 --- a/src/Traits/QueryCacheModule.php +++ b/src/Traits/QueryCacheModule.php @@ -56,9 +56,10 @@ 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; @@ -66,7 +67,7 @@ public function getFromQueryCache(string $method = 'get', $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) { @@ -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; From 2b9879705e77ec222c9e8f1d321d19179c83485c Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Sat, 14 Dec 2019 21:05:00 +0200 Subject: [PATCH 2/2] Updated readme. --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index 12b2c66..c9d8ac9 100644 --- a/README.md +++ b/README.md @@ -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); + } +} +```