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 |
||
15 | class Cache { |
||
16 | |||
17 | /** |
||
18 | * Cache storage object |
||
19 | * @var StorageInterface |
||
20 | */ |
||
21 | protected $storage; |
||
22 | |||
23 | /** |
||
24 | * Caching is enabled |
||
25 | * @var bool |
||
26 | */ |
||
27 | protected $enabled = true; |
||
28 | |||
29 | /** |
||
30 | * Key name encryption |
||
31 | * @var bool |
||
32 | */ |
||
33 | protected $encryptKeys = true; |
||
34 | |||
35 | /** |
||
36 | * Cache values information |
||
37 | * @var Info |
||
38 | */ |
||
39 | protected $info; |
||
40 | |||
41 | /** |
||
42 | * Temp info storage |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $temp; |
||
46 | |||
47 | /** |
||
48 | * Read key names |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $readKeys = array(); |
||
52 | |||
53 | /** |
||
54 | * System reserved info key |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $infoKey = '_system.info'; |
||
58 | |||
59 | /** |
||
60 | * Initialised (-1: not yet, 0: in progress, 1: initialised) |
||
61 | * @var int |
||
62 | */ |
||
63 | protected $initialised = -1; |
||
64 | |||
65 | const STORE_METHOD_SERIALIZE = 1; |
||
66 | const STORE_METHOD_JSON = 2; |
||
67 | |||
68 | /** |
||
69 | * Constructor |
||
70 | * |
||
71 | * @param StorageInterface $storage |
||
72 | * @param array $options |
||
73 | */ |
||
74 | public function __construct(StorageInterface $storage, array $options = array()) { |
||
80 | |||
81 | /** |
||
82 | * Initialise (lazy) |
||
83 | */ |
||
84 | public function init() { |
||
100 | |||
101 | /** |
||
102 | * Handles cached value expiration |
||
103 | * |
||
104 | * @param array $data |
||
105 | * @param string $key |
||
106 | * |
||
107 | * @return boolean |
||
108 | */ |
||
109 | protected function handleExpiration($data, $key) { |
||
118 | |||
119 | /** |
||
120 | * Gets Cache info |
||
121 | * |
||
122 | * @param $name Cache key |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | public function getInfo($name = '') { |
||
134 | |||
135 | /** |
||
136 | * Check if Cache is enabled |
||
137 | * |
||
138 | * @return bool |
||
139 | */ |
||
140 | public function isEnabled() { |
||
143 | |||
144 | /** |
||
145 | * Enable/disable caching |
||
146 | * |
||
147 | * @param bool $enabled |
||
148 | */ |
||
149 | public function setEnabled($enabled) { |
||
152 | |||
153 | /** |
||
154 | * Checks if the specified name in cache exists |
||
155 | * |
||
156 | * @param string $name cache name |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | public function has($name) { |
||
169 | |||
170 | /** |
||
171 | * Deletes the specified cache or each one if '' given |
||
172 | * |
||
173 | * @param string $name cache name |
||
174 | * |
||
175 | * @return bool |
||
176 | */ |
||
177 | public function delete($name = '') { |
||
194 | |||
195 | /** |
||
196 | * Flush all from cache |
||
197 | * |
||
198 | * @return bool |
||
199 | */ |
||
200 | public function flush() { |
||
203 | |||
204 | /** |
||
205 | * Stores the variable to the $name cache |
||
206 | * |
||
207 | * @param string $name cache name |
||
208 | * @param mixed $val variable to be stored |
||
209 | * @param bool $compressed Compressed storage |
||
210 | * @param int|string $expiry Expires in the given seconds (0:never) or the time defined by valid date string (eg. '2014-10-01' or '1week' or '2hours') |
||
211 | * @param string $storeMethod Storing method (serialize|json) |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | public function store($name, $val, $compressed = false, $expiry = 0, $storeMethod = self::STORE_METHOD_SERIALIZE) { |
||
242 | |||
243 | /** |
||
244 | * Extracts expiry by string |
||
245 | * |
||
246 | * @param mixed $expiry |
||
247 | * |
||
248 | * @return int |
||
249 | */ |
||
250 | protected function extractExpiryDate($expiry) { |
||
266 | |||
267 | /** |
||
268 | * Retrieves the content of $name cache |
||
269 | * |
||
270 | * @param string $name cache name |
||
271 | * @param mixed $default |
||
272 | * |
||
273 | * @return mixed |
||
274 | */ |
||
275 | public function get($name, $default = null) { |
||
297 | |||
298 | /** |
||
299 | * Extract cached value parameters |
||
300 | * |
||
301 | * @param string $name |
||
302 | * |
||
303 | * @return array |
||
304 | */ |
||
305 | protected function extractParameters($name) { |
||
310 | |||
311 | /** |
||
312 | * Attempts to get a value and if not exists store the given default variable |
||
313 | * |
||
314 | * @param string $name cache name |
||
315 | * @param mixed $default default value |
||
316 | * @param bool $compressed Compressed storage |
||
317 | * @param int|string $expiry Expires in the given seconds (0:never) or the time defined by valid date string (eg. '2014-10-01' or '1week' or '2hours') |
||
318 | * @param int $storeMethod Storing method (serialize|json) |
||
319 | * |
||
320 | * @return mixed |
||
321 | */ |
||
322 | public function getOrStore($name, $default, $compressed = false, $expiry = 0, $storeMethod = self::STORE_METHOD_SERIALIZE) { |
||
330 | |||
331 | /** |
||
332 | * Retrieves and deletes value from cache |
||
333 | * |
||
334 | * @param string $name |
||
335 | * |
||
336 | * @return mixed |
||
337 | */ |
||
338 | public function pull($name) { |
||
343 | |||
344 | /** |
||
345 | * Retrieves information of Cache state |
||
346 | * |
||
347 | * @param bool $getFields |
||
348 | * |
||
349 | * @return array|bool |
||
350 | */ |
||
351 | public function info($getFields = false) { |
||
358 | |||
359 | /** |
||
360 | * Encodes variable with the specified method |
||
361 | * |
||
362 | * @param mixed $var Variable |
||
363 | * @param int $storeMethod serialize|json |
||
364 | * |
||
365 | * @return mixed |
||
366 | */ |
||
367 | protected function encode($var, $storeMethod = self::STORE_METHOD_SERIALIZE) { |
||
378 | |||
379 | /** |
||
380 | * Decodes variable with the specified method |
||
381 | * |
||
382 | * @param mixed $var Variable |
||
383 | * @param int $storeMethod serialize|json |
||
384 | * |
||
385 | * @return mixed |
||
386 | */ |
||
387 | protected function decode($var, $storeMethod = self::STORE_METHOD_SERIALIZE) { |
||
403 | |||
404 | /** |
||
405 | * Encrypts key |
||
406 | * |
||
407 | * @param string $key |
||
408 | * |
||
409 | * @return string |
||
410 | */ |
||
411 | protected function encryptKey($key) { |
||
414 | |||
415 | /** |
||
416 | * Gets cache hits |
||
417 | * |
||
418 | * @return int |
||
419 | */ |
||
420 | public function getHits() { |
||
427 | |||
428 | /** |
||
429 | * Gets cache misses |
||
430 | * |
||
431 | * @return int |
||
432 | */ |
||
433 | public function getMisses() { |
||
440 | |||
441 | /** |
||
442 | * Stores cache values expiral information into cache |
||
443 | */ |
||
444 | public function writeExpirals() { |
||
450 | |||
451 | /** |
||
452 | * Modifies expiry by setting Time To Live |
||
453 | * |
||
454 | * @param string $name |
||
455 | * @param int $ttl |
||
456 | */ |
||
457 | public function setTTL($name, $ttl) { |
||
464 | |||
465 | /** |
||
466 | * Modifies expiry |
||
467 | * |
||
468 | * @param string $name |
||
469 | * @param mixed $expiry |
||
470 | */ |
||
471 | public function setExpiry($name, $expiry) { |
||
476 | |||
477 | /** |
||
478 | * Gets all cache key names |
||
479 | * |
||
480 | * @return array |
||
481 | */ |
||
482 | public function getKeys() { |
||
489 | |||
490 | /** |
||
491 | * Gets cache key names which already read |
||
492 | * |
||
493 | * @return array |
||
494 | */ |
||
495 | public function getReadKeys() { |
||
498 | |||
499 | /** |
||
500 | * Gets storage object |
||
501 | * |
||
502 | * @return StorageInterface |
||
503 | */ |
||
504 | public function getStorage() { |
||
507 | |||
508 | /** |
||
509 | * Retrieves key encryption |
||
510 | * |
||
511 | * @return bool |
||
512 | */ |
||
513 | public function getEncryptKeys() { |
||
516 | |||
517 | /** |
||
518 | * Sets key encryption |
||
519 | * |
||
520 | * @param bool $encryptKeys |
||
521 | */ |
||
522 | public function setEncryptKeys($encryptKeys) { |
||
525 | |||
526 | /** |
||
527 | * Sets cache storage |
||
528 | * |
||
529 | * @param StorageInterface $storage |
||
530 | */ |
||
531 | public function setStorage(StorageInterface $storage) { |
||
534 | |||
535 | /** |
||
536 | * Destructor |
||
537 | */ |
||
538 | public function __destruct() { |
||
541 | |||
542 | /** |
||
543 | * Sets a tagged cache value |
||
544 | * |
||
545 | * @param string $name cache name |
||
546 | * @param mixed $val variable to be stored |
||
547 | * @param array $tags tags |
||
548 | * @param bool $compressed Compressed storage |
||
549 | * @param int|string $expiry Expires in the given seconds (0:never) or the time defined by valid date string (eg. '2014-10-01' or '1week' or '2hours') |
||
550 | * @param int $storeMethod Storing method (serialize|json) |
||
551 | * |
||
552 | * @return bool |
||
553 | */ |
||
554 | public function storeTagged($name, $val, $tags, $compressed = false, $expiry = 0, $storeMethod = self::STORE_METHOD_SERIALIZE) { |
||
561 | |||
562 | /** |
||
563 | * Gets tagged cache values |
||
564 | * |
||
565 | * @param array $tags |
||
566 | * |
||
567 | * @return array |
||
568 | */ |
||
569 | public function getTagged($tags) { |
||
583 | |||
584 | /** |
||
585 | * Gets tags of a cached variable |
||
586 | * |
||
587 | * @param string $key |
||
588 | * |
||
589 | * @return array |
||
590 | */ |
||
591 | public function getTags($key) { |
||
601 | |||
602 | /** |
||
603 | * Sets tags of a cached variable |
||
604 | * |
||
605 | * @param string $name |
||
606 | * @param array $tags |
||
607 | * |
||
608 | * @return array |
||
609 | */ |
||
610 | public function setTags($name, $tags) { |
||
617 | |||
618 | /** |
||
619 | * Adds tags for a cached variable |
||
620 | * |
||
621 | * @param string $name |
||
622 | * @param array $tags |
||
623 | * |
||
624 | * @return array |
||
625 | */ |
||
626 | public function addTags($name, $tags) { |
||
634 | |||
635 | /** |
||
636 | * Deletes cache values matching the given tags |
||
637 | * |
||
638 | * @param array $tags |
||
639 | * |
||
640 | * @return array |
||
641 | */ |
||
642 | public function deleteTagged($tags) { |
||
652 | |||
653 | /** |
||
654 | * Gets all tags currently in use |
||
655 | * |
||
656 | * @return array |
||
657 | */ |
||
658 | public function getAllTags() { |
||
671 | |||
672 | /** |
||
673 | * Prepares tags parameter |
||
674 | * |
||
675 | * @param array|string $tags |
||
676 | */ |
||
677 | protected function prepareTags(&$tags) { |
||
683 | |||
684 | /** |
||
685 | * Checks if cache value info can be modified (cache is enabled and value exists) |
||
686 | * |
||
687 | * @param string $name |
||
688 | * |
||
689 | * @return boolean |
||
690 | */ |
||
691 | protected function canModify($name) { |
||
698 | |||
699 | /** |
||
700 | * Processes default value |
||
701 | * |
||
702 | * @param \Closure|mixed $default |
||
703 | * |
||
704 | * @return mixed |
||
705 | */ |
||
706 | protected function processDefault($default) { |
||
709 | |||
710 | /** |
||
711 | * Gets created (first write) time of a cached value |
||
712 | * |
||
713 | * @param string $name Cache name |
||
714 | * @param string $format Date format |
||
715 | * |
||
716 | * @return string |
||
717 | */ |
||
718 | public function getCreated($name, $format = 'U') { |
||
721 | |||
722 | /** |
||
723 | * Gets last access (either read or write) time of a cached value |
||
724 | * |
||
725 | * @param string $name Cache name |
||
726 | * @param string $format Date format |
||
727 | * |
||
728 | * @return string |
||
729 | */ |
||
730 | public function getLastAccess($name, $format = 'U') { |
||
733 | |||
734 | /** |
||
735 | * Gets last read time of a cached value |
||
736 | * |
||
737 | * @param string $name Cache name |
||
738 | * @param string $format Date format |
||
739 | * |
||
740 | * @return string |
||
741 | */ |
||
742 | public function getLastRead($name, $format = 'U') { |
||
745 | |||
746 | /** |
||
747 | * Gets last write time of a cached value |
||
748 | * |
||
749 | * @param string $name Cache name |
||
750 | * @param string $format Date format |
||
751 | * |
||
752 | * @return string |
||
753 | */ |
||
754 | public function getLastWrite($name, $format = 'U') { |
||
757 | |||
758 | /** |
||
759 | * Gets read count of a cached value |
||
760 | * |
||
761 | * @param string $name Cache name |
||
762 | * |
||
763 | * @return int |
||
764 | */ |
||
765 | public function getReadCount($name) { |
||
768 | |||
769 | /** |
||
770 | * Gets write count of a cached value |
||
771 | * |
||
772 | * @param string $name Cache name |
||
773 | * |
||
774 | * @return int |
||
775 | */ |
||
776 | public function getWriteCount($name) { |
||
779 | |||
780 | /** |
||
781 | * Gets expiry information of a cached value (0: never) |
||
782 | * |
||
783 | * @param string $name Cache name |
||
784 | * @param string $format Date format |
||
785 | * |
||
786 | * @return string |
||
787 | */ |
||
788 | public function getExpiry($name, $format = 'U') { |
||
795 | |||
796 | /** |
||
797 | * Calculates Time To Live |
||
798 | * |
||
799 | * @param string $name |
||
800 | * |
||
801 | * @return int |
||
802 | */ |
||
803 | public function getTTL($name) { |
||
807 | |||
808 | } |
||
809 |