-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRememberableEloquentEngine.php
223 lines (201 loc) · 5.52 KB
/
RememberableEloquentEngine.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
<?php
namespace App\Datatables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
use Yajra\Datatables\Request;
use Yajra\Datatables\Engines\EloquentEngine;
use Illuminate\Support\Facades\Log;
/**
* Class EloquentEngine.
* S'inspirer de https://github.com/dwightwatson/rememberable/blob/master/src/Query/Builder.php
* EloquentEngine hérite de QueryBuilderEngine
* $this->query = Query/Builder.php
* @package Yajra\Datatables\Engines
* @author Arjay Angeles <[email protected]>
*/
class RememberableEloquentEngine extends EloquentEngine
{
/**
* The number of minutes to cache the query.
*
* @var int
*/
protected $cacheMinutes = null;
/**
* The key that should be used when caching the query.
*
* @var string
*/
protected $cacheKey = null;
/**
* A cache prefix.
*
* @var string
*/
protected $cachePrefix = 'rememberabledatatables';
/**
* The tags for the query cache.
*
* @var array
*/
protected $cacheTags;
/**
* The cache driver to be used.
*
* @var string
*/
protected $cacheDriver;
/**
* Get results
* OVERRIDDEN FROM QueryBuilderEngine
*
* @return array|static[]
*/
public function results()
{
if ( ! is_null($this->cacheMinutes)) {
return $this->getCached();
}
return $this->results ?: $this->results = $this->query->get();
}
/**
* Execute the query as a cached "select" statement.
*
* @param array $columns
* @return array
*/
public function getCached($columns = ['*'])
{
// if (is_null($this->columns)) {
// $this->columns = $columns;
// }
// 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.
list($key, $minutes) = $this->getCacheInfo();
$cache = $this->getCache();
$callback = $this->getCacheCallback($columns);
Log::debug('getcachecallback '.var_export($callback, true));
// If we've been given a DateTime instance or a "minutes" value that is
// greater than zero then we'll pass it on to the remember method.
// Otherwise we'll cache it indefinitely.
if ($minutes instanceof DateTime || $minutes > 0) {
return $cache->remember($key, $minutes, $callback);
}
return $cache->rememberForever($key, $callback);
}
/**
* Get the Closure callback used when caching queries.
*
* @param array $columns
* @return \Closure
*/
protected function getCacheCallback($columns)
{
return function () use ($columns) {
$this->cacheMinutes = null;
Log::debug('getCacheCallback Closure called ');
return $this->query->get($columns);
};
}
/**
* Indicate that the query results should be cached.
*
* @param \DateTime|int $minutes
* @param string $key
* @return $this
*/
public function remember($minutes, $key = null)
{
list($this->cacheMinutes, $this->cacheKey) = [$minutes, $key];
return $this;
}
/**
* Indicate that the results, if cached, should use the given cache tags.
*
* @param array|mixed $cacheTags
* @return $this
*/
public function cacheTags($cacheTags)
{
$this->cacheTags = $cacheTags;
return $this;
}
/**
* Generate the unique cache key for the query.
*
* @return string
*/
public function generateCacheKey()
{
$name = $this->connection->getName();
return hash('sha256', $name.$this->query->toSql().serialize($this->query->getBindings()));
}
/**
* Flush the cache for the current model or a given tag name
*
* @param mixed $cacheTags
* @return boolean
*/
public function flushCache($cacheTags = null)
{
$store = app('cache')->getStore();
if ( ! method_exists($store, 'tags')) {
return false;
}
$cacheTags = $cacheTags ?: $this->cacheTags;
$store->tags($cacheTags)->flush();
return true;
}
/**
* Indicate that the results, if cached, should use the given cache driver.
*
* @param string $cacheDriver
* @return $this
*/
public function cacheDriver($cacheDriver)
{
$this->cacheDriver = $cacheDriver;
return $this;
}
/**
* Get the cache object with tags assigned, if applicable.
*
* @return \Illuminate\Cache\CacheManager
*/
protected function getCache()
{
$cache = app('cache')->driver($this->cacheDriver);
return $this->cacheTags ? $cache->tags($this->cacheTags) : $cache;
}
/**
* Get the cache key and cache minutes as an array.
*
* @return array
*/
protected function getCacheInfo()
{
return [$this->getCacheKey(), $this->cacheMinutes];
}
/**
* Get a unique cache key for the complete query.
*
* @return string
*/
public function getCacheKey()
{
return $this->cachePrefix.':'.($this->cacheKey ?: $this->generateCacheKey());
}
/**
* Set the cache prefix.
*
* @param string $prefix
*
* @return $this
*/
public function prefix($prefix)
{
$this->cachePrefix = $prefix;
return $this;
}
}