Skip to content

Commit bb7cca4

Browse files
authored
Merge pull request #53 from GeneaLabs/feature/hash-cache-keys
feature/hash-cache-keys
2 parents 15730e0 + d8bd679 commit bb7cca4

11 files changed

+170
-92
lines changed

src/CacheKey.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ public function __construct(array $eagerLoad, Model $model, Builder $query)
1717
$this->query = $query;
1818
}
1919

20-
public function make(array $columns = ['*'], $idColumn = null) : string
21-
{
20+
public function make(
21+
array $columns = ['*'],
22+
$idColumn = null,
23+
string $keyDifferentiator = ''
24+
) : string {
2225
$key = $this->getModelSlug();
2326
$key .= $this->getIdColumn($idColumn ?: '');
2427
$key .= $this->getQueryColumns($columns);
@@ -27,6 +30,8 @@ public function make(array $columns = ['*'], $idColumn = null) : string
2730
$key .= $this->getOrderByClauses();
2831
$key .= $this->getOffsetClause();
2932
$key .= $this->getLimitClause();
33+
$key .= $keyDifferentiator;
34+
$key = sha1($key);
3035

3136
return $key;
3237
}

src/CachedBuilder.php

Lines changed: 62 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ public function avg($column)
1717
}
1818

1919
return $this->cache($this->makeCacheTags())
20-
->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) {
21-
return parent::avg($column);
22-
});
20+
->rememberForever(
21+
$this->makeCacheKey(['*'], null, "-avg_{$column}"),
22+
function () use ($column) {
23+
return parent::avg($column);
24+
}
25+
);
2326
}
2427

2528
public function count($columns = ['*'])
@@ -29,9 +32,12 @@ public function count($columns = ['*'])
2932
}
3033

3134
return $this->cache($this->makeCacheTags())
32-
->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) {
33-
return parent::count($columns);
34-
});
35+
->rememberForever(
36+
$this->makeCacheKey(['*'], null, "-count"),
37+
function () use ($columns) {
38+
return parent::count($columns);
39+
}
40+
);
3541
}
3642

3743
public function cursor()
@@ -41,9 +47,12 @@ public function cursor()
4147
}
4248

4349
return $this->cache($this->makeCacheTags())
44-
->rememberForever($this->makeCacheKey() . "-cursor", function () {
45-
return collect(parent::cursor());
46-
});
50+
->rememberForever(
51+
$this->makeCacheKey(['*'], null, "-cursor"),
52+
function () {
53+
return collect(parent::cursor());
54+
}
55+
);
4756
}
4857

4958
public function delete()
@@ -64,9 +73,12 @@ public function find($id, $columns = ['*'])
6473
}
6574

6675
return $this->cache($this->makeCacheTags())
67-
->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) {
68-
return parent::find($id, $columns);
69-
});
76+
->rememberForever(
77+
$this->makeCacheKey($columns, $id),
78+
function () use ($id, $columns) {
79+
return parent::find($id, $columns);
80+
}
81+
);
7082
}
7183

7284
public function first($columns = ['*'])
@@ -76,9 +88,12 @@ public function first($columns = ['*'])
7688
}
7789

7890
return $this->cache($this->makeCacheTags())
79-
->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) {
80-
return parent::first($columns);
81-
});
91+
->rememberForever(
92+
$this->makeCacheKey($columns, null, '-first'),
93+
function () use ($columns) {
94+
return parent::first($columns);
95+
}
96+
);
8297
}
8398

8499
public function get($columns = ['*'])
@@ -88,9 +103,12 @@ public function get($columns = ['*'])
88103
}
89104

90105
return $this->cache($this->makeCacheTags())
91-
->rememberForever($this->makeCacheKey($columns), function () use ($columns) {
92-
return parent::get($columns);
93-
});
106+
->rememberForever(
107+
$this->makeCacheKey($columns),
108+
function () use ($columns) {
109+
return parent::get($columns);
110+
}
111+
);
94112
}
95113

96114
public function max($column)
@@ -100,9 +118,12 @@ public function max($column)
100118
}
101119

102120
return $this->cache($this->makeCacheTags())
103-
->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) {
104-
return parent::max($column);
105-
});
121+
->rememberForever(
122+
$this->makeCacheKey(['*'], null, "-max_{$column}"),
123+
function () use ($column) {
124+
return parent::max($column);
125+
}
126+
);
106127
}
107128

108129
public function min($column)
@@ -112,9 +133,12 @@ public function min($column)
112133
}
113134

114135
return $this->cache($this->makeCacheTags())
115-
->rememberForever($this->makeCacheKey() . "-min_{$column}", function () use ($column) {
116-
return parent::min($column);
117-
});
136+
->rememberForever(
137+
$this->makeCacheKey(['*'], null, "-min_{$column}"),
138+
function () use ($column) {
139+
return parent::min($column);
140+
}
141+
);
118142
}
119143

120144
public function pluck($column, $key = null)
@@ -123,11 +147,8 @@ public function pluck($column, $key = null)
123147
return parent::pluck($column, $key);
124148
}
125149

126-
$cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}";
127-
128-
if ($key) {
129-
$cacheKey .= "_{$key}";
130-
}
150+
$keyDifferentiator = "-pluck_{$column}" . ($key ? "_{$key}" : "");
151+
$cacheKey = $this->makeCacheKey([$column], null, $keyDifferentiator);
131152

132153
return $this->cache($this->makeCacheTags())
133154
->rememberForever($cacheKey, function () use ($column, $key) {
@@ -142,9 +163,12 @@ public function sum($column)
142163
}
143164

144165
return $this->cache($this->makeCacheTags())
145-
->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) {
146-
return parent::sum($column);
147-
});
166+
->rememberForever(
167+
$this->makeCacheKey(['*'], null, "-sum_{$column}"),
168+
function () use ($column) {
169+
return parent::sum($column);
170+
}
171+
);
148172
}
149173

150174
public function value($column)
@@ -154,8 +178,11 @@ public function value($column)
154178
}
155179

156180
return $this->cache($this->makeCacheTags())
157-
->rememberForever($this->makeCacheKey() . "-value_{$column}", function () use ($column) {
158-
return parent::value($column);
159-
});
181+
->rememberForever(
182+
$this->makeCacheKey(['*'], null, "-value_{$column}"),
183+
function () use ($column) {
184+
return parent::value($column);
185+
}
186+
);
160187
}
161188
}

src/CachedModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static function all($columns = ['*'])
5555
$class = get_called_class();
5656
$instance = new $class;
5757
$tags = [str_slug(get_called_class())];
58-
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
58+
$key = $instance->makeCacheKey();
5959

6060
return $instance->cache($tags)
6161
->rememberForever($key, function () use ($columns) {

src/Traits/Cachable.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use GeneaLabs\LaravelModelCaching\CacheTags;
55
use GeneaLabs\LaravelModelCaching\CachedModel;
66
use Illuminate\Cache\TaggableStore;
7+
use Illuminate\Database\Query\Builder;
78

89
trait Cachable
910
{
@@ -41,10 +42,17 @@ public function flushCache(array $tags = [])
4142
$this->cache($tags)->flush();
4243
}
4344

44-
protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string
45-
{
46-
return (new CacheKey($this->eagerLoad, $this->model, $this->query))
47-
->make($columns, $idColumn);
45+
protected function makeCacheKey(
46+
array $columns = ['*'],
47+
$idColumn = null,
48+
string $keyDifferentiator = ''
49+
) : string {
50+
$eagerLoad = $this->eagerLoad ?? [];
51+
$model = $this->model ?? $this;
52+
$query = $this->query ?? app(Builder::class);
53+
54+
return (new CacheKey($eagerLoad, $model, $query))
55+
->make($columns, $idColumn, $keyDifferentiator);
4856
}
4957

5058
protected function makeCacheTags() : array

tests/Unit/CacheKeyTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Unit;
2+
3+
use GeneaLabs\LaravelModelCaching\CachedBuilder;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
5+
use GeneaLabs\LaravelModelCaching\Tests\UnitTestCase;
6+
use ReflectionMethod;
7+
8+
class CacheKeyTest extends UnitTestCase
9+
{
10+
public function testKeyIsSHA1()
11+
{
12+
$makeCacheKey = new ReflectionMethod(
13+
CachedBuilder::class,
14+
'makeCacheKey'
15+
);
16+
$makeCacheKey->setAccessible(true);
17+
18+
$builder = (new Author)->startsWithA();
19+
$key = $makeCacheKey->invoke($builder);
20+
21+
$this->assertTrue(strlen($key) === 40 && ctype_xdigit($key));
22+
}
23+
}

0 commit comments

Comments
 (0)