From aabf8b9d365b48eb5cd0b4d936d6692494772546 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Sat, 25 Sep 2021 10:59:35 +0300 Subject: [PATCH 1/2] Added methods to cache --- src/Traits/QueryCacheable.php | 20 ++++++++++++++++++++ tests/Models/Book.php | 10 ++++++++++ tests/Models/Kid.php | 10 ++++++++++ tests/Models/Page.php | 10 ++++++++++ tests/Models/Post.php | 10 ++++++++++ tests/Models/Role.php | 10 ++++++++++ tests/Models/User.php | 10 ++++++++++ 7 files changed, 80 insertions(+) diff --git a/src/Traits/QueryCacheable.php b/src/Traits/QueryCacheable.php index eaab077..fb0ced2 100644 --- a/src/Traits/QueryCacheable.php +++ b/src/Traits/QueryCacheable.php @@ -91,22 +91,42 @@ protected function newBaseQueryBuilder() $builder->cacheFor($this->cacheFor); } + if (method_exists($this, 'cacheForValue')) { + $builder->cacheFor($this->cacheForValue($builder)); + } + if ($this->cacheTags) { $builder->cacheTags($this->cacheTags); } + if (method_exists($this, 'cacheTagsValue')) { + $builder->cacheTags($this->cacheTagsValue($builder)); + } + if ($this->cachePrefix) { $builder->cachePrefix($this->cachePrefix); } + if (method_exists($this, 'cachePrefixValue')) { + $builder->cachePrefix($this->cachePrefixValue($builder)); + } + if ($this->cacheDriver) { $builder->cacheDriver($this->cacheDriver); } + if (method_exists($this, 'cacheDriverValue')) { + $builder->cacheDriver($this->cacheDriverValue($builder)); + } + if ($this->cacheUsePlainKey) { $builder->withPlainKey(); } + if (method_exists($this, 'cacheUsePlainKeyValue')) { + $builder->withPlainKey($this->cacheUsePlainKeyValue($builder)); + } + return $builder->cacheBaseTags($this->getCacheBaseTags()); } } diff --git a/tests/Models/Book.php b/tests/Models/Book.php index 3b923f2..7cef648 100644 --- a/tests/Models/Book.php +++ b/tests/Models/Book.php @@ -14,4 +14,14 @@ class Book extends Model protected $fillable = [ 'name', ]; + + protected function cacheUsePlainKeyValue() + { + return $this->cacheUsePlainKey; + } + + protected function cacheForValue() + { + return 3600; + } } diff --git a/tests/Models/Kid.php b/tests/Models/Kid.php index a5c23c8..cbe039c 100644 --- a/tests/Models/Kid.php +++ b/tests/Models/Kid.php @@ -21,4 +21,14 @@ protected function getCacheBaseTags(): array // ]; } + + protected function cacheUsePlainKeyValue() + { + return $this->cacheUsePlainKey; + } + + protected function cacheForValue() + { + return 3600; + } } diff --git a/tests/Models/Page.php b/tests/Models/Page.php index d9de9a8..c1f622b 100644 --- a/tests/Models/Page.php +++ b/tests/Models/Page.php @@ -23,4 +23,14 @@ protected function getCacheBaseTags(): array 'test', ]; } + + protected function cacheUsePlainKeyValue() + { + return $this->cacheUsePlainKey; + } + + protected function cacheForValue() + { + return 3600; + } } diff --git a/tests/Models/Post.php b/tests/Models/Post.php index 3454d9c..74d671b 100644 --- a/tests/Models/Post.php +++ b/tests/Models/Post.php @@ -21,4 +21,14 @@ protected function getCacheBaseTags(): array // ]; } + + protected function cacheUsePlainKeyValue() + { + return $this->cacheUsePlainKey; + } + + protected function cacheForValue() + { + return 3600; + } } diff --git a/tests/Models/Role.php b/tests/Models/Role.php index 17500f2..b212b01 100644 --- a/tests/Models/Role.php +++ b/tests/Models/Role.php @@ -21,4 +21,14 @@ protected function getCacheBaseTags(): array // ]; } + + protected function cacheUsePlainKeyValue() + { + return $this->cacheUsePlainKey; + } + + protected function cacheForValue() + { + return 3600; + } } diff --git a/tests/Models/User.php b/tests/Models/User.php index 7901202..adbeae7 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -32,6 +32,16 @@ protected function getCacheBaseTags(): array ]; } + protected function cacheUsePlainKeyValue() + { + return $this->cacheUsePlainKey; + } + + protected function cacheForValue() + { + return 3600; + } + public function getCacheTagsToInvalidateOnUpdate($relation = null, $pivotedModels = null): array { if ($relation === 'roles') { From dce83f58c70aa937d00623f334090ad7beb50026 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Sat, 25 Sep 2021 11:03:43 +0300 Subject: [PATCH 2/2] csfixing --- src/FlushQueryCacheObserver.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/FlushQueryCacheObserver.php b/src/FlushQueryCacheObserver.php index 00b223b..ba049ff 100644 --- a/src/FlushQueryCacheObserver.php +++ b/src/FlushQueryCacheObserver.php @@ -147,6 +147,7 @@ public function morphToManyUpdatedExistingPivot($relation, Model $model, $ids) * @param string|null $relation * @param \Illuminate\Database\Eloquent\Collection|null $pivotedModels * @return void + * * @throws Exception */ protected function invalidateCache(Model $model, $relation = null, $pivotedModels = null): void