1
|
|
|
<?php namespace GeneaLabs\LaravelModelCaching; |
2
|
|
|
|
3
|
|
|
use GeneaLabs\LaravelModelCaching\Traits\Cachable; |
4
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
8
|
|
|
*/ |
9
|
|
|
class CachedBuilder extends EloquentBuilder |
10
|
|
|
{ |
11
|
|
|
use Cachable; |
12
|
|
|
|
13
|
|
|
public function avg($column) |
14
|
|
|
{ |
15
|
|
|
if (! $this->isCachable) { |
16
|
|
|
return parent::avg($column); |
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
$arguments = func_get_args(); |
20
|
|
|
$cacheKey = $this->makeCacheKey(['*'], null, "-avg_{$column}"); |
21
|
|
|
$method = 'avg'; |
22
|
|
|
|
23
|
|
|
return $this->cachedValue($arguments, $cacheKey, $method); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function count($columns = ['*']) |
27
|
|
|
{ |
28
|
|
|
if (! $this->isCachable) { |
29
|
|
|
return parent::count($columns); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$arguments = func_get_args(); |
33
|
|
|
$cacheKey = $this->makeCacheKey(['*'], null, "-count"); |
34
|
|
|
$method = 'count'; |
35
|
|
|
|
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); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function delete() |
53
|
|
|
{ |
54
|
|
|
$this->cache($this->makeCacheTags()) |
55
|
|
|
->flush(); |
56
|
|
|
|
57
|
|
|
return parent::delete(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
62
|
|
|
*/ |
63
|
|
|
public function find($id, $columns = ['*']) |
64
|
|
|
{ |
65
|
|
|
if (! $this->isCachable) { |
66
|
|
|
return parent::find($id, $columns); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$arguments = func_get_args(); |
70
|
|
|
$cacheKey = $this->makeCacheKey($columns); |
71
|
|
|
$method = 'find'; |
72
|
|
|
|
73
|
|
|
return $this->cachedValue($arguments, $cacheKey, $method); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function first($columns = ['*']) |
77
|
|
|
{ |
78
|
|
|
if (! $this->isCachable) { |
79
|
|
|
return parent::first($columns); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$arguments = func_get_args(); |
83
|
|
|
$cacheKey = $this->makeCacheKey($columns); |
84
|
|
|
$method = 'first'; |
85
|
|
|
|
86
|
|
|
return $this->cachedValue($arguments, $cacheKey, $method); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function get($columns = ['*']) |
90
|
|
|
{ |
91
|
|
|
if (! $this->isCachable) { |
92
|
|
|
return parent::get($columns); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$arguments = func_get_args(); |
96
|
|
|
$cacheKey = $this->makeCacheKey($columns); |
97
|
|
|
$method = 'get'; |
98
|
|
|
|
99
|
|
|
return $this->cachedValue($arguments, $cacheKey, $method); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function max($column) |
103
|
|
|
{ |
104
|
|
|
if (! $this->isCachable) { |
105
|
|
|
return parent::max($column); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$arguments = func_get_args(); |
109
|
|
|
$cacheKey = $this->makeCacheKey(['*'], null, "-max_{$column}"); |
110
|
|
|
$method = 'max'; |
111
|
|
|
|
112
|
|
|
return $this->cachedValue($arguments, $cacheKey, $method); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function min($column) |
116
|
|
|
{ |
117
|
|
|
if (! $this->isCachable) { |
118
|
|
|
return parent::min($column); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$arguments = func_get_args(); |
122
|
|
|
$cacheKey = $this->makeCacheKey(['*'], null, "-min_{$column}"); |
123
|
|
|
$method = 'min'; |
124
|
|
|
|
125
|
|
|
return $this->cachedValue($arguments, $cacheKey, $method); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function pluck($column, $key = null) |
129
|
|
|
{ |
130
|
|
|
if (! $this->isCachable) { |
131
|
|
|
return parent::pluck($column, $key); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$keyDifferentiator = "-pluck_{$column}" . ($key ? "_{$key}" : ""); |
135
|
|
|
$arguments = func_get_args(); |
136
|
|
|
$cacheKey = $this->makeCacheKey([$column], null, $keyDifferentiator); |
137
|
|
|
$method = 'pluck'; |
138
|
|
|
|
139
|
|
|
return $this->cachedValue($arguments, $cacheKey, $method); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function sum($column) |
143
|
|
|
{ |
144
|
|
|
if (! $this->isCachable) { |
145
|
|
|
return parent::sum($column); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$arguments = func_get_args(); |
149
|
|
|
$cacheKey = $this->makeCacheKey(['*'], null, "-sum_{$column}"); |
150
|
|
|
$method = 'sum'; |
151
|
|
|
|
152
|
|
|
return $this->cachedValue($arguments, $cacheKey, $method); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function value($column) |
156
|
|
|
{ |
157
|
|
|
if (! $this->isCachable) { |
158
|
|
|
return parent::value($column); |
159
|
|
|
} |
160
|
|
|
|
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) |
204
|
|
|
->rememberForever( |
205
|
|
|
$hashedCacheKey, |
206
|
|
|
function () use ($arguments, $cacheKey, $method) { |
207
|
|
|
return [ |
208
|
|
|
'key' => $cacheKey, |
209
|
|
|
'value' => parent::{$method}(...$arguments), |
210
|
|
|
]; |
211
|
|
|
} |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|