Skip to content

Commit 898bb67

Browse files
- replaced
$eagerLoad = $this->eagerLoad ?? []; $model = $this->model ?? $this; $query = $this->query ?? app(Builder::class); with $eagerLoad = $this->retrieveEagerLoad(); $model = $this->retrieveCacheModel(); $query = $this->retrieveCacheQuery(); to avoid name collisions in case the model contains one of the 'eagerLoad', 'mode' or 'query' attributes - added makeCachePrefix and used it to prefix cache tags and keyDifferentiator and class name tag with the value returned by the 'getCachePrefix' method of the model This method can be used in a multitenant implementation where an object with the same key and tags is cached for each tenant database connection. - added CacheGlobal class to be a placeholder for the global static cache disable flag. The cached value using $cache might generate unpredictable behaviour in case multiple requests are made to the server in the same time.
1 parent 403dc41 commit 898bb67

File tree

1 file changed

+77
-12
lines changed

1 file changed

+77
-12
lines changed

src/Traits/Cachable.php

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
use Illuminate\Database\Query\Builder;
88
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
99
use GeneaLabs\LaravelModelCaching\CachedBuilder;
10+
use Illuminate\Database\Eloquent\Model;
11+
12+
use GeneaLabs\LaravelModelCaching\CacheGlobal;
1013

1114
trait Cachable
1215
{
1316
protected $isCachable = true;
14-
protected static $isCachableKey = 'genealabs:model-caching:is-disabled';
1517

1618
protected function cache(array $tags = [])
1719
{
@@ -22,8 +24,8 @@ protected function cache(array $tags = [])
2224
}
2325

2426
if (is_subclass_of($cache->getStore(), TaggableStore::class)) {
25-
if (is_a($this, CachedModel::class)) {
26-
array_push($tags, str_slug(get_called_class()));
27+
if (is_a($this, Model::class)) {
28+
array_push($tags, $this->makeCachePrefix(str_slug(get_called_class())));
2729
}
2830

2931
$cache = $cache->tags($tags);
@@ -34,7 +36,8 @@ protected function cache(array $tags = [])
3436

3537
public function disableCache()
3638
{
37-
cache()->forever(self::$isCachableKey, true);
39+
40+
CacheGlobal::disableCache();
3841

3942
$this->isCachable = false;
4043

@@ -50,24 +53,86 @@ public function flushCache(array $tags = [])
5053
$this->cache($tags)->flush();
5154
}
5255

56+
protected function retrieveEagerLoad()
57+
{
58+
if (is_a($this, Model::class)) {
59+
return [];
60+
}
61+
if (is_a($this, EloquentBuilder::class)) {
62+
return $this->eagerLoad ?? [];
63+
}
64+
return null;
65+
}
66+
67+
protected function retrieveCacheModel()
68+
{
69+
if (is_a($this, Model::class)) {
70+
return $this;
71+
}
72+
if (is_a($this, EloquentBuilder::class)) {
73+
return $this->model;
74+
}
75+
return null;
76+
}
77+
78+
protected function retrieveCacheQuery()
79+
{
80+
if (is_a($this, Model::class)) {
81+
return app(Builder::class);
82+
}
83+
if (is_a($this, EloquentBuilder::class)) {
84+
return $this->query;
85+
}
86+
return null;
87+
}
88+
89+
protected function makeCachePrefix($elementMix)
90+
{
91+
$model = $this->retrieveCacheModel();
92+
if (!method_exists($model, "getCachePrefix")) {
93+
return $elementMix;
94+
}
95+
96+
$result = null;
97+
$cachePrefix = $model->getCachePrefix();
98+
if ($cachePrefix == null) {
99+
return $elementMix;
100+
}
101+
if (is_array($elementMix)) {
102+
$result = [];
103+
foreach ($elementMix as $value) {
104+
array_push($result, $cachePrefix . '-' . $value);
105+
}
106+
return $result;
107+
} else {
108+
$result = $cachePrefix . '-' . $elementMix;
109+
}
110+
return $result;
111+
}
112+
53113
protected function makeCacheKey(
54114
array $columns = ['*'],
55115
$idColumn = null,
56116
string $keyDifferentiator = ''
57117
) : string {
58-
$eagerLoad = $this->eagerLoad ?? [];
59-
$model = $this->model ?? $this;
60-
$query = $this->query ?? app(Builder::class);
118+
$eagerLoad = $this->retrieveEagerLoad();
119+
$model = $this->retrieveCacheModel();
120+
$query = $this->retrieveCacheQuery();
61121

62122
return (new CacheKey($eagerLoad, $model, $query))
63-
->make($columns, $idColumn, $keyDifferentiator);
123+
->make($columns, $idColumn, $this->makeCachePrefix($keyDifferentiator));
64124
}
65125

66126
protected function makeCacheTags() : array
67127
{
68-
$tags = (new CacheTags($this->eagerLoad ?? [], $this->model ?? $this))
128+
$eagerLoad = $this->retrieveEagerLoad();
129+
$model = $this->retrieveCacheModel();
130+
131+
$tags = (new CacheTags($eagerLoad, $model))
69132
->make();
70133

134+
$tags = $this->makeCachePrefix($tags);
135+
71136
return $tags;
72137
}
73138

@@ -80,7 +145,7 @@ public static function bootCachable()
80145

81146
public static function all($columns = ['*'])
82147
{
83-
if (cache()->get(self::$isCachableKey)) {
148+
if (CacheGlobal::isDisabled()) {
84149
return parent::all($columns);
85150
}
86151

@@ -97,8 +162,8 @@ public static function all($columns = ['*'])
97162

98163
public function newEloquentBuilder($query)
99164
{
100-
if (cache()->get(self::$isCachableKey)) {
101-
cache()->forget(self::$isCachableKey);
165+
if (CacheGlobal::isDisabled()) {
166+
CacheGlobal::enableCache();
102167

103168
return new EloquentBuilder($query);
104169
}

0 commit comments

Comments
 (0)