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 | * Read key names |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $readKeys = array(); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * System reserved info key |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $infoKey = '_system.info'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Initialised (-1: not yet, 0: in progress, 1: initialised) |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | protected $initialised = -1; |
||
| 58 | |||
| 59 | const STORE_METHOD_SERIALIZE = 1; |
||
| 60 | const STORE_METHOD_JSON = 2; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Constructor |
||
| 64 | * |
||
| 65 | * @param StorageInterface $storage |
||
| 66 | * @param array $options |
||
| 67 | */ |
||
| 68 | public function __construct(StorageInterface $storage, array $options = array()) { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Initialise (lazy) |
||
| 77 | */ |
||
| 78 | public function init() { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Handles cached value expiration |
||
| 98 | * |
||
| 99 | * @param array $data |
||
| 100 | * @param string $key |
||
| 101 | * |
||
| 102 | * @return boolean |
||
| 103 | */ |
||
| 104 | protected function handleExpiration($data, $key) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Gets Cache info |
||
| 116 | * |
||
| 117 | * @param $name Cache key |
||
| 118 | * |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | public function getInfo($name = '') { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Check if Cache is enabled |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | public function isEnabled() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Enable/disable caching |
||
| 141 | * |
||
| 142 | * @param bool $enabled |
||
| 143 | */ |
||
| 144 | public function setEnabled($enabled) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Checks if the specified name in cache exists |
||
| 150 | * |
||
| 151 | * @param string $name cache name |
||
| 152 | * |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | public function has($name) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Deletes the specified cache or each one if '' given |
||
| 167 | * |
||
| 168 | * @param string $name cache name |
||
| 169 | * |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | public function delete($name = '') { |
||
| 173 | if (!$this->isEnabled()) { |
||
| 174 | return false; |
||
| 175 | } |
||
| 176 | |||
| 177 | $this->init(); |
||
| 178 | $realKey = $this->encryptKey($name); |
||
| 179 | $success = $this->storage->delete($realKey); |
||
| 180 | |||
| 181 | if ($name == '') { |
||
| 182 | $this->info->setData(array()); |
||
| 183 | } elseif (isset($this->info[$name])) { |
||
| 184 | unset($this->info[$name]); |
||
| 185 | } |
||
| 186 | |||
| 187 | return $success; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Flush all from cache |
||
| 192 | * |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | public function flush() { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Stores the variable to the $name cache |
||
| 201 | * |
||
| 202 | * @param string $name cache name |
||
| 203 | * @param mixed $val variable to be stored |
||
| 204 | * @param bool $compressed Compressed storage |
||
| 205 | * @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') |
||
| 206 | * @param string $storeMethod Storing method (serialize|json) |
||
| 207 | * |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public function store($name, $val, $compressed = false, $expiry = 0, $storeMethod = self::STORE_METHOD_SERIALIZE) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Extracts expiry by string |
||
| 240 | * |
||
| 241 | * @param mixed $expiry |
||
| 242 | * |
||
| 243 | * @return int |
||
| 244 | */ |
||
| 245 | protected function extractExpiryDate($expiry) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Retrieves the content of $name cache |
||
| 264 | * |
||
| 265 | * @param string $name cache name |
||
| 266 | * @param mixed $default |
||
| 267 | * |
||
| 268 | * @return mixed |
||
| 269 | */ |
||
| 270 | public function get($name, $default = null) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Extract cached value parameters |
||
| 295 | * |
||
| 296 | * @param string $name |
||
| 297 | * |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | protected function extractParameters($name) { |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Attempts to get a value and if not exists store the given default variable |
||
| 308 | * |
||
| 309 | * @param string $name cache name |
||
| 310 | * @param mixed $default default value |
||
| 311 | * @param bool $compressed Compressed storage |
||
| 312 | * @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') |
||
| 313 | * @param int $storeMethod Storing method (serialize|json) |
||
| 314 | * |
||
| 315 | * @return mixed |
||
| 316 | */ |
||
| 317 | public function getOrStore($name, $default, $compressed = false, $expiry = 0, $storeMethod = self::STORE_METHOD_SERIALIZE) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Retrieves and deletes value from cache |
||
| 328 | * |
||
| 329 | * @param string $name |
||
| 330 | * |
||
| 331 | * @return mixed |
||
| 332 | */ |
||
| 333 | public function pull($name) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Retrieves information of Cache state |
||
| 341 | * |
||
| 342 | * @param bool $getFields |
||
| 343 | * |
||
| 344 | * @return array|bool |
||
| 345 | */ |
||
| 346 | public function info($getFields = false) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Encodes variable with the specified method |
||
| 356 | * |
||
| 357 | * @param mixed $var Variable |
||
| 358 | * @param int $storeMethod serialize|json |
||
| 359 | * |
||
| 360 | * @return mixed |
||
| 361 | */ |
||
| 362 | protected function encode($var, $storeMethod = self::STORE_METHOD_SERIALIZE) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Decodes variable with the specified method |
||
| 376 | * |
||
| 377 | * @param mixed $var Variable |
||
| 378 | * @param int $storeMethod serialize|json |
||
| 379 | * |
||
| 380 | * @return mixed |
||
| 381 | */ |
||
| 382 | protected function decode($var, $storeMethod = self::STORE_METHOD_SERIALIZE) { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Encrypts key |
||
| 401 | * |
||
| 402 | * @param string $key |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | protected function encryptKey($key) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Gets cache hits |
||
| 412 | * |
||
| 413 | * @return int |
||
| 414 | */ |
||
| 415 | public function getHits() { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Gets cache misses |
||
| 425 | * |
||
| 426 | * @return int |
||
| 427 | */ |
||
| 428 | public function getMisses() { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Stores cache values expiral information into cache |
||
| 438 | */ |
||
| 439 | public function writeExpirals() { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Modifies expiry by setting Time To Live |
||
| 448 | * |
||
| 449 | * @param string $name |
||
| 450 | * @param int $ttl |
||
| 451 | */ |
||
| 452 | public function setTTL($name, $ttl) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Modifies expiry |
||
| 462 | * |
||
| 463 | * @param string $name |
||
| 464 | * @param mixed $expiry |
||
| 465 | */ |
||
| 466 | public function setExpiry($name, $expiry) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Gets all cache key names |
||
| 474 | * |
||
| 475 | * @return array |
||
| 476 | */ |
||
| 477 | public function getKeys() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Gets cache key names which already read |
||
| 487 | * |
||
| 488 | * @return array |
||
| 489 | */ |
||
| 490 | public function getReadKeys() { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Gets storage object |
||
| 496 | * |
||
| 497 | * @return StorageInterface |
||
| 498 | */ |
||
| 499 | public function getStorage() { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Retrieves key encryption |
||
| 505 | * |
||
| 506 | * @return bool |
||
| 507 | */ |
||
| 508 | public function getEncryptKeys() { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Sets key encryption |
||
| 514 | * |
||
| 515 | * @param bool $encryptKeys |
||
| 516 | */ |
||
| 517 | public function setEncryptKeys($encryptKeys) { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Sets cache storage |
||
| 523 | * |
||
| 524 | * @param StorageInterface $storage |
||
| 525 | */ |
||
| 526 | public function setStorage(StorageInterface $storage) { |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Destructor |
||
| 532 | */ |
||
| 533 | public function __destruct() { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Sets a tagged cache value |
||
| 539 | * |
||
| 540 | * @param string $name cache name |
||
| 541 | * @param mixed $val variable to be stored |
||
| 542 | * @param array $tags tags |
||
| 543 | * @param bool $compressed Compressed storage |
||
| 544 | * @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') |
||
| 545 | * @param int $storeMethod Storing method (serialize|json) |
||
| 546 | * |
||
| 547 | * @return bool |
||
| 548 | */ |
||
| 549 | public function storeTagged($name, $val, $tags, $compressed = false, $expiry = 0, $storeMethod = self::STORE_METHOD_SERIALIZE) { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Gets tagged cache values |
||
| 559 | * |
||
| 560 | * @param array $tags |
||
| 561 | * |
||
| 562 | * @return array |
||
| 563 | */ |
||
| 564 | public function getTagged($tags) { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Gets tags of a cached variable |
||
| 581 | * |
||
| 582 | * @param string $key |
||
| 583 | * |
||
| 584 | * @return array |
||
| 585 | */ |
||
| 586 | public function getTags($key) { |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Sets tags of a cached variable |
||
| 599 | * |
||
| 600 | * @param string $name |
||
| 601 | * @param array $tags |
||
| 602 | * |
||
| 603 | * @return array |
||
| 604 | */ |
||
| 605 | public function setTags($name, $tags) { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Adds tags for a cached variable |
||
| 615 | * |
||
| 616 | * @param string $name |
||
| 617 | * @param array $tags |
||
| 618 | * |
||
| 619 | * @return array |
||
| 620 | */ |
||
| 621 | public function addTags($name, $tags) { |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Deletes cache values matching the given tags |
||
| 632 | * |
||
| 633 | * @param array $tags |
||
| 634 | * |
||
| 635 | * @return array |
||
| 636 | */ |
||
| 637 | public function deleteTagged($tags) { |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Gets all tags currently in use |
||
| 650 | * |
||
| 651 | * @return array |
||
| 652 | */ |
||
| 653 | public function getAllTags() { |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Prepares tags parameter |
||
| 669 | * |
||
| 670 | * @param array|string $tags |
||
| 671 | */ |
||
| 672 | protected function prepareTags(&$tags) { |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Checks if cache value info can be modified (cache is enabled and value exists) |
||
| 681 | * |
||
| 682 | * @param string $name |
||
| 683 | * |
||
| 684 | * @return boolean |
||
| 685 | */ |
||
| 686 | protected function canModify($name) { |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Processes default value |
||
| 696 | * |
||
| 697 | * @param \Closure|mixed $default |
||
| 698 | * |
||
| 699 | * @return mixed |
||
| 700 | */ |
||
| 701 | protected function processDefault($default) { |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Gets created (first write) time of a cached value |
||
| 707 | * |
||
| 708 | * @param string $name Cache name |
||
| 709 | * @param string $format Date format |
||
| 710 | * |
||
| 711 | * @return string |
||
| 712 | */ |
||
| 713 | public function getCreated($name, $format = 'U') { |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Gets last access (either read or write) time of a cached value |
||
| 719 | * |
||
| 720 | * @param string $name Cache name |
||
| 721 | * @param string $format Date format |
||
| 722 | * |
||
| 723 | * @return string |
||
| 724 | */ |
||
| 725 | public function getLastAccess($name, $format = 'U') { |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Gets last read time of a cached value |
||
| 731 | * |
||
| 732 | * @param string $name Cache name |
||
| 733 | * @param string $format Date format |
||
| 734 | * |
||
| 735 | * @return string |
||
| 736 | */ |
||
| 737 | public function getLastRead($name, $format = 'U') { |
||
| 740 | |||
| 741 | /** |
||
| 742 | * Gets last write time of a cached value |
||
| 743 | * |
||
| 744 | * @param string $name Cache name |
||
| 745 | * @param string $format Date format |
||
| 746 | * |
||
| 747 | * @return string |
||
| 748 | */ |
||
| 749 | public function getLastWrite($name, $format = 'U') { |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Gets read count of a cached value |
||
| 755 | * |
||
| 756 | * @param string $name Cache name |
||
| 757 | * |
||
| 758 | * @return int |
||
| 759 | */ |
||
| 760 | public function getReadCount($name) { |
||
| 763 | |||
| 764 | /** |
||
| 765 | * Gets write count of a cached value |
||
| 766 | * |
||
| 767 | * @param string $name Cache name |
||
| 768 | * |
||
| 769 | * @return int |
||
| 770 | */ |
||
| 771 | public function getWriteCount($name) { |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Gets expiry information of a cached value (0: never) |
||
| 777 | * |
||
| 778 | * @param string $name Cache name |
||
| 779 | * @param string $format Date format |
||
| 780 | * |
||
| 781 | * @return string |
||
| 782 | */ |
||
| 783 | public function getExpiry($name, $format = 'U') { |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Calculates Time To Live |
||
| 793 | * |
||
| 794 | * @param string $name |
||
| 795 | * |
||
| 796 | * @return int |
||
| 797 | */ |
||
| 798 | public function getTTL($name) { |
||
| 802 | |||
| 803 | } |
||
| 804 |