-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryCache.php
More file actions
174 lines (153 loc) · 3.55 KB
/
QueryCache.php
File metadata and controls
174 lines (153 loc) · 3.55 KB
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
<?php
namespace SimpleSoftwareIO\Cache;
use Illuminate\Cache\TaggableStore;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Cache;
class QueryCache
{
/**
* The cache store to use.
*
* @var string
*/
protected $store;
/**
* The amount of time to store the cache.
*
* @var int
*/
protected $length = 30;
/**
* QueryCache constructor.
*
* @param string $store
* @param int $length
*/
public function __construct($store, $length)
{
$this->store = $store;
$this->length = $length;
}
/**
* Returns the status of the cache.
*
* @return bool
*/
public function enabled()
{
return $this->length === 0 ? false : true;
}
/**
* Sets the length of the cache.
*
* @param int $minutes
*/
public function length($minutes)
{
$this->length = $minutes;
}
/**
* Enables caching on the model.
*
* @param int $minutes
*/
public function enable($minutes = 30)
{
$this->length($minutes);
}
/**
* Disables the cache on the model.
*/
public function disable()
{
$this->length = 0;
}
/**
* Gets the model results.
*
* @param QueryCacheBuilder $builder
* @param array $columns
*
* @return Collection
*/
public function get(QueryCacheBuilder $builder, $columns = ['*'])
{
if (!$this->enabled()) {
return $this->performQuery($builder, $columns);
}
$key = $this->generateKey($builder, $columns);
$cache = $this->getCache($builder);
return $cache->remember($key, $this->length, function () use ($builder, $columns) {
return $this->performQuery($builder, $columns);
});
}
/**
* Gets a Cache instance.
*
* @return Cache
*/
protected function getCache(QueryCacheBuilder $builder)
{
return $this->isTaggable() ? Cache::store($this->store)->tags($this->getTag($builder)) : Cache::store($this->store);
}
/**
* Determines if the cache store support tagging.
*
* @return bool
*/
protected function isTaggable()
{
return Cache::getStore() instanceof TaggableStore;
}
/**
* Performs the query on the model.
*
* @param QueryCacheBuilder $builder
* @param array $columns
*
* @return mixed
*/
protected function performQuery(QueryCacheBuilder $builder, $columns = ['*'])
{
return call_user_func([$builder, 'parent::get'], $columns);
}
/**
* Generates the cache key.
*
* @param QueryCacheBuilder $builder
* @param array $columns
*
* @return string
*/
protected function generateKey(QueryCacheBuilder $builder, array $columns)
{
$sql = $builder->select($columns)->toSql();
$whereClause = serialize($builder->getBindings());
return sha1($sql.$whereClause);
}
/**
* Returns the tag to tag a cache.
*
* @param QueryCacheBuilder $builder
*
* @return string
*/
protected function getTag(QueryCacheBuilder $builder)
{
return $builder->from;
}
/**
* Flushes the cache for a model.
*
* @param $tag
*
* @return mixed
*/
public function flush($tag)
{
if ($this->isTaggable()) {
return Cache::tags($tag)->flush();
}
return Cache::flush();
}
}