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 = '') { |
||
| 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) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Retrieves the content of $name cache |
||
| 263 | * |
||
| 264 | * @param string $name cache name |
||
| 265 | * @param mixed $default |
||
| 266 | * |
||
| 267 | * @return mixed |
||
| 268 | */ |
||
| 269 | public function get($name, $default = null) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Extract cached value parameters |
||
| 294 | * |
||
| 295 | * @param string $name |
||
| 296 | * |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | protected function extractParameters($name) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Attempts to get a value and if not exists store the given default variable |
||
| 307 | * |
||
| 308 | * @param string $name cache name |
||
| 309 | * @param mixed $default default value |
||
| 310 | * @param bool $compressed Compressed storage |
||
| 311 | * @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') |
||
| 312 | * @param int $storeMethod Storing method (serialize|json) |
||
| 313 | * |
||
| 314 | * @return mixed |
||
| 315 | */ |
||
| 316 | public function getOrStore($name, $default, $compressed = false, $expiry = 0, $storeMethod = self::STORE_METHOD_SERIALIZE) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Retrieves and deletes value from cache |
||
| 327 | * |
||
| 328 | * @param string $name |
||
| 329 | * |
||
| 330 | * @return mixed |
||
| 331 | */ |
||
| 332 | public function pull($name) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Retrieves information of Cache state |
||
| 340 | * |
||
| 341 | * @param bool $getFields |
||
| 342 | * |
||
| 343 | * @return array|bool |
||
| 344 | */ |
||
| 345 | public function info($getFields = false) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Encodes variable with the specified method |
||
| 355 | * |
||
| 356 | * @param mixed $var Variable |
||
| 357 | * @param int $storeMethod serialize|json |
||
| 358 | * |
||
| 359 | * @return mixed |
||
| 360 | */ |
||
| 361 | protected function encode($var, $storeMethod = self::STORE_METHOD_SERIALIZE) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Decodes variable with the specified method |
||
| 375 | * |
||
| 376 | * @param mixed $var Variable |
||
| 377 | * @param int $storeMethod serialize|json |
||
| 378 | * |
||
| 379 | * @return mixed |
||
| 380 | */ |
||
| 381 | protected function decode($var, $storeMethod = self::STORE_METHOD_SERIALIZE) { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Encrypts key |
||
| 400 | * |
||
| 401 | * @param string $key |
||
| 402 | * |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | protected function encryptKey($key) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Gets cache hits |
||
| 411 | * |
||
| 412 | * @return int |
||
| 413 | */ |
||
| 414 | public function getHits() { |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Gets cache misses |
||
| 424 | * |
||
| 425 | * @return int |
||
| 426 | */ |
||
| 427 | public function getMisses() { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Stores cache values expiral information into cache |
||
| 437 | */ |
||
| 438 | public function writeExpirals() { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Modifies expiry by setting Time To Live |
||
| 447 | * |
||
| 448 | * @param string $name |
||
| 449 | * @param int $ttl |
||
| 450 | */ |
||
| 451 | public function setTTL($name, $ttl) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Modifies expiry |
||
| 461 | * |
||
| 462 | * @param string $name |
||
| 463 | * @param mixed $expiry |
||
| 464 | */ |
||
| 465 | public function setExpiry($name, $expiry) { |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Gets all cache key names |
||
| 473 | * |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | public function getKeys() { |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Gets cache key names which already read |
||
| 486 | * |
||
| 487 | * @return array |
||
| 488 | */ |
||
| 489 | public function getReadKeys() { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Gets storage object |
||
| 495 | * |
||
| 496 | * @return StorageInterface |
||
| 497 | */ |
||
| 498 | public function getStorage() { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Retrieves key encryption |
||
| 504 | * |
||
| 505 | * @return bool |
||
| 506 | */ |
||
| 507 | public function getEncryptKeys() { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Sets key encryption |
||
| 513 | * |
||
| 514 | * @param bool $encryptKeys |
||
| 515 | */ |
||
| 516 | public function setEncryptKeys($encryptKeys) { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Sets cache storage |
||
| 522 | * |
||
| 523 | * @param StorageInterface $storage |
||
| 524 | */ |
||
| 525 | public function setStorage(StorageInterface $storage) { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Destructor |
||
| 531 | */ |
||
| 532 | public function __destruct() { |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Sets a tagged cache value |
||
| 538 | * |
||
| 539 | * @param string $name cache name |
||
| 540 | * @param mixed $val variable to be stored |
||
| 541 | * @param array $tags tags |
||
| 542 | * @param bool $compressed Compressed storage |
||
| 543 | * @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') |
||
| 544 | * @param int $storeMethod Storing method (serialize|json) |
||
| 545 | * |
||
| 546 | * @return bool |
||
| 547 | */ |
||
| 548 | public function storeTagged($name, $val, $tags, $compressed = false, $expiry = 0, $storeMethod = self::STORE_METHOD_SERIALIZE) { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Gets tagged cache values |
||
| 558 | * |
||
| 559 | * @param array $tags |
||
| 560 | * |
||
| 561 | * @return array |
||
| 562 | */ |
||
| 563 | public function getTagged($tags) { |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Gets tags of a cached variable |
||
| 580 | * |
||
| 581 | * @param string $key |
||
| 582 | * |
||
| 583 | * @return array |
||
| 584 | */ |
||
| 585 | public function getTags($key) { |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Sets tags of a cached variable |
||
| 598 | * |
||
| 599 | * @param string $name |
||
| 600 | * @param array $tags |
||
| 601 | * |
||
| 602 | * @return array |
||
| 603 | */ |
||
| 604 | public function setTags($name, $tags) { |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Adds tags for a cached variable |
||
| 614 | * |
||
| 615 | * @param string $name |
||
| 616 | * @param array $tags |
||
| 617 | * |
||
| 618 | * @return array |
||
| 619 | */ |
||
| 620 | public function addTags($name, $tags) { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Deletes cache values matching the given tags |
||
| 631 | * |
||
| 632 | * @param array $tags |
||
| 633 | * |
||
| 634 | * @return array |
||
| 635 | */ |
||
| 636 | public function deleteTagged($tags) { |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Gets all tags currently in use |
||
| 649 | * |
||
| 650 | * @return array |
||
| 651 | */ |
||
| 652 | public function getAllTags() { |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Prepares tags parameter |
||
| 668 | * |
||
| 669 | * @param array|string $tags |
||
| 670 | */ |
||
| 671 | protected function prepareTags(&$tags) { |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Checks if cache value info can be modified (cache is enabled and value exists) |
||
| 680 | * |
||
| 681 | * @param string $name |
||
| 682 | * |
||
| 683 | * @return boolean |
||
| 684 | */ |
||
| 685 | protected function canModify($name) { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Processes default value |
||
| 695 | * |
||
| 696 | * @param \Closure|mixed $default |
||
| 697 | * |
||
| 698 | * @return mixed |
||
| 699 | */ |
||
| 700 | protected function processDefault($default) { |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Gets created (first write) time of a cached value |
||
| 706 | * |
||
| 707 | * @param string $name Cache name |
||
| 708 | * @param string $format Date format |
||
| 709 | * |
||
| 710 | * @return string |
||
| 711 | */ |
||
| 712 | public function getCreated($name, $format = 'U') { |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Gets last access (either read or write) time of a cached value |
||
| 718 | * |
||
| 719 | * @param string $name Cache name |
||
| 720 | * @param string $format Date format |
||
| 721 | * |
||
| 722 | * @return string |
||
| 723 | */ |
||
| 724 | public function getLastAccess($name, $format = 'U') { |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Gets last read time of a cached value |
||
| 730 | * |
||
| 731 | * @param string $name Cache name |
||
| 732 | * @param string $format Date format |
||
| 733 | * |
||
| 734 | * @return string |
||
| 735 | */ |
||
| 736 | public function getLastRead($name, $format = 'U') { |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Gets last write time of a cached value |
||
| 742 | * |
||
| 743 | * @param string $name Cache name |
||
| 744 | * @param string $format Date format |
||
| 745 | * |
||
| 746 | * @return string |
||
| 747 | */ |
||
| 748 | public function getLastWrite($name, $format = 'U') { |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Gets read count of a cached value |
||
| 754 | * |
||
| 755 | * @param string $name Cache name |
||
| 756 | * |
||
| 757 | * @return int |
||
| 758 | */ |
||
| 759 | public function getReadCount($name) { |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Gets write count of a cached value |
||
| 765 | * |
||
| 766 | * @param string $name Cache name |
||
| 767 | * |
||
| 768 | * @return int |
||
| 769 | */ |
||
| 770 | public function getWriteCount($name) { |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Gets expiry information of a cached value (0: never) |
||
| 776 | * |
||
| 777 | * @param string $name Cache name |
||
| 778 | * @param string $format Date format |
||
| 779 | * |
||
| 780 | * @return string |
||
| 781 | */ |
||
| 782 | public function getExpiry($name, $format = 'U') { |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Calculates Time To Live |
||
| 792 | * |
||
| 793 | * @param string $name |
||
| 794 | * |
||
| 795 | * @return int |
||
| 796 | */ |
||
| 797 | public function getTTL($name) { |
||
| 801 | |||
| 802 | } |
||
| 803 |