Complex classes like Cache often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Cache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | abstract class Cache extends Component implements \ArrayAccess |
||
54 | { |
||
55 | /** |
||
56 | * @var string a string prefixed to every cache key so that it is unique globally in the whole cache storage. |
||
57 | * It is recommended that you set a unique cache key prefix for each application if the same cache |
||
58 | * storage is being used by different applications. |
||
59 | * |
||
60 | * To ensure interoperability, only alphanumeric characters should be used. |
||
61 | */ |
||
62 | public $keyPrefix; |
||
63 | /** |
||
64 | * @var null|array|false the functions used to serialize and unserialize cached data. Defaults to null, meaning |
||
65 | * using the default PHP `serialize()` and `unserialize()` functions. If you want to use some more efficient |
||
66 | * serializer (e.g. [igbinary](http://pecl.php.net/package/igbinary)), you may configure this property with |
||
67 | * a two-element array. The first element specifies the serialization function, and the second the deserialization |
||
68 | * function. If this property is set false, data will be directly sent to and retrieved from the underlying |
||
69 | * cache component without any serialization or deserialization. You should not turn off serialization if |
||
70 | * you are using [[Dependency|cache dependency]], because it relies on data serialization. Also, some |
||
71 | * implementations of the cache can not correctly save and retrieve data different from a string type. |
||
72 | */ |
||
73 | public $serializer; |
||
74 | /** |
||
75 | * @var integer default duration in seconds before a cache entry will expire. Default value is 0, meaning infinity. |
||
76 | * This value is used by [[set()]] if the duration is not explicitly given. |
||
77 | * @since 2.0.11 |
||
78 | */ |
||
79 | public $defaultDuration = 0; |
||
80 | |||
81 | |||
82 | /** |
||
83 | * Builds a normalized cache key from a given key. |
||
84 | * |
||
85 | * If the given key is a string containing alphanumeric characters only and no more than 32 characters, |
||
86 | * then the key will be returned back prefixed with [[keyPrefix]]. Otherwise, a normalized key |
||
87 | * is generated by serializing the given key, applying MD5 hashing, and prefixing with [[keyPrefix]]. |
||
88 | * |
||
89 | * @param mixed $key the key to be normalized |
||
90 | * @return string the generated cache key |
||
91 | */ |
||
92 | 113 | public function buildKey($key) |
|
102 | |||
103 | /** |
||
104 | * Retrieves a value from cache with a specified key. |
||
105 | * @param mixed $key a key identifying the cached value. This can be a simple string or |
||
106 | * a complex data structure consisting of factors representing the key. |
||
107 | * @return mixed the value stored in cache, false if the value is not in the cache, expired, |
||
108 | * or the dependency associated with the cached data has changed. |
||
109 | */ |
||
110 | 79 | public function get($key) |
|
127 | |||
128 | /** |
||
129 | * Checks whether a specified key exists in the cache. |
||
130 | * This can be faster than getting the value from the cache if the data is big. |
||
131 | * In case a cache does not support this feature natively, this method will try to simulate it |
||
132 | * but has no performance improvement over getting it. |
||
133 | * Note that this method does not check whether the dependency associated |
||
134 | * with the cached data, if there is any, has changed. So a call to [[get]] |
||
135 | * may return false while exists returns true. |
||
136 | * @param mixed $key a key identifying the cached value. This can be a simple string or |
||
137 | * a complex data structure consisting of factors representing the key. |
||
138 | * @return bool true if a value exists in cache, false if the value is not in the cache or expired. |
||
139 | */ |
||
140 | 2 | public function exists($key) |
|
147 | |||
148 | /** |
||
149 | * Retrieves multiple values from cache with the specified keys. |
||
150 | * Some caches (such as memcache, apc) allow retrieving multiple cached values at the same time, |
||
151 | * which may improve the performance. In case a cache does not support this feature natively, |
||
152 | * this method will try to simulate it. |
||
153 | * @param string[] $keys list of string keys identifying the cached values |
||
154 | * @return array list of cached values corresponding to the specified keys. The array |
||
155 | * is returned in terms of (key, value) pairs. |
||
156 | * If a value is not cached or expired, the corresponding array value will be false. |
||
157 | * @since 2.0.7 |
||
158 | */ |
||
159 | 13 | public function multiGet($keys) |
|
185 | |||
186 | /** |
||
187 | * Stores a value identified by a key into cache. |
||
188 | * If the cache already contains such a key, the existing value and |
||
189 | * expiration time will be replaced with the new ones, respectively. |
||
190 | * |
||
191 | * @param mixed $key a key identifying the value to be cached. This can be a simple string or |
||
192 | * a complex data structure consisting of factors representing the key. |
||
193 | * @param mixed $value the value to be cached |
||
194 | * @param int $duration default duration in seconds before the cache will expire. If not set, |
||
195 | * default [[ttl]] value is used. |
||
196 | * @param Dependency $dependency dependency of the cached item. If the dependency changes, |
||
197 | * the corresponding value in the cache will be invalidated when it is fetched via [[get()]]. |
||
198 | * This parameter is ignored if [[serializer]] is false. |
||
199 | * @return bool whether the value is successfully stored into cache |
||
200 | */ |
||
201 | 73 | public function set($key, $value, $duration = null, $dependency = null) |
|
219 | |||
220 | /** |
||
221 | * Stores multiple items in cache. Each item contains a value identified by a key. |
||
222 | * If the cache already contains such a key, the existing value and |
||
223 | * expiration time will be replaced with the new ones, respectively. |
||
224 | * |
||
225 | * @param array $items the items to be cached, as key-value pairs. |
||
226 | * @param int $duration default number of seconds in which the cached values will expire. 0 means never expire. |
||
227 | * @param Dependency $dependency dependency of the cached items. If the dependency changes, |
||
228 | * the corresponding values in the cache will be invalidated when it is fetched via [[get()]]. |
||
229 | * This parameter is ignored if [[serializer]] is false. |
||
230 | * @return array array of failed keys |
||
231 | * @since 2.0.7 |
||
232 | */ |
||
233 | 15 | public function multiSet($items, $duration = 0, $dependency = null) |
|
253 | |||
254 | /** |
||
255 | * Stores multiple items in cache. Each item contains a value identified by a key. |
||
256 | * If the cache already contains such a key, the existing value and expiration time will be preserved. |
||
257 | * |
||
258 | * @param array $items the items to be cached, as key-value pairs. |
||
259 | * @param int $duration default number of seconds in which the cached values will expire. 0 means never expire. |
||
260 | * @param Dependency $dependency dependency of the cached items. If the dependency changes, |
||
261 | * the corresponding values in the cache will be invalidated when it is fetched via [[get()]]. |
||
262 | * This parameter is ignored if [[serializer]] is false. |
||
263 | * @return array array of failed keys |
||
264 | * @since 2.0.7 |
||
265 | */ |
||
266 | 5 | public function multiAdd($items, $duration = 0, $dependency = null) |
|
286 | |||
287 | /** |
||
288 | * Stores a value identified by a key into cache if the cache does not contain this key. |
||
289 | * Nothing will be done if the cache already contains the key. |
||
290 | * @param mixed $key a key identifying the value to be cached. This can be a simple string or |
||
291 | * a complex data structure consisting of factors representing the key. |
||
292 | * @param mixed $value the value to be cached |
||
293 | * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire. |
||
294 | * @param Dependency $dependency dependency of the cached item. If the dependency changes, |
||
295 | * the corresponding value in the cache will be invalidated when it is fetched via [[get()]]. |
||
296 | * This parameter is ignored if [[serializer]] is false. |
||
297 | * @return bool whether the value is successfully stored into cache |
||
298 | */ |
||
299 | 8 | public function add($key, $value, $duration = 0, $dependency = null) |
|
313 | |||
314 | /** |
||
315 | * Deletes a value with the specified key from cache |
||
316 | * @param mixed $key a key identifying the value to be deleted from cache. This can be a simple string or |
||
317 | * a complex data structure consisting of factors representing the key. |
||
318 | * @return bool if no error happens during deletion |
||
319 | */ |
||
320 | 33 | public function delete($key) |
|
326 | |||
327 | /** |
||
328 | * Deletes all values from cache. |
||
329 | * Be careful of performing this operation if the cache is shared among multiple applications. |
||
330 | * @return bool whether the flush operation was successful. |
||
331 | */ |
||
332 | 48 | public function flush() |
|
336 | |||
337 | /** |
||
338 | * Retrieves a value from cache with a specified key. |
||
339 | * This method should be implemented by child classes to retrieve the data |
||
340 | * from specific cache storage. |
||
341 | * @param string $key a unique key identifying the cached value |
||
342 | * @return mixed|false the value stored in cache, false if the value is not in the cache or expired. Most often |
||
343 | * value is a string. If you have disabled [[serializer]], it could be something else. |
||
344 | */ |
||
345 | abstract protected function getValue($key); |
||
346 | |||
347 | /** |
||
348 | * Stores a value identified by a key in cache. |
||
349 | * This method should be implemented by child classes to store the data |
||
350 | * in specific cache storage. |
||
351 | * @param string $key the key identifying the value to be cached |
||
352 | * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]], |
||
353 | * it could be something else. |
||
354 | * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire. |
||
355 | * @return bool true if the value is successfully stored into cache, false otherwise |
||
356 | */ |
||
357 | abstract protected function setValue($key, $value, $duration); |
||
358 | |||
359 | /** |
||
360 | * Stores a value identified by a key into cache if the cache does not contain this key. |
||
361 | * This method should be implemented by child classes to store the data |
||
362 | * in specific cache storage. |
||
363 | * @param string $key the key identifying the value to be cached |
||
364 | * @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]], |
||
365 | * it could be something else. |
||
366 | * @param int $duration the number of seconds in which the cached value will expire. 0 means never expire. |
||
367 | * @return bool true if the value is successfully stored into cache, false otherwise |
||
368 | */ |
||
369 | abstract protected function addValue($key, $value, $duration); |
||
370 | |||
371 | /** |
||
372 | * Deletes a value with the specified key from cache |
||
373 | * This method should be implemented by child classes to delete the data from actual cache storage. |
||
374 | * @param string $key the key of the value to be deleted |
||
375 | * @return bool if no error happens during deletion |
||
376 | */ |
||
377 | abstract protected function deleteValue($key); |
||
378 | |||
379 | /** |
||
380 | * Deletes all values from cache. |
||
381 | * Child classes may implement this method to realize the flush operation. |
||
382 | * @return bool whether the flush operation was successful. |
||
383 | */ |
||
384 | abstract protected function flushValues(); |
||
385 | |||
386 | /** |
||
387 | * Retrieves multiple values from cache with the specified keys. |
||
388 | * The default implementation calls [[getValue()]] multiple times to retrieve |
||
389 | * the cached values one by one. If the underlying cache storage supports multiget, |
||
390 | * this method should be overridden to exploit that feature. |
||
391 | * @param array $keys a list of keys identifying the cached values |
||
392 | * @return array a list of cached values indexed by the keys |
||
393 | */ |
||
394 | 10 | protected function getValues($keys) |
|
403 | |||
404 | /** |
||
405 | * Stores multiple key-value pairs in cache. |
||
406 | * The default implementation calls [[setValue()]] multiple times store values one by one. If the underlying cache |
||
407 | * storage supports multi-set, this method should be overridden to exploit that feature. |
||
408 | * @param array $data array where key corresponds to cache key while value is the value stored |
||
409 | * @param int $duration the number of seconds in which the cached values will expire. 0 means never expire. |
||
410 | * @return array array of failed keys |
||
411 | */ |
||
412 | 13 | protected function setValues($data, $duration) |
|
423 | |||
424 | /** |
||
425 | * Adds multiple key-value pairs to cache. |
||
426 | * The default implementation calls [[addValue()]] multiple times add values one by one. If the underlying cache |
||
427 | * storage supports multi-add, this method should be overridden to exploit that feature. |
||
428 | * @param array $data array where key corresponds to cache key while value is the value stored. |
||
429 | * @param int $duration the number of seconds in which the cached values will expire. 0 means never expire. |
||
430 | * @return array array of failed keys |
||
431 | */ |
||
432 | 5 | protected function addValues($data, $duration) |
|
443 | |||
444 | /** |
||
445 | * Returns whether there is a cache entry with a specified key. |
||
446 | * This method is required by the interface [[\ArrayAccess]]. |
||
447 | * @param string $key a key identifying the cached value |
||
448 | * @return bool |
||
449 | */ |
||
450 | public function offsetExists($key) |
||
454 | |||
455 | /** |
||
456 | * Retrieves the value from cache with a specified key. |
||
457 | * This method is required by the interface [[\ArrayAccess]]. |
||
458 | * @param string $key a key identifying the cached value |
||
459 | * @return mixed the value stored in cache, false if the value is not in the cache or expired. |
||
460 | */ |
||
461 | 5 | public function offsetGet($key) |
|
465 | |||
466 | /** |
||
467 | * Stores the value identified by a key into cache. |
||
468 | * If the cache already contains such a key, the existing value will be |
||
469 | * replaced with the new ones. To add expiration and dependencies, use the [[set()]] method. |
||
470 | * This method is required by the interface [[\ArrayAccess]]. |
||
471 | * @param string $key the key identifying the value to be cached |
||
472 | * @param mixed $value the value to be cached |
||
473 | */ |
||
474 | 40 | public function offsetSet($key, $value) |
|
478 | |||
479 | /** |
||
480 | * Deletes the value with the specified key from cache |
||
481 | * This method is required by the interface [[\ArrayAccess]]. |
||
482 | * @param string $key the key of the value to be deleted |
||
483 | */ |
||
484 | public function offsetUnset($key) |
||
488 | } |
||
489 |