Skip to content

Commit 9782a4c

Browse files
committed
Merge branch 'release/0.2.29'
2 parents 403dc41 + ad59342 commit 9782a4c

File tree

8 files changed

+222
-143
lines changed

8 files changed

+222
-143
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.2.29] - 18 Feb 2018
8+
### Added
9+
- hash collision detection and prevetion.
10+
711
## [0.2.28] - 18 Feb 2018
812
### Changed
913
- disabling of cache from using session to use cache-key instead.

src/CacheKey.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public function make(
3131
$key .= $this->getOffsetClause();
3232
$key .= $this->getLimitClause();
3333
$key .= $keyDifferentiator;
34-
$key = sha1($key);
3534

3635
return $key;
3736
}

src/CachedBuilder.php

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

19-
return $this->cache($this->makeCacheTags())
20-
->rememberForever(
21-
$this->makeCacheKey(['*'], null, "-avg_{$column}"),
22-
function () use ($column) {
23-
return parent::avg($column);
24-
}
25-
);
19+
$arguments = func_get_args();
20+
$cacheKey = $this->makeCacheKey(['*'], null, "-avg_{$column}");
21+
$method = 'avg';
22+
23+
return $this->cachedValue($arguments, $cacheKey, $method);
2624
}
2725

2826
public function count($columns = ['*'])
@@ -31,13 +29,11 @@ public function count($columns = ['*'])
3129
return parent::count($columns);
3230
}
3331

34-
return $this->cache($this->makeCacheTags())
35-
->rememberForever(
36-
$this->makeCacheKey(['*'], null, "-count"),
37-
function () use ($columns) {
38-
return parent::count($columns);
39-
}
40-
);
32+
$arguments = func_get_args();
33+
$cacheKey = $this->makeCacheKey(['*'], null, "-count");
34+
$method = 'count';
35+
36+
return $this->cachedValue($arguments, $cacheKey, $method);
4137
}
4238

4339
public function cursor()
@@ -46,13 +42,11 @@ public function cursor()
4642
return collect(parent::cursor());
4743
}
4844

49-
return $this->cache($this->makeCacheTags())
50-
->rememberForever(
51-
$this->makeCacheKey(['*'], null, "-cursor"),
52-
function () {
53-
return collect(parent::cursor());
54-
}
55-
);
45+
$arguments = func_get_args();
46+
$cacheKey = $this->makeCacheKey(['*'], null, "-cursor");
47+
$method = 'cursor';
48+
49+
return $this->cachedValue($arguments, $cacheKey, $method);
5650
}
5751

5852
public function delete()
@@ -72,13 +66,11 @@ public function find($id, $columns = ['*'])
7266
return parent::find($id, $columns);
7367
}
7468

75-
return $this->cache($this->makeCacheTags())
76-
->rememberForever(
77-
$this->makeCacheKey($columns, $id),
78-
function () use ($id, $columns) {
79-
return parent::find($id, $columns);
80-
}
81-
);
69+
$arguments = func_get_args();
70+
$cacheKey = $this->makeCacheKey($columns);
71+
$method = 'find';
72+
73+
return $this->cachedValue($arguments, $cacheKey, $method);
8274
}
8375

8476
public function first($columns = ['*'])
@@ -87,13 +79,11 @@ public function first($columns = ['*'])
8779
return parent::first($columns);
8880
}
8981

90-
return $this->cache($this->makeCacheTags())
91-
->rememberForever(
92-
$this->makeCacheKey($columns, null, '-first'),
93-
function () use ($columns) {
94-
return parent::first($columns);
95-
}
96-
);
82+
$arguments = func_get_args();
83+
$cacheKey = $this->makeCacheKey($columns);
84+
$method = 'first';
85+
86+
return $this->cachedValue($arguments, $cacheKey, $method);
9787
}
9888

9989
public function get($columns = ['*'])
@@ -102,13 +92,11 @@ public function get($columns = ['*'])
10292
return parent::get($columns);
10393
}
10494

105-
return $this->cache($this->makeCacheTags())
106-
->rememberForever(
107-
$this->makeCacheKey($columns),
108-
function () use ($columns) {
109-
return parent::get($columns);
110-
}
111-
);
95+
$arguments = func_get_args();
96+
$cacheKey = $this->makeCacheKey($columns);
97+
$method = 'get';
98+
99+
return $this->cachedValue($arguments, $cacheKey, $method);
112100
}
113101

114102
public function max($column)
@@ -117,13 +105,11 @@ public function max($column)
117105
return parent::max($column);
118106
}
119107

120-
return $this->cache($this->makeCacheTags())
121-
->rememberForever(
122-
$this->makeCacheKey(['*'], null, "-max_{$column}"),
123-
function () use ($column) {
124-
return parent::max($column);
125-
}
126-
);
108+
$arguments = func_get_args();
109+
$cacheKey = $this->makeCacheKey(['*'], null, "-max_{$column}");
110+
$method = 'max';
111+
112+
return $this->cachedValue($arguments, $cacheKey, $method);
127113
}
128114

129115
public function min($column)
@@ -132,13 +118,11 @@ public function min($column)
132118
return parent::min($column);
133119
}
134120

135-
return $this->cache($this->makeCacheTags())
136-
->rememberForever(
137-
$this->makeCacheKey(['*'], null, "-min_{$column}"),
138-
function () use ($column) {
139-
return parent::min($column);
140-
}
141-
);
121+
$arguments = func_get_args();
122+
$cacheKey = $this->makeCacheKey(['*'], null, "-min_{$column}");
123+
$method = 'min';
124+
125+
return $this->cachedValue($arguments, $cacheKey, $method);
142126
}
143127

144128
public function pluck($column, $key = null)
@@ -148,12 +132,11 @@ public function pluck($column, $key = null)
148132
}
149133

150134
$keyDifferentiator = "-pluck_{$column}" . ($key ? "_{$key}" : "");
135+
$arguments = func_get_args();
151136
$cacheKey = $this->makeCacheKey([$column], null, $keyDifferentiator);
137+
$method = 'pluck';
152138

153-
return $this->cache($this->makeCacheTags())
154-
->rememberForever($cacheKey, function () use ($column, $key) {
155-
return parent::pluck($column, $key);
156-
});
139+
return $this->cachedValue($arguments, $cacheKey, $method);
157140
}
158141

159142
public function sum($column)
@@ -162,13 +145,11 @@ public function sum($column)
162145
return parent::sum($column);
163146
}
164147

165-
return $this->cache($this->makeCacheTags())
166-
->rememberForever(
167-
$this->makeCacheKey(['*'], null, "-sum_{$column}"),
168-
function () use ($column) {
169-
return parent::sum($column);
170-
}
171-
);
148+
$arguments = func_get_args();
149+
$cacheKey = $this->makeCacheKey(['*'], null, "-sum_{$column}");
150+
$method = 'sum';
151+
152+
return $this->cachedValue($arguments, $cacheKey, $method);
172153
}
173154

174155
public function value($column)
@@ -177,11 +158,56 @@ public function value($column)
177158
return parent::value($column);
178159
}
179160

180-
return $this->cache($this->makeCacheTags())
161+
$arguments = func_get_args();
162+
$cacheKey = $this->makeCacheKey(['*'], null, "-value_{$column}");
163+
$method = 'value';
164+
165+
return $this->cachedValue($arguments, $cacheKey, $method);
166+
}
167+
168+
public function cachedValue(array $arguments, string $cacheKey, string $method)
169+
{
170+
$cacheTags = $this->makeCacheTags();
171+
$hashedCacheKey = sha1($cacheKey);
172+
173+
$result = $this->retrieveCachedValue(
174+
$arguments,
175+
$cacheKey,
176+
$cacheTags,
177+
$hashedCacheKey,
178+
$method
179+
);
180+
181+
if ($result['key'] !== $cacheKey) {
182+
cache()->tags($cacheTags)->forget($hashedCacheKey);
183+
}
184+
185+
$result = $this->retrieveCachedValue(
186+
$arguments,
187+
$cacheKey,
188+
$cacheTags,
189+
$hashedCacheKey,
190+
$method
191+
);
192+
193+
return $result['value'];
194+
}
195+
196+
protected function retrieveCachedValue(
197+
array $arguments,
198+
string $cacheKey,
199+
array $cacheTags,
200+
string $hashedCacheKey,
201+
string $method
202+
) {
203+
return $this->cache($cacheTags)
181204
->rememberForever(
182-
$this->makeCacheKey(['*'], null, "-value_{$column}"),
183-
function () use ($column) {
184-
return parent::value($column);
205+
$hashedCacheKey,
206+
function () use ($arguments, $cacheKey, $method) {
207+
return [
208+
'key' => $cacheKey,
209+
'value' => parent::{$method}(...$arguments),
210+
];
185211
}
186212
);
187213
}

tests/Unit/CacheKeyTest.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)