1
|
|
|
<?php namespace GeneaLabs\LaravelModelCaching; |
2
|
|
|
|
3
|
|
|
use Closure; |
4
|
|
|
use Illuminate\Cache\TaggableStore; |
5
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Pivot; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
9
|
|
|
|
10
|
|
|
class CachedBuilder extends EloquentBuilder |
11
|
|
|
{ |
12
|
|
|
protected function cache(array $tags = []) |
13
|
|
|
{ |
14
|
|
|
$cache = cache(); |
15
|
|
|
|
16
|
|
|
if (is_subclass_of($cache->getStore(), TaggableStore::class)) { |
17
|
|
|
$cache = $cache->tags($tags); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
return $cache; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
protected function getCacheKey(array $columns = ['*'], $idColumn = null) : string |
24
|
|
|
{ |
25
|
|
|
$key = $this->getModelSlug(); |
26
|
|
|
$key .= $this->getIdColumn($idColumn ?: ''); |
27
|
|
|
$key .= $this->getQueryColumns($columns); |
28
|
|
|
$key .= $this->getWhereClauses(); |
29
|
|
|
$key .= $this->getWithModels(); |
30
|
|
|
$key .= $this->getOrderClauses(); |
31
|
|
|
$key .= $this->getOffsetClause(); |
32
|
|
|
$key .= $this->getLimitClause(); |
33
|
|
|
|
34
|
|
|
return $key; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function getIdColumn(string $idColumn) : string |
38
|
|
|
{ |
39
|
|
|
|
40
|
|
|
return $idColumn ? "_{$idColumn}" : ''; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function getLimitClause() : string |
44
|
|
|
{ |
45
|
|
|
if (! $this->query->limit) { |
46
|
|
|
return ''; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return "-limit_{$this->query->limit}"; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function getModelSlug() : string |
53
|
|
|
{ |
54
|
|
|
return str_slug(get_class($this->model)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function getOffsetClause() : string |
58
|
|
|
{ |
59
|
|
|
if (! $this->query->offset) { |
60
|
|
|
return ''; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return "-offset_{$this->query->offset}"; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function getQueryColumns(array $columns) : string |
67
|
|
|
{ |
68
|
|
|
if ($columns === ['*'] || $columns === []) { |
69
|
|
|
return ''; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return '_' . implode('_', $columns); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getWhereClauses(array $wheres = []) : string |
76
|
|
|
{ |
77
|
|
|
$wheres = collect($wheres); |
78
|
|
|
|
79
|
|
|
if ($wheres->isEmpty()) { |
80
|
|
|
$wheres = collect($this->query->wheres); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $wheres->reduce(function ($carry, $where) { |
84
|
|
|
if (in_array($where['type'], ['Exists', 'Nested'])) { |
85
|
|
|
return $this->getWhereClauses($where['query']->wheres); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($where['type'] === 'Column') { |
89
|
|
|
return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}"; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if ($where['type'] === 'raw') { |
93
|
|
|
return "_{$where['boolean']}_" . str_slug($where['sql']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$value = array_get($where, 'value'); |
97
|
|
|
|
98
|
|
|
if (in_array($where['type'], ['In', 'Null', 'NotNull'])) { |
99
|
|
|
$value = strtolower($where['type']); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (is_array(array_get($where, 'values'))) { |
103
|
|
|
$value .= '_' . implode('_', $where['values']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return "{$carry}-{$where['column']}_{$value}"; |
107
|
|
|
}) ?: ''; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function getWithModels() : string |
111
|
|
|
{ |
112
|
|
|
$eagerLoads = collect($this->eagerLoad); |
113
|
|
|
|
114
|
|
|
if ($eagerLoads->isEmpty()) { |
115
|
|
|
return ''; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return '-' . implode('-', $eagerLoads->keys()->toArray()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
protected function getOrderClauses(){ |
122
|
|
|
$orders = collect($this->query->orders); |
123
|
|
|
|
124
|
|
|
return $orders->reduce(function($carry, $order){ |
125
|
|
|
$carry .= '_sort_' . array_get($order, 'column') . '_' . array_get($order, 'direction'); |
126
|
|
|
|
127
|
|
|
return $carry; |
128
|
|
|
}); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function getCacheTags() : array |
132
|
|
|
{ |
133
|
|
|
return collect($this->eagerLoad)->keys() |
134
|
|
|
->map(function ($relationName) { |
135
|
|
|
$relation = collect(explode('.', $relationName)) |
136
|
|
|
->reduce(function ($carry, $name) { |
137
|
|
|
if (! $carry) { |
138
|
|
|
$carry = $this->model; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ($carry instanceof Relation) { |
142
|
|
|
$carry = $carry->getQuery()->model; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $carry->{$name}(); |
146
|
|
|
}); |
147
|
|
|
|
148
|
|
|
return str_slug(get_class($relation->getQuery()->model)); |
149
|
|
|
}) |
150
|
|
|
->prepend(str_slug(get_class($this->model))) |
151
|
|
|
->values() |
152
|
|
|
->toArray(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
View Code Duplication |
public function avg($column) |
156
|
|
|
{ |
157
|
|
|
$tags = [str_slug(get_class($this->model))]; |
158
|
|
|
$key = str_slug(get_class($this->model)) ."-avg_{$column}"; |
159
|
|
|
|
160
|
|
|
return $this->cache($tags) |
161
|
|
|
->rememberForever($key, function () use ($column) { |
162
|
|
|
return parent::avg($column); |
163
|
|
|
}); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
View Code Duplication |
public function count($columns = ['*']) |
167
|
|
|
{ |
168
|
|
|
$tags = [str_slug(get_class($this->model))]; |
169
|
|
|
$key = str_slug(get_class($this->model)) ."-count"; |
170
|
|
|
|
171
|
|
|
return $this->cache($tags) |
172
|
|
|
->rememberForever($key, function () use ($columns) { |
173
|
|
|
return parent::count($columns); |
174
|
|
|
}); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
View Code Duplication |
public function cursor() |
178
|
|
|
{ |
179
|
|
|
$tags = [str_slug(get_class($this->model))]; |
180
|
|
|
$key = str_slug(get_class($this->model)) ."-cursor"; |
181
|
|
|
|
182
|
|
|
return $this->cache($tags) |
183
|
|
|
->rememberForever($key, function () { |
184
|
|
|
return collect(parent::cursor()); |
185
|
|
|
}); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
190
|
|
|
*/ |
191
|
|
View Code Duplication |
public function find($id, $columns = ['*']) |
192
|
|
|
{ |
193
|
|
|
$tags = $this->getCacheTags(); |
194
|
|
|
$key = $this->getCacheKey($columns, $id); |
195
|
|
|
|
196
|
|
|
return $this->cache($tags) |
197
|
|
|
->rememberForever($key, function () use ($id, $columns) { |
198
|
|
|
return parent::find($id, $columns); |
199
|
|
|
}); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
View Code Duplication |
public function first($columns = ['*']) |
203
|
|
|
{ |
204
|
|
|
$tags = $this->getCacheTags(); |
205
|
|
|
$key = $this->getCacheKey($columns) . '-first'; |
206
|
|
|
|
207
|
|
|
return $this->cache($tags) |
208
|
|
|
->rememberForever($key, function () use ($columns) { |
209
|
|
|
return parent::first($columns); |
210
|
|
|
}); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
View Code Duplication |
public function get($columns = ['*']) |
214
|
|
|
{ |
215
|
|
|
$tags = $this->getCacheTags(); |
216
|
|
|
$key = $this->getCacheKey($columns); |
217
|
|
|
|
218
|
|
|
return $this->cache($tags) |
219
|
|
|
->rememberForever($key, function () use ($columns) { |
220
|
|
|
return parent::get($columns); |
221
|
|
|
}); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
View Code Duplication |
public function max($column) |
225
|
|
|
{ |
226
|
|
|
$tags = [str_slug(get_class($this->model))]; |
227
|
|
|
$key = str_slug(get_class($this->model)) ."-max_{$column}"; |
228
|
|
|
|
229
|
|
|
return $this->cache($tags) |
230
|
|
|
->rememberForever($key, function () use ($column) { |
231
|
|
|
return parent::max($column); |
232
|
|
|
}); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
View Code Duplication |
public function min($column) |
236
|
|
|
{ |
237
|
|
|
$tags = [str_slug(get_class($this->model))]; |
238
|
|
|
$key = str_slug(get_class($this->model)) ."-min_{$column}"; |
239
|
|
|
|
240
|
|
|
return $this->cache($tags) |
241
|
|
|
->rememberForever($key, function () use ($column) { |
242
|
|
|
return parent::min($column); |
243
|
|
|
}); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function pluck($column, $key = null) |
247
|
|
|
{ |
248
|
|
|
$tags = $this->getCacheTags(); |
249
|
|
|
$cacheKey = $this->getCacheKey([$column]) . "-pluck_{$column}"; |
250
|
|
|
|
251
|
|
|
if ($key) { |
252
|
|
|
$cacheKey .= "_{$key}"; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $this->cache($tags) |
256
|
|
|
->rememberForever($cacheKey, function () use ($column, $key) { |
257
|
|
|
return parent::pluck($column, $key); |
258
|
|
|
}); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
View Code Duplication |
public function sum($column) |
262
|
|
|
{ |
263
|
|
|
$tags = [str_slug(get_class($this->model))]; |
264
|
|
|
$key = str_slug(get_class($this->model)) ."-sum_{$column}"; |
265
|
|
|
|
266
|
|
|
return $this->cache($tags) |
267
|
|
|
->rememberForever($key, function () use ($column) { |
268
|
|
|
return parent::sum($column); |
269
|
|
|
}); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|