Skip to content

Commit 1fcb964

Browse files
committed
Implemented passing a key suffix to CacheKey.
1 parent 83690c5 commit 1fcb964

File tree

2 files changed

+68
-38
lines changed

2 files changed

+68
-38
lines changed

src/CachedBuilder.php

Lines changed: 61 additions & 31 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,7 +147,7 @@ public function pluck($column, $key = null)
123147
return parent::pluck($column, $key);
124148
}
125149

126-
$cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}";
150+
$cacheKey = $this->makeCacheKey([$column], null, "-pluck_{$column}");
127151

128152
if ($key) {
129153
$cacheKey .= "_{$key}";
@@ -142,9 +166,12 @@ public function sum($column)
142166
}
143167

144168
return $this->cache($this->makeCacheTags())
145-
->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) {
146-
return parent::sum($column);
147-
});
169+
->rememberForever(
170+
$this->makeCacheKey(['*'], null, "-sum_{$column}"),
171+
function () use ($column) {
172+
return parent::sum($column);
173+
}
174+
);
148175
}
149176

150177
public function value($column)
@@ -154,8 +181,11 @@ public function value($column)
154181
}
155182

156183
return $this->cache($this->makeCacheTags())
157-
->rememberForever($this->makeCacheKey() . "-value_{$column}", function () use ($column) {
158-
return parent::value($column);
159-
});
184+
->rememberForever(
185+
$this->makeCacheKey(['*'], null, "-value_{$column}"),
186+
function () use ($column) {
187+
return parent::value($column);
188+
}
189+
);
160190
}
161191
}

src/Traits/Cachable.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public function flushCache(array $tags = [])
4141
$this->cache($tags)->flush();
4242
}
4343

44-
protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string
45-
{
46-
$cacheKey = (new CacheKey($this->eagerLoad, $this->model, $this->query));
47-
if ($suffix) {
48-
$cacheKey->setSuffix($suffix);
49-
}
50-
return $cacheKey->make($columns, $idColumn, $suffix);
44+
protected function makeCacheKey(
45+
array $columns = ['*'],
46+
$idColumn = null,
47+
string $suffix = ''
48+
) : string {
49+
return (new CacheKey($this->eagerLoad, $this->model, $this->query))
50+
->make($columns, $idColumn, $suffix);
5151
}
5252

5353
protected function makeCacheTags() : array

0 commit comments

Comments
 (0)