|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Laravel Platfourm package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Longman\Platfourm\Repository\Traits; |
|
12
|
|
|
|
|
13
|
|
|
use Illuminate\Contracts\Cache\Repository as CacheRepository; |
|
14
|
|
|
use Longman\Platfourm\Contracts\Repository\Criteria; |
|
15
|
|
|
use Longman\Platfourm\Repository\Helpers\CacheKeys; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class CacheableRepository. |
|
19
|
|
|
*/ |
|
20
|
|
|
trait CacheableRepository |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var CacheRepository |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $cacheRepository = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Set Cache Repository. |
|
29
|
|
|
* |
|
30
|
|
|
* @param CacheRepository $repository |
|
31
|
|
|
* |
|
32
|
|
|
* @return $this |
|
33
|
|
|
*/ |
|
34
|
|
|
public function setCacheRepository(CacheRepository $repository) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->cacheRepository = $repository; |
|
37
|
|
|
|
|
38
|
|
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Return instance of Cache Repository. |
|
43
|
|
|
* |
|
44
|
|
|
* @return CacheRepository |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getCacheRepository() |
|
47
|
|
|
{ |
|
48
|
|
|
|
|
49
|
|
|
if (is_null($this->cacheRepository)) { |
|
50
|
|
|
$this->cacheRepository = app(config('database.cache.repository', 'cache')); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this->cacheRepository; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Skip Cache. |
|
58
|
|
|
* |
|
59
|
|
|
* @param bool $status |
|
60
|
|
|
* |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
|
|
public function skipCache($status = true) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->cacheSkip = $status; |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
public function isSkippedCache() |
|
74
|
|
|
{ |
|
75
|
|
|
$skipped = isset($this->cacheSkip) ? $this->cacheSkip : false; |
|
76
|
|
|
$request = app('Illuminate\Http\Request'); |
|
77
|
|
|
$skipCacheParam = config('database.cache.params.skipCache', 'skipCache'); |
|
78
|
|
|
|
|
79
|
|
|
if ($request->has($skipCacheParam) && $request->get($skipCacheParam)) { |
|
80
|
|
|
$skipped = true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $skipped; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param $method |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function allowedCache($method) |
|
92
|
|
|
{ |
|
93
|
|
|
$cacheEnabled = config('database.cache.enabled', true); |
|
94
|
|
|
|
|
95
|
|
|
if (!$cacheEnabled) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$cacheOnly = isset($this->cacheOnly) ? $this->cacheOnly : config('database.cache.allowed.only', null); |
|
|
|
|
|
|
100
|
|
|
$cacheExcept = isset($this->cacheExcept) ? $this->cacheExcept : config('database.cache.allowed.except', null); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
if (is_array($cacheOnly)) { |
|
103
|
|
|
return in_array($method, $cacheOnly); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if (is_array($cacheExcept)) { |
|
107
|
|
|
return !in_array($method, $cacheExcept); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if (is_null($cacheOnly) && is_null($cacheExcept)) { |
|
111
|
|
|
return true; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Get Cache key for the method. |
|
119
|
|
|
* |
|
120
|
|
|
* @param $method |
|
121
|
|
|
* @param $args |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getCacheKey($method, $args = null) |
|
126
|
|
|
{ |
|
127
|
|
|
$request = app('Illuminate\Http\Request'); |
|
128
|
|
|
$args = serialize($args); |
|
129
|
|
|
$key = sprintf( |
|
130
|
|
|
'%s@%s-%s', |
|
131
|
|
|
get_called_class(), |
|
132
|
|
|
$method, |
|
133
|
|
|
md5($args . $request->fullUrl()) |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
|
|
CacheKeys::putKey(get_called_class(), $key); |
|
137
|
|
|
|
|
138
|
|
|
return $key; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get cache minutes. |
|
143
|
|
|
* |
|
144
|
|
|
* @return int |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getCacheMinutes() |
|
147
|
|
|
{ |
|
148
|
|
|
$cacheMinutes = isset($this->cacheMinutes) ? $this->cacheMinutes : config('database.cache.minutes', 30); |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
return $cacheMinutes; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Retrieve all data of repository. |
|
155
|
|
|
* |
|
156
|
|
|
* @param array $columns |
|
157
|
|
|
* |
|
158
|
|
|
* @return mixed |
|
159
|
|
|
*/ |
|
160
|
|
|
public function all($columns = ['*']) |
|
161
|
|
|
{ |
|
162
|
|
|
|
|
163
|
|
|
if (!$this->allowedCache('all') || $this->isSkippedCache()) { |
|
164
|
|
|
return parent::all($columns); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$key = $this->getCacheKey('all', func_get_args()); |
|
168
|
|
|
$minutes = $this->getCacheMinutes(); |
|
169
|
|
|
$value = $this->getCacheRepository()->remember($key, $minutes, function () use ($columns) { |
|
170
|
|
|
return parent::all($columns); |
|
171
|
|
|
}); |
|
172
|
|
|
|
|
173
|
|
|
return $value; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Retrieve all data of repository, paginated. |
|
178
|
|
|
* |
|
179
|
|
|
* @param null $limit |
|
180
|
|
|
* @param array $columns |
|
181
|
|
|
* |
|
182
|
|
|
* @return mixed |
|
183
|
|
|
*/ |
|
184
|
|
View Code Duplication |
public function paginate($limit = null, $columns = ['*']) |
|
|
|
|
|
|
185
|
|
|
{ |
|
186
|
|
|
|
|
187
|
|
|
if (!$this->allowedCache('paginate') || $this->isSkippedCache()) { |
|
188
|
|
|
return parent::paginate($limit, $columns); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$key = $this->getCacheKey('paginate', func_get_args()); |
|
192
|
|
|
|
|
193
|
|
|
$minutes = $this->getCacheMinutes(); |
|
194
|
|
|
$value = $this->getCacheRepository()->remember($key, $minutes, function () use ($limit, $columns) { |
|
195
|
|
|
return parent::paginate($limit, $columns); |
|
196
|
|
|
}); |
|
197
|
|
|
|
|
198
|
|
|
return $value; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Find data by id. |
|
203
|
|
|
* |
|
204
|
|
|
* @param $id |
|
205
|
|
|
* @param array $columns |
|
206
|
|
|
* |
|
207
|
|
|
* @return mixed |
|
208
|
|
|
*/ |
|
209
|
|
View Code Duplication |
public function find($id, $columns = ['*']) |
|
|
|
|
|
|
210
|
|
|
{ |
|
211
|
|
|
|
|
212
|
|
|
if (!$this->allowedCache('find') || $this->isSkippedCache()) { |
|
213
|
|
|
return parent::find($id, $columns); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
$key = $this->getCacheKey('find', func_get_args()); |
|
217
|
|
|
$minutes = $this->getCacheMinutes(); |
|
218
|
|
|
$value = $this->getCacheRepository()->remember($key, $minutes, function () use ($id, $columns) { |
|
219
|
|
|
return parent::find($id, $columns); |
|
220
|
|
|
}); |
|
221
|
|
|
|
|
222
|
|
|
return $value; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Find data by field and value. |
|
227
|
|
|
* |
|
228
|
|
|
* @param $field |
|
229
|
|
|
* @param $value |
|
230
|
|
|
* @param array $columns |
|
231
|
|
|
* |
|
232
|
|
|
* @return mixed |
|
233
|
|
|
*/ |
|
234
|
|
View Code Duplication |
public function findByField($field, $value = null, $columns = ['*']) |
|
|
|
|
|
|
235
|
|
|
{ |
|
236
|
|
|
|
|
237
|
|
|
if (!$this->allowedCache('findByField') || $this->isSkippedCache()) { |
|
238
|
|
|
return parent::findByField($field, $value, $columns); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
$key = $this->getCacheKey('findByField', func_get_args()); |
|
242
|
|
|
$minutes = $this->getCacheMinutes(); |
|
243
|
|
|
$value = $this->getCacheRepository()->remember($key, $minutes, function () use ($field, $value, $columns) { |
|
244
|
|
|
return parent::findByField($field, $value, $columns); |
|
245
|
|
|
}); |
|
246
|
|
|
|
|
247
|
|
|
return $value; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Find data by multiple fields. |
|
252
|
|
|
* |
|
253
|
|
|
* @param array $where |
|
254
|
|
|
* @param array $columns |
|
255
|
|
|
* |
|
256
|
|
|
* @return mixed |
|
257
|
|
|
*/ |
|
258
|
|
View Code Duplication |
public function findWhere(array $where, $columns = ['*']) |
|
|
|
|
|
|
259
|
|
|
{ |
|
260
|
|
|
|
|
261
|
|
|
if (!$this->allowedCache('findWhere') || $this->isSkippedCache()) { |
|
262
|
|
|
return parent::findWhere($where, $columns); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
$key = $this->getCacheKey('findWhere', func_get_args()); |
|
266
|
|
|
$minutes = $this->getCacheMinutes(); |
|
267
|
|
|
$value = $this->getCacheRepository()->remember($key, $minutes, function () use ($where, $columns) { |
|
268
|
|
|
return parent::findWhere($where, $columns); |
|
269
|
|
|
}); |
|
270
|
|
|
|
|
271
|
|
|
return $value; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Find data by Criteria. |
|
276
|
|
|
* |
|
277
|
|
|
* @param Criteria $criteria |
|
278
|
|
|
* |
|
279
|
|
|
* @return mixed |
|
280
|
|
|
*/ |
|
281
|
|
|
public function getByCriteria(Criteria $criteria) |
|
282
|
|
|
{ |
|
283
|
|
|
|
|
284
|
|
|
if (!$this->allowedCache('getByCriteria') || $this->isSkippedCache()) { |
|
285
|
|
|
return parent::getByCriteria($criteria); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
$key = $this->getCacheKey('getByCriteria', func_get_args()); |
|
289
|
|
|
$minutes = $this->getCacheMinutes(); |
|
290
|
|
|
$value = $this->getCacheRepository()->remember($key, $minutes, function () use ($criteria) { |
|
291
|
|
|
return parent::getByCriteria($criteria); |
|
292
|
|
|
}); |
|
293
|
|
|
|
|
294
|
|
|
return $value; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
} |
|
298
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: