1 | <?php |
||
34 | class CacheBehavior extends Behavior |
||
35 | { |
||
36 | /** |
||
37 | * The cache component to use. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | public $cache = 'cache'; |
||
42 | |||
43 | /** |
||
44 | * A backup cache component to use. |
||
45 | * For example if your main cache is MemCache then you may want to use |
||
46 | * FileCache or DbCache as a backup cache storage. |
||
47 | * |
||
48 | * @var bool |
||
49 | */ |
||
50 | public $backupCache = false; |
||
51 | |||
52 | /** |
||
53 | * The relations to clear cache when this models cache is cleared. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | public $cacheRelations = []; |
||
58 | |||
59 | /** |
||
60 | * Unique md5 for this model. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $_cacheKeyPrefixName; |
||
65 | |||
66 | /** |
||
67 | * Keys that have already been cleared. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $_cacheKeysCleared = []; |
||
72 | |||
73 | /** |
||
74 | * @inheritdoc |
||
75 | */ |
||
76 | public function events() |
||
84 | |||
85 | /** |
||
86 | * Event to clear cache. |
||
87 | * |
||
88 | * @param Event $event |
||
89 | */ |
||
90 | public function clearCacheEvent($event) |
||
94 | |||
95 | /** |
||
96 | * Get a cached value. |
||
97 | * |
||
98 | * @param string $key |
||
99 | * @param bool $useBackupCache |
||
100 | * @return mixed |
||
101 | */ |
||
102 | public function getCache($key, $useBackupCache = false) |
||
114 | |||
115 | /** |
||
116 | * Set a cached value. |
||
117 | * |
||
118 | * @param string $key |
||
119 | * @param mixed $value |
||
120 | * @param bool $useBackupCache |
||
121 | * @return mixed |
||
122 | */ |
||
123 | public function setCache($key, $value, $useBackupCache = false) |
||
132 | |||
133 | /** |
||
134 | * Clear cache for model and relations. |
||
135 | * |
||
136 | * @param bool $resetCleared |
||
137 | */ |
||
138 | public function clearCache($resetCleared = true) |
||
139 | { |
||
140 | // check for recursion |
||
141 | if ($resetCleared) { |
||
142 | $this->_cacheKeysCleared = []; |
||
143 | } |
||
144 | if (isset($this->_cacheKeysCleared[$this->cacheKeyPrefixName])) { |
||
145 | return; |
||
146 | } |
||
147 | $this->_cacheKeysCleared[$this->cacheKeyPrefixName] = true; |
||
148 | // clear related cache |
||
149 | foreach ($this->cacheRelations as $cacheRelation) { |
||
150 | $models = is_array($this->owner->$cacheRelation) ? $this->owner->$cacheRelation : [$this->owner->$cacheRelation]; |
||
151 | foreach ($models as $cacheRelationModel) { |
||
152 | if ($cacheRelationModel instanceof BaseActiveRecord) { |
||
153 | $cacheRelationModel->clearCache(false); |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | // clear own cache |
||
158 | $this->clearCacheKeyPrefix(); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Get the cache prefix. |
||
163 | * |
||
164 | * @return bool|string |
||
165 | */ |
||
166 | public function getCacheKeyPrefix() |
||
167 | { |
||
168 | $cacheKeyPrefix = Yii::$app->{$this->cache}->get($this->cacheKeyPrefixName); |
||
169 | if (!$cacheKeyPrefix && $this->backupCache) { |
||
170 | $cacheKeyPrefix = Yii::$app->{$this->backupCache}->get($this->cacheKeyPrefixName); |
||
171 | } |
||
172 | if (!$cacheKeyPrefix) { |
||
173 | $cacheKeyPrefix = $this->setCacheKeyPrefix(); |
||
174 | } |
||
175 | return $cacheKeyPrefix; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Set the cache prefix. |
||
180 | * |
||
181 | * @param null|string $cacheKeyPrefix |
||
182 | */ |
||
183 | public function setCacheKeyPrefix($cacheKeyPrefix = null) |
||
184 | { |
||
185 | if (!$cacheKeyPrefix) { |
||
186 | $cacheKeyPrefix = md5($this->cacheKeyPrefixName . '.' . bin2hex(openssl_random_pseudo_bytes(10))); |
||
187 | } |
||
188 | Yii::$app->{$this->cache}->set($this->cacheKeyPrefixName, $cacheKeyPrefix); |
||
189 | if ($this->backupCache) { |
||
190 | Yii::$app->{$this->backupCache}->set($this->cacheKeyPrefixName, $cacheKeyPrefix); |
||
191 | } |
||
192 | return $cacheKeyPrefix; |
||
193 | } |
||
194 | |||
195 | |||
196 | /** |
||
197 | * Clear the cache prefix. |
||
198 | * |
||
199 | * @param null|string $cacheKeyPrefix |
||
200 | */ |
||
201 | protected function clearCacheKeyPrefix() |
||
202 | { |
||
203 | Yii::$app->{$this->cache}->delete($this->cacheKeyPrefixName); |
||
204 | if ($this->backupCache) { |
||
205 | Yii::$app->{$this->backupCache}->delete($this->cacheKeyPrefixName); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Get the cache prefix name. |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | public function getCacheKeyPrefixName() |
||
223 | |||
224 | } |
||
225 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.