-
Notifications
You must be signed in to change notification settings - Fork 13
/
CacheDecorator.php
233 lines (201 loc) · 5.69 KB
/
CacheDecorator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
namespace Orchestra\Database;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Support\Collection;
use Throwable;
class CacheDecorator
{
/**
* The key that should be used when caching the query.
*
* @var string
*/
protected $cacheKey;
/**
* The number of minutes to cache the query.
*
* @var int
*/
protected $cacheMinutes;
/**
* The Query Builder.
*
* @var \Illuminate\Database\Query\Builder
*/
protected $query;
/**
* The cache repository implementation.
*
* @var \Illuminate\Contracts\Cache\Repository
*/
protected $repository;
/**
* Construct a new decorator.
*
* @param \Illuminate\Database\Query\Builder $query
*/
public function __construct($query, Repository $repository)
{
$this->repository = $repository;
$this->query = $query;
}
/**
* Indicate that the query results should be cached.
*
* @param \DateTime|int $minutes
* @param string $key
*
* @return $this
*/
public function remember($minutes, ?string $key = null)
{
$this->cacheMinutes = $minutes;
$this->cacheKey = $key;
return $this;
}
/**
* Indicate that the query results should be cached forever.
*
* @param string $key
*
* @return $this
*/
public function rememberForever(?string $key = null)
{
return $this->remember(-1, $key);
}
/**
* Get an array with the values of a given column.
*/
public function pluck(string $column, ?string $key = null): Collection
{
$results = $this->get(\is_null($key) ? [$column] : [$column, $key]);
// If the columns are qualified with a table or have an alias, we cannot use
// those directly in the "pluck" operations since the results from the DB
// are only keyed by the column itself. We'll strip the table out here.
return $results->pluck(
$this->stripTableForPluck($column),
$this->stripTableForPluck($key)
);
}
/**
* Strip off the table name or alias from a column identifier.
*/
protected function stripTableForPluck(string $column): ?string
{
return \is_null($column) ? $column : \end(\preg_split('~\.| ~', $column));
}
/**
* Execute the query and get the first result.
*
* @param array $columns
*
* @return mixed|static
*/
public function first($columns = ['*'])
{
$this->query->take(1);
$results = $this->get($columns);
if ($results instanceof Collection) {
return $results->first();
}
return \count($results) > 0 ? \reset($results) : null;
}
/**
* Execute the query as a "select" statement.
*
* @param array $columns
*/
public function get($columns = ['*']): Collection
{
try {
if (! \is_null($this->cacheMinutes)) {
return $this->getCached($columns);
}
} catch (Throwable $e) {
//
}
return $this->getFresh($columns);
}
/**
* Execute the query as a cached "select" statement.
*
* @param array $columns
*/
public function getCached($columns = ['*']): Collection
{
// If the query is requested to be cached, we will cache it using a unique key
// for this database connection and query statement, including the bindings
// that are used on this query, providing great convenience when caching.
[$key, $minutes] = $this->getCacheInfo();
$cache = $this->getCache();
$callback = $this->getCacheCallback($columns);
// If the "minutes" value is less than zero, we will use that as the indicator
// that the value should be remembered values should be stored indefinitely
// and if we have minutes we will use the typical remember function here.
if ($minutes < 0) {
return Collection::make($cache->rememberForever($key, $callback));
}
return Collection::make($cache->remember($key, $minutes, $callback));
}
/**
* Execute the query as a fresh "select" statement.
*
* @param array $columns
*/
public function getFresh($columns = ['*']): Collection
{
return $this->query->get($columns);
}
/**
* Get a unique cache key for the complete query.
*/
public function getCacheKey(): string
{
return $this->cacheKey ?: $this->generateCacheKey();
}
/**
* Generate the unique cache key for the query.
*/
public function generateCacheKey(): string
{
$name = $this->getConnection()->getName();
return \md5($name.$this->toSql().\serialize($this->getBindings()));
}
/**
* Get the cache object with tags assigned, if applicable.
*/
protected function getCache(): Repository
{
return $this->repository;
}
/**
* Get the Closure callback used when caching queries.
*
* @param array $columns
*
* @return \Closure
*/
protected function getCacheCallback($columns)
{
return function () use ($columns) {
return $this->getFresh($columns)->all();
};
}
/**
* Get the cache key and cache minutes as an array.
*/
protected function getCacheInfo(): array
{
return [$this->getCacheKey(), $this->cacheMinutes];
}
/**
* Handle dynamic method calls into the method.
*
* @return mixed
*/
public function __call(string $method, array $parameters)
{
return $this->query->{$method}(...$parameters);
}
}