Skip to content

Commit

Permalink
Merge pull request #93 from renoki-co/feature/method-defined-caching
Browse files Browse the repository at this point in the history
[3.x] Cache by methods defined in the model
  • Loading branch information
rennokki authored Sep 25, 2021
2 parents cc7c910 + dce83f5 commit 3168fb2
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/FlushQueryCacheObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/Traits/QueryCacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
10 changes: 10 additions & 0 deletions tests/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ class Book extends Model
protected $fillable = [
'name',
];

protected function cacheUsePlainKeyValue()
{
return $this->cacheUsePlainKey;
}

protected function cacheForValue()
{
return 3600;
}
}
10 changes: 10 additions & 0 deletions tests/Models/Kid.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@ protected function getCacheBaseTags(): array
//
];
}

protected function cacheUsePlainKeyValue()
{
return $this->cacheUsePlainKey;
}

protected function cacheForValue()
{
return 3600;
}
}
10 changes: 10 additions & 0 deletions tests/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ protected function getCacheBaseTags(): array
'test',
];
}

protected function cacheUsePlainKeyValue()
{
return $this->cacheUsePlainKey;
}

protected function cacheForValue()
{
return 3600;
}
}
10 changes: 10 additions & 0 deletions tests/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@ protected function getCacheBaseTags(): array
//
];
}

protected function cacheUsePlainKeyValue()
{
return $this->cacheUsePlainKey;
}

protected function cacheForValue()
{
return 3600;
}
}
10 changes: 10 additions & 0 deletions tests/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@ protected function getCacheBaseTags(): array
//
];
}

protected function cacheUsePlainKeyValue()
{
return $this->cacheUsePlainKey;
}

protected function cacheForValue()
{
return 3600;
}
}
10 changes: 10 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down

0 comments on commit 3168fb2

Please sign in to comment.