Skip to content

Commit ad573d1

Browse files
committed
Merge branch 'master' into develop
2 parents 75784e6 + b75582c commit ad573d1

File tree

8 files changed

+231
-152
lines changed

8 files changed

+231
-152
lines changed

src/CacheKey.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,26 @@ protected function getValuesClause(array $where = null) : string
114114
return "";
115115
}
116116

117-
$values = is_array(array_get($where, "values"))
117+
$values = $this->getValuesFromWhere($where);
118+
$values = $this->getValuesFromBindings($values);
119+
120+
return "_" . $values;
121+
}
122+
123+
protected function getValuesFromWhere(array $where) : string
124+
{
125+
return is_array(array_get($where, "values"))
118126
? implode("_", $where["values"])
119127
: "";
128+
}
120129

130+
protected function getValuesFromBindings(string $values) : string
131+
{
121132
if (! $values && $this->query->bindings["where"] ?? false) {
122133
$values = implode("_", $this->query->bindings["where"]);
123134
}
124135

125-
return "_" . $values;
136+
return $values;
126137
}
127138

128139
protected function getWhereClauses(array $wheres = []) : string

src/CacheTags.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,40 @@ public function make() : array
2828
$tags = collect($this->eagerLoad)
2929
->keys()
3030
->map(function ($relationName) {
31-
$relation = collect(explode('.', $relationName))
32-
->reduce(function ($carry, $name) {
33-
if (! $carry) {
34-
$carry = $this->model;
35-
}
36-
37-
if ($carry instanceof Relation) {
38-
$carry = $carry->getQuery()->getModel();
39-
}
40-
41-
return $carry->{$name}();
42-
});
31+
$relation = $this->getRelation($relationName);
4332

4433
return $this->getCachePrefix()
4534
. str_slug(get_class($relation->getQuery()->getModel()));
4635
})
47-
->prepend(
48-
$this->getCachePrefix()
49-
. str_slug(get_class($this->model))
50-
)
36+
->prepend($this->getTagName())
5137
->values()
5238
->toArray();
5339

5440
return $tags;
5541
}
42+
43+
protected function getRelatedModel($carry) : Model
44+
{
45+
if ($carry instanceof Relation) {
46+
return $carry->getQuery()->getModel();
47+
}
48+
49+
return $carry;
50+
}
51+
52+
protected function getRelation(string $relationName) : Relation
53+
{
54+
return collect(explode('.', $relationName))
55+
->reduce(function ($carry, $name) {
56+
$carry = $carry ?: $this->model;
57+
$carry = $this->getRelatedModel($carry);
58+
59+
return $carry->{$name}();
60+
});
61+
}
62+
63+
protected function getTagName() : string
64+
{
65+
return $this->getCachePrefix() . str_slug(get_class($this->model));
66+
}
5667
}

src/CachedBuilder.php

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ public function avg($column)
1616
return parent::avg($column);
1717
}
1818

19-
$arguments = func_get_args();
2019
$cacheKey = $this->makeCacheKey(["*"], null, "-avg_{$column}");
21-
$method = "avg";
2220

23-
return $this->cachedValue($arguments, $cacheKey, $method);
21+
return $this->cachedValue(func_get_args(), $cacheKey);
2422
}
2523

2624
public function count($columns = ["*"])
@@ -29,24 +27,9 @@ public function count($columns = ["*"])
2927
return parent::count($columns);
3028
}
3129

32-
$arguments = func_get_args();
33-
$cacheKey = $this->makeCacheKey(["*"], null, "-count");
34-
$method = "count";
30+
$cacheKey = $this->makeCacheKey($columns, null, "-count");
3531

36-
return $this->cachedValue($arguments, $cacheKey, $method);
37-
}
38-
39-
public function cursor()
40-
{
41-
if (! $this->isCachable()) {
42-
return collect(parent::cursor());
43-
}
44-
45-
$arguments = func_get_args();
46-
$cacheKey = $this->makeCacheKey(["*"], null, "-cursor");
47-
$method = "cursor";
48-
49-
return $this->cachedValue($arguments, $cacheKey, $method);
32+
return $this->cachedValue(func_get_args(), $cacheKey);
5033
}
5134

5235
public function delete()
@@ -66,11 +49,9 @@ public function find($id, $columns = ["*"])
6649
return parent::find($id, $columns);
6750
}
6851

69-
$arguments = func_get_args();
70-
$cacheKey = $this->makeCacheKey(["*"], null, "-find_{$id}");
71-
$method = "find";
52+
$cacheKey = $this->makeCacheKey($columns, null, "-find_{$id}");
7253

73-
return $this->cachedValue($arguments, $cacheKey, $method);
54+
return $this->cachedValue(func_get_args(), $cacheKey);
7455
}
7556

7657
public function first($columns = ["*"])
@@ -79,11 +60,9 @@ public function first($columns = ["*"])
7960
return parent::first($columns);
8061
}
8162

82-
$arguments = func_get_args();
8363
$cacheKey = $this->makeCacheKey($columns);
84-
$method = "first";
8564

86-
return $this->cachedValue($arguments, $cacheKey, $method);
65+
return $this->cachedValue(func_get_args(), $cacheKey);
8766
}
8867

8968
public function get($columns = ["*"])
@@ -92,11 +71,16 @@ public function get($columns = ["*"])
9271
return parent::get($columns);
9372
}
9473

95-
$arguments = func_get_args();
9674
$cacheKey = $this->makeCacheKey($columns);
97-
$method = "get";
9875

99-
return $this->cachedValue($arguments, $cacheKey, $method);
76+
return $this->cachedValue(func_get_args(), $cacheKey);
77+
}
78+
79+
public function insert(array $values)
80+
{
81+
$this->checkCooldownAndFlushAfterPersiting($this->model);
82+
83+
return parent::insert($values);
10084
}
10185

10286
public function max($column)
@@ -105,11 +89,9 @@ public function max($column)
10589
return parent::max($column);
10690
}
10791

108-
$arguments = func_get_args();
10992
$cacheKey = $this->makeCacheKey(["*"], null, "-max_{$column}");
110-
$method = "max";
11193

112-
return $this->cachedValue($arguments, $cacheKey, $method);
94+
return $this->cachedValue(func_get_args(), $cacheKey);
11395
}
11496

11597
public function min($column)
@@ -118,11 +100,9 @@ public function min($column)
118100
return parent::min($column);
119101
}
120102

121-
$arguments = func_get_args();
122103
$cacheKey = $this->makeCacheKey(["*"], null, "-min_{$column}");
123-
$method = "min";
124104

125-
return $this->cachedValue($arguments, $cacheKey, $method);
105+
return $this->cachedValue(func_get_args(), $cacheKey);
126106
}
127107

128108
public function paginate(
@@ -135,12 +115,10 @@ public function paginate(
135115
return parent::paginate($perPage, $columns, $pageName, $page);
136116
}
137117

138-
$arguments = func_get_args();
139118
$page = $page ?: 1;
140119
$cacheKey = $this->makeCacheKey($columns, null, "-paginate_by_{$perPage}_{$pageName}_{$page}");
141-
$method = "paginate";
142120

143-
return $this->cachedValue($arguments, $cacheKey, $method);
121+
return $this->cachedValue(func_get_args(), $cacheKey);
144122
}
145123

146124
public function pluck($column, $key = null)
@@ -150,11 +128,9 @@ public function pluck($column, $key = null)
150128
}
151129

152130
$keyDifferentiator = "-pluck_{$column}" . ($key ? "_{$key}" : "");
153-
$arguments = func_get_args();
154131
$cacheKey = $this->makeCacheKey([$column], null, $keyDifferentiator);
155-
$method = "pluck";
156132

157-
return $this->cachedValue($arguments, $cacheKey, $method);
133+
return $this->cachedValue(func_get_args(), $cacheKey);
158134
}
159135

160136
public function sum($column)
@@ -163,11 +139,16 @@ public function sum($column)
163139
return parent::sum($column);
164140
}
165141

166-
$arguments = func_get_args();
167142
$cacheKey = $this->makeCacheKey(["*"], null, "-sum_{$column}");
168-
$method = "sum";
169143

170-
return $this->cachedValue($arguments, $cacheKey, $method);
144+
return $this->cachedValue(func_get_args(), $cacheKey);
145+
}
146+
147+
public function update(array $values)
148+
{
149+
$this->checkCooldownAndFlushAfterPersiting($this->model);
150+
151+
return parent::update($values);
171152
}
172153

173154
public function value($column)
@@ -176,15 +157,14 @@ public function value($column)
176157
return parent::value($column);
177158
}
178159

179-
$arguments = func_get_args();
180160
$cacheKey = $this->makeCacheKey(["*"], null, "-value_{$column}");
181-
$method = "value";
182161

183-
return $this->cachedValue($arguments, $cacheKey, $method);
162+
return $this->cachedValue(func_get_args(), $cacheKey);
184163
}
185164

186-
public function cachedValue(array $arguments, string $cacheKey, string $method)
165+
public function cachedValue(array $arguments, string $cacheKey)
187166
{
167+
$method = debug_backtrace()[1]['function'];
188168
$cacheTags = $this->makeCacheTags();
189169
$hashedCacheKey = sha1($cacheKey);
190170
$result = $this->retrieveCachedValue(

src/Console/Commands/Flush.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,27 @@ class Flush extends Command
1010
public function handle()
1111
{
1212
$option = $this->option('model');
13-
13+
1414
if (! $option) {
15-
cache()
16-
->store(config('laravel-model-caching.store'))
17-
->flush();
15+
return $this->flushEntireCache();
16+
}
1817

19-
$this->info("✔︎ Entire model cache has been flushed.");
18+
return $this->flushModelCache($option);
19+
}
2020

21-
return 0;
22-
}
21+
protected function flushEntireCache() : int
22+
{
23+
cache()
24+
->store(config('laravel-model-caching.store'))
25+
->flush();
2326

27+
$this->info("✔︎ Entire model cache has been flushed.");
28+
29+
return 0;
30+
}
31+
32+
protected function flushModelCache(string $option) : int
33+
{
2434
$model = new $option;
2535
$usesCachableTrait = collect(class_uses($model))
2636
->contains("GeneaLabs\LaravelModelCaching\Traits\Cachable");
@@ -34,5 +44,7 @@ public function handle()
3444

3545
$model->flushCache();
3646
$this->info("✔︎ Cache for model '{$option}' has been flushed.");
47+
48+
return 0;
3749
}
3850
}

0 commit comments

Comments
 (0)