1 | <?php |
||
33 | abstract class SimpleCache extends Component implements CacheInterface |
||
34 | { |
||
35 | /** |
||
36 | * @var int default TTL for a cache entry. Default value is 0, meaning infinity. |
||
37 | * This value is used by [[set()]] and [[setMultiple()]], if the duration is not explicitly given. |
||
38 | */ |
||
39 | public $defaultTtl = 0; |
||
40 | /** |
||
41 | * @var string a string prefixed to every cache key so that it is unique globally in the whole cache storage. |
||
42 | * It is recommended that you set a unique cache key prefix for each application if the same cache |
||
43 | * storage is being used by different applications. |
||
44 | * |
||
45 | * To ensure interoperability, only alphanumeric characters should be used. |
||
46 | */ |
||
47 | public $keyPrefix = ''; |
||
48 | /** |
||
49 | * @var null|array|false the functions used to serialize and unserialize cached data. Defaults to null, meaning |
||
50 | * using the default PHP `serialize()` and `unserialize()` functions. If you want to use some more efficient |
||
51 | * serializer (e.g. [igbinary](http://pecl.php.net/package/igbinary)), you may configure this property with |
||
52 | * a two-element array. The first element specifies the serialization function, and the second the deserialization |
||
53 | * function. If this property is set false, data will be directly sent to and retrieved from the underlying |
||
54 | * cache component without any serialization or deserialization. You should not turn off serialization if |
||
55 | * you are using [[Dependency|cache dependency]], because it relies on data serialization. Also, some |
||
56 | * implementations of the cache can not correctly save and retrieve data different from a string type. |
||
57 | */ |
||
58 | public $serializer; |
||
59 | |||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 109 | public function get($key, $default = null) |
|
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | 16 | public function getMultiple($keys, $default = null) |
|
81 | { |
||
82 | 16 | $keyMap = []; |
|
83 | 16 | foreach ($keys as $key) { |
|
84 | 16 | $keyMap[$key] = $this->normalizeKey($key); |
|
85 | } |
||
86 | 16 | $values = $this->getValues(array_values($keyMap)); |
|
87 | 16 | $results = []; |
|
88 | 16 | foreach ($keyMap as $key => $newKey) { |
|
89 | 16 | $results[$key] = $default; |
|
90 | 16 | if (isset($values[$newKey]) && $values[$newKey] !== false) { |
|
91 | 16 | if ($this->serializer === false) { |
|
92 | $results[$key] = $values[$newKey]; |
||
93 | } else { |
||
94 | 16 | $results[$key] = $this->serializer === null ? unserialize($values[$newKey]) |
|
95 | 16 | : call_user_func($this->serializer[1], $values[$newKey]); |
|
96 | } |
||
97 | } |
||
98 | } |
||
99 | 16 | return $results; |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 2 | public function has($key) |
|
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | 103 | public function set($key, $value, $ttl = null) |
|
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 21 | public function setMultiple($values, $ttl = null) |
|
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | 93 | public function delete($key) |
|
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | public function deleteMultiple($keys) |
||
167 | |||
168 | /** |
||
169 | * Builds a normalized cache key from a given key. |
||
170 | * |
||
171 | * The given key will be type-casted to string. |
||
172 | * If the result string does not contain alphanumeric characters only or has more than 32 characters, |
||
173 | * then the hash of the key will be used. |
||
174 | * The result key will be returned back prefixed with [[keyPrefix]]. |
||
175 | * |
||
176 | * @param mixed $key the key to be normalized |
||
177 | * @return string the generated cache key |
||
178 | */ |
||
179 | 202 | protected function normalizeKey($key) |
|
185 | |||
186 | /** |
||
187 | * Normalizes cache TTL handling `null` value and [[\DateInterval]] objects. |
||
188 | * @param int|\DateInterval $ttl raw TTL. |
||
189 | * @return int TTL value as UNIX timestamp. |
||
190 | */ |
||
191 | 119 | protected function normalizeTtl($ttl) |
|
201 | |||
202 | /** |
||
203 | * Retrieves a value from cache with a specified key. |
||
204 | * This method should be implemented by child classes to retrieve the data |
||
205 | * from specific cache storage. |
||
206 | * @param string $key a unique key identifying the cached value |
||
207 | * @return mixed|false the value stored in cache, `false` if the value is not in the cache or expired. Most often |
||
208 | * value is a string. If you have disabled [[serializer]], it could be something else. |
||
209 | */ |
||
210 | abstract protected function getValue($key); |
||
211 | |||
212 | /** |
||
213 | * Stores a value identified by a key in cache. |
||
214 | * This method should be implemented by child classes to store the data |
||
215 | * in specific cache storage. |
||
216 | * @param string $key the key identifying the value to be cached |
||
217 | * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]], |
||
218 | * it could be something else. |
||
219 | * @param int $ttl the number of seconds in which the cached value will expire. |
||
220 | * @return bool true if the value is successfully stored into cache, false otherwise |
||
221 | */ |
||
222 | abstract protected function setValue($key, $value, $ttl); |
||
223 | |||
224 | /** |
||
225 | * Deletes a value with the specified key from cache |
||
226 | * This method should be implemented by child classes to delete the data from actual cache storage. |
||
227 | * @param string $key the key of the value to be deleted |
||
228 | * @return bool if no error happens during deletion |
||
229 | */ |
||
230 | abstract protected function deleteValue($key); |
||
231 | |||
232 | /** |
||
233 | * Retrieves multiple values from cache with the specified keys. |
||
234 | * The default implementation calls [[getValue()]] multiple times to retrieve |
||
235 | * the cached values one by one. If the underlying cache storage supports multiget, |
||
236 | * this method should be overridden to exploit that feature. |
||
237 | * @param array $keys a list of keys identifying the cached values |
||
238 | * @return array a list of cached values indexed by the keys |
||
239 | */ |
||
240 | 7 | protected function getValues($keys) |
|
251 | |||
252 | /** |
||
253 | * Stores multiple key-value pairs in cache. |
||
254 | * The default implementation calls [[setValue()]] multiple times store values one by one. If the underlying cache |
||
255 | * storage supports multi-set, this method should be overridden to exploit that feature. |
||
256 | * @param array $values array where key corresponds to cache key while value is the value stored |
||
257 | * @param int $ttl the number of seconds in which the cached values will expire. |
||
258 | * @return bool `true` on success and `false` on failure. |
||
259 | */ |
||
260 | 13 | protected function setValues($values, $ttl) |
|
270 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: