1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace luya\traits; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Cacheable trait allows caching whether application has caching enabled or not. |
9
|
|
|
* |
10
|
|
|
* implementation example: |
11
|
|
|
* |
12
|
|
|
* ```php |
13
|
|
|
* $cacheKey = 'foobar'; |
14
|
|
|
* |
15
|
|
|
* $cache = $this->getHasCache($cacheKey); |
16
|
|
|
* |
17
|
|
|
* if ($cache === false) { |
18
|
|
|
* |
19
|
|
|
* // execute code and save cache data into variable |
20
|
|
|
* $cache = "Hello World"; |
21
|
|
|
* |
22
|
|
|
* $this->setHasCache($cacheKey, $cache); // variable $cache has been changed from above |
23
|
|
|
* } |
24
|
|
|
* |
25
|
|
|
* return $cache; |
26
|
|
|
* ``` |
27
|
|
|
* |
28
|
|
|
* An example for a simple cache query dependency |
29
|
|
|
* |
30
|
|
|
* ```php |
31
|
|
|
* $this->setHasCache('myCacheKey', $data, new DbDependency(['sql' => 'SELECT MAX(id) FROM admin_storage_folder WHERE is_deleted=0']), 0); |
32
|
|
|
* ``` |
33
|
|
|
* |
34
|
|
|
* You can also use an array notation in order to generate cache dependency: |
35
|
|
|
* |
36
|
|
|
* ```php |
37
|
|
|
* $dependency = [ |
38
|
|
|
* 'class' => 'yii\caching\DbDependency', |
39
|
|
|
* 'sql' => 'SELECT max(timestamp) FROM table WHERE id=:id', |
40
|
|
|
* 'params' => [':id' => Yii::$app->request->get('id')], |
41
|
|
|
* ]; |
42
|
|
|
* |
43
|
|
|
* $this->setHasCache(['my', 'key'], $data, $dependency); |
44
|
|
|
* ``` |
45
|
|
|
* |
46
|
|
|
* @author Basil Suter <[email protected]> |
47
|
|
|
* @since 1.0.0 |
48
|
|
|
*/ |
49
|
|
|
trait CacheableTrait |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* @var integer Defined the duration of the caching lifetime in seconds. 3600 = 1 hour, 86400 = 24 hours. 0 is forever |
53
|
|
|
*/ |
54
|
|
|
private $_cacheExpiration = 86400; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var boolean Whether the caching is enabled or disabled. |
58
|
|
|
*/ |
59
|
|
|
private $_cachable; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Check if the current configuration of the application and the property allows a caching of the |
63
|
|
|
* language container data. |
64
|
|
|
* |
65
|
|
|
* @return boolean |
66
|
|
|
*/ |
67
|
|
|
public function isCachable() |
68
|
|
|
{ |
69
|
|
|
if ($this->_cachable === null) { |
70
|
|
|
$this->_cachable = Yii::$app->has('cache') ? true : false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->_cachable; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Method combines both [[setHasCache()]] and [[getHasCache()]] methods to retrieve value identified by a $key, |
78
|
|
|
* or to store the result of $closure execution if there is no cache available for the $key. |
79
|
|
|
* |
80
|
|
|
* Usage example: |
81
|
|
|
* |
82
|
|
|
* ```php |
83
|
|
|
* use CacheableTrait; |
84
|
|
|
* |
85
|
|
|
* public function getTopProducts($count = 10) |
86
|
|
|
* { |
87
|
|
|
* return $this->getOrSetHasCache(['top-n-products', 'n' => $count], function ($cache) use ($count) { |
88
|
|
|
* return Products::find()->mostPopular()->limit(10)->all(); |
89
|
|
|
* }, 1000); |
90
|
|
|
* } |
91
|
|
|
* ``` |
92
|
|
|
* |
93
|
|
|
* @param mixed $key a key identifying the value to be cached. This can be a simple string or |
94
|
|
|
* a complex data structure consisting of factors representing the key. |
95
|
|
|
* @param \Closure $closure the closure that will be used to generate a value to be cached. |
96
|
|
|
* In case $closure returns `false`, the value will not be cached. |
97
|
|
|
* @param int $duration default duration in seconds before the cache will expire. If not set, |
98
|
|
|
* [[defaultDuration]] value will be used. 0 means never expire. |
99
|
|
|
* @param \yii\caching\Dependency $dependency dependency of the cached item. If the dependency changes, |
100
|
|
|
* the corresponding value in the cache will be invalidated when it is fetched via [[get()]]. |
101
|
|
|
* This parameter is ignored if [[serializer]] is `false`. |
102
|
|
|
* @return mixed result of $closure execution |
103
|
|
|
*/ |
104
|
|
|
public function getOrSetHasCache($key, \Closure $closure, $duration = null, $dependency = null) |
105
|
|
|
{ |
106
|
|
|
if (($value = $this->getHasCache($key)) !== false) { |
107
|
|
|
return $value; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$value = call_user_func($closure, $this); |
111
|
|
|
|
112
|
|
|
$this->setHasCache($key, $value, $dependency, $duration); |
113
|
|
|
|
114
|
|
|
return $value; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Store cache data for a specific key if caching is enabled in this application. |
119
|
|
|
* |
120
|
|
|
* @param string|array $key The identifier key or a array to store complex keys |
121
|
|
|
* @param mixed $value The value to store in the cache component. |
122
|
|
|
* @param \yii\caching\Dependency|array $dependency Dependency of the cached item. If the dependency changes, the corresponding value in the cache will be invalidated when it is fetched |
123
|
|
|
* via get(). This parameter is ignored if $serializer is false. You can also define an array with defintion which will generate the Object instead of object is provided. |
124
|
|
|
* @param $cacheExpiration integer The time in seconds before the cache data expires, 0 means never expire. |
125
|
|
|
* @return boolean Whether set has been success or not |
126
|
|
|
*/ |
127
|
|
|
public function setHasCache($key, $value, $dependency = null, $cacheExpiration = null) |
128
|
|
|
{ |
129
|
|
|
if ($this->isCachable()) { |
130
|
|
|
if (is_array($dependency)) { |
131
|
|
|
$dependency = Yii::createObject($dependency); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return Yii::$app->cache->set($key, $value, is_null($cacheExpiration) ? $this->_cacheExpiration : $cacheExpiration, $dependency); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Remove a value from the cache if caching is enabled. |
142
|
|
|
* |
143
|
|
|
* @param string|array $key The cache identifier |
144
|
|
|
* @return boolean Whether delete of key has been success or not |
145
|
|
|
*/ |
146
|
|
|
public function deleteHasCache($key) |
147
|
|
|
{ |
148
|
|
|
if ($this->isCachable()) { |
149
|
|
|
return Yii::$app->cache->delete($key); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Get the caching data if caching is allowed and there is any data stored for this key. |
157
|
|
|
* |
158
|
|
|
* @param string|array $key The identifiere key, can be a string or an array which will be calculated. |
159
|
|
|
* @return mixed|boolean Returns the data, if not found returns false. |
160
|
|
|
*/ |
161
|
|
|
public function getHasCache($key) |
162
|
|
|
{ |
163
|
|
|
if ($this->isCachable()) { |
164
|
|
|
$data = Yii::$app->cache->get($key); |
165
|
|
|
|
166
|
|
|
$enumKey = (is_array($key)) ? implode(",", $key) : $key; |
167
|
|
|
|
168
|
|
|
if ($data) { |
169
|
|
|
Yii::debug("Cacheable trait key '$enumKey' successfully loaded from cache.", __METHOD__); |
170
|
|
|
return $data; |
171
|
|
|
} |
172
|
|
|
Yii::debug("Cacheable trait key '$enumKey' has not been found in cache.", __METHOD__); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return false; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Deletes all values from cache. |
180
|
|
|
* |
181
|
|
|
* @return boolean Whether the flush operation was successful. |
182
|
|
|
*/ |
183
|
|
|
public function flushHasCache() |
184
|
|
|
{ |
185
|
|
|
if ($this->isCachable()) { |
186
|
|
|
return Yii::$app->cache->flush(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return false; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|