Complex classes like Zend_Cache_Backend_File 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 Zend_Cache_Backend_File, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Zend_Cache_Backend_File extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * Available options |
||
| 44 | * |
||
| 45 | * =====> (string) cache_dir : |
||
| 46 | * - Directory where to put the cache files |
||
| 47 | * |
||
| 48 | * =====> (boolean) file_locking : |
||
| 49 | * - Enable / disable file_locking |
||
| 50 | * - Can avoid cache corruption under bad circumstances but it doesn't work on multithread |
||
| 51 | * webservers and on NFS filesystems for example |
||
| 52 | * |
||
| 53 | * =====> (boolean) read_control : |
||
| 54 | * - Enable / disable read control |
||
| 55 | * - If enabled, a control key is embeded in cache file and this key is compared with the one |
||
| 56 | * calculated after the reading. |
||
| 57 | * |
||
| 58 | * =====> (string) read_control_type : |
||
| 59 | * - Type of read control (only if read control is enabled). Available values are : |
||
| 60 | * 'md5' for a md5 hash control (best but slowest) |
||
| 61 | * 'crc32' for a crc32 hash control (lightly less safe but faster, better choice) |
||
| 62 | * 'adler32' for an adler32 hash control (excellent choice too, faster than crc32) |
||
| 63 | * 'strlen' for a length only test (fastest) |
||
| 64 | * |
||
| 65 | * =====> (int) hashed_directory_level : |
||
| 66 | * - Hashed directory level |
||
| 67 | * - Set the hashed directory structure level. 0 means "no hashed directory |
||
| 68 | * structure", 1 means "one level of directory", 2 means "two levels"... |
||
| 69 | * This option can speed up the cache only when you have many thousands of |
||
| 70 | * cache file. Only specific benchs can help you to choose the perfect value |
||
| 71 | * for you. Maybe, 1 or 2 is a good start. |
||
| 72 | * |
||
| 73 | * =====> (int) hashed_directory_umask : |
||
| 74 | * - Umask for hashed directory structure |
||
| 75 | * |
||
| 76 | * =====> (string) file_name_prefix : |
||
| 77 | * - prefix for cache files |
||
| 78 | * - be really carefull with this option because a too generic value in a system cache dir |
||
| 79 | * (like /tmp) can cause disasters when cleaning the cache |
||
| 80 | * |
||
| 81 | * =====> (int) cache_file_umask : |
||
| 82 | * - Umask for cache files |
||
| 83 | * |
||
| 84 | * =====> (int) metatadatas_array_max_size : |
||
| 85 | * - max size for the metadatas array (don't change this value unless you |
||
| 86 | * know what you are doing) |
||
| 87 | * |
||
| 88 | * @var array available options |
||
| 89 | */ |
||
| 90 | protected $_options = array( |
||
| 91 | 'cache_dir' => null, |
||
| 92 | 'file_locking' => true, |
||
| 93 | 'read_control' => true, |
||
| 94 | 'read_control_type' => 'crc32', |
||
| 95 | 'hashed_directory_level' => 0, |
||
| 96 | 'hashed_directory_umask' => 0700, |
||
| 97 | 'file_name_prefix' => 'zend_cache', |
||
| 98 | 'cache_file_umask' => 0600, |
||
| 99 | 'metadatas_array_max_size' => 100 |
||
| 100 | ); |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Array of metadatas (each item is an associative array) |
||
| 104 | * |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | protected $_metadatasArray = array(); |
||
| 108 | |||
| 109 | |||
| 110 | /** |
||
| 111 | * Constructor |
||
| 112 | * |
||
| 113 | * @param array $options associative array of options |
||
| 114 | * @throws Zend_Cache_Exception |
||
| 115 | * @return void |
||
| 116 | */ |
||
| 117 | public function __construct(array $options = array()) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Set the cache_dir (particular case of setOption() method) |
||
| 145 | * |
||
| 146 | * @param string $value |
||
| 147 | * @param boolean $trailingSeparator If true, add a trailing separator is necessary |
||
| 148 | * @throws Zend_Cache_Exception |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | public function setCacheDir($value, $trailingSeparator = true) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Test if a cache is available for the given id and (if yes) return it (false else) |
||
| 168 | * |
||
| 169 | * @param string $id cache id |
||
| 170 | * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested |
||
| 171 | * @return string|false cached datas |
||
| 172 | */ |
||
| 173 | public function load($id, $doNotTestCacheValidity = false) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Test if a cache is available or not (for the given id) |
||
| 197 | * |
||
| 198 | * @param string $id cache id |
||
| 199 | * @return mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record |
||
| 200 | */ |
||
| 201 | public function test($id) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Save some string datas into a cache record |
||
| 209 | * |
||
| 210 | * Note : $data is always "string" (serialization is done by the |
||
| 211 | * core not by the backend) |
||
| 212 | * |
||
| 213 | * @param string $data Datas to cache |
||
| 214 | * @param string $id Cache id |
||
| 215 | * @param array $tags Array of strings, the cache record will be tagged by each string entry |
||
| 216 | * @param int $specificLifetime If != false, set a specific lifetime for this cache record (null => infinite lifetime) |
||
| 217 | * @return boolean true if no problem |
||
| 218 | */ |
||
| 219 | public function save($data, $id, $tags = array(), $specificLifetime = false) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Remove a cache record |
||
| 255 | * |
||
| 256 | * @param string $id cache id |
||
| 257 | * @return boolean true if no problem |
||
| 258 | */ |
||
| 259 | public function remove($id) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Clean some cache records |
||
| 269 | * |
||
| 270 | * Available modes are : |
||
| 271 | * |
||
| 272 | * Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used) |
||
| 273 | * Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used) |
||
| 274 | * Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags |
||
| 275 | * ($tags can be an array of strings or a single string) |
||
| 276 | * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags} |
||
| 277 | * ($tags can be an array of strings or a single string) |
||
| 278 | * Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags |
||
| 279 | * ($tags can be an array of strings or a single string) |
||
| 280 | * |
||
| 281 | * @param string $mode clean mode |
||
| 282 | * @param tags array $tags array of tags |
||
| 283 | * @return boolean true if no problem |
||
| 284 | */ |
||
| 285 | public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array()) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Return an array of stored cache ids |
||
| 294 | * |
||
| 295 | * @return array array of stored cache ids (string) |
||
| 296 | */ |
||
| 297 | public function getIds() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Return an array of stored tags |
||
| 304 | * |
||
| 305 | * @return array array of stored tags (string) |
||
| 306 | */ |
||
| 307 | public function getTags() |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Return an array of stored cache ids which match given tags |
||
| 314 | * |
||
| 315 | * In case of multiple tags, a logical AND is made between tags |
||
| 316 | * |
||
| 317 | * @param array $tags array of tags |
||
| 318 | * @return array array of matching cache ids (string) |
||
| 319 | */ |
||
| 320 | public function getIdsMatchingTags($tags = array()) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Return an array of stored cache ids which don't match given tags |
||
| 327 | * |
||
| 328 | * In case of multiple tags, a logical OR is made between tags |
||
| 329 | * |
||
| 330 | * @param array $tags array of tags |
||
| 331 | * @return array array of not matching cache ids (string) |
||
| 332 | */ |
||
| 333 | public function getIdsNotMatchingTags($tags = array()) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Return an array of stored cache ids which match any given tags |
||
| 340 | * |
||
| 341 | * In case of multiple tags, a logical AND is made between tags |
||
| 342 | * |
||
| 343 | * @param array $tags array of tags |
||
| 344 | * @return array array of any matching cache ids (string) |
||
| 345 | */ |
||
| 346 | public function getIdsMatchingAnyTags($tags = array()) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Return the filling percentage of the backend storage |
||
| 353 | * |
||
| 354 | * @throws Zend_Cache_Exception |
||
| 355 | * @return int integer between 0 and 100 |
||
| 356 | */ |
||
| 357 | public function getFillingPercentage() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Return an array of metadatas for the given cache id |
||
| 373 | * |
||
| 374 | * The array must include these keys : |
||
| 375 | * - expire : the expire timestamp |
||
| 376 | * - tags : a string array of tags |
||
| 377 | * - mtime : timestamp of last modification time |
||
| 378 | * |
||
| 379 | * @param string $id cache id |
||
| 380 | * @return array array of metadatas (false if the cache id is not found) |
||
| 381 | */ |
||
| 382 | public function getMetadatas($id) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Give (if possible) an extra lifetime to the given cache id |
||
| 400 | * |
||
| 401 | * @param string $id cache id |
||
| 402 | * @param int $extraLifetime |
||
| 403 | * @return boolean true if ok |
||
| 404 | */ |
||
| 405 | public function touch($id, $extraLifetime) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Return an associative array of capabilities (booleans) of the backend |
||
| 429 | * |
||
| 430 | * The array must include these keys : |
||
| 431 | * - automatic_cleaning (is automating cleaning necessary) |
||
| 432 | * - tags (are tags supported) |
||
| 433 | * - expired_read (is it possible to read expired cache records |
||
| 434 | * (for doNotTestCacheValidity option for example)) |
||
| 435 | * - priority does the backend deal with priority when saving |
||
| 436 | * - infinite_lifetime (is infinite lifetime can work with this backend) |
||
| 437 | * - get_list (is it possible to get the list of cache ids and the complete list of tags) |
||
| 438 | * |
||
| 439 | * @return array associative of with capabilities |
||
| 440 | */ |
||
| 441 | public function getCapabilities() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * PUBLIC METHOD FOR UNIT TESTING ONLY ! |
||
| 455 | * |
||
| 456 | * Force a cache record to expire |
||
| 457 | * |
||
| 458 | * @param string $id cache id |
||
| 459 | */ |
||
| 460 | public function ___expire($id) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get a metadatas record |
||
| 471 | * |
||
| 472 | * @param string $id Cache id |
||
| 473 | * @return array|false Associative array of metadatas |
||
| 474 | */ |
||
| 475 | protected function _getMetadatas($id) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Set a metadatas record |
||
| 491 | * |
||
| 492 | * @param string $id Cache id |
||
| 493 | * @param array $metadatas Associative array of metadatas |
||
| 494 | * @param boolean $save optional pass false to disable saving to file |
||
| 495 | * @return boolean True if no problem |
||
| 496 | */ |
||
| 497 | protected function _setMetadatas($id, $metadatas, $save = true) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Drop a metadata record |
||
| 515 | * |
||
| 516 | * @param string $id Cache id |
||
| 517 | * @return boolean True if no problem |
||
| 518 | */ |
||
| 519 | protected function _delMetadatas($id) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Clear the metadatas array |
||
| 530 | * |
||
| 531 | * @return void |
||
| 532 | */ |
||
| 533 | protected function _cleanMetadatas() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Load metadatas from disk |
||
| 540 | * |
||
| 541 | * @param string $id Cache id |
||
| 542 | * @return array|false Metadatas associative array |
||
| 543 | */ |
||
| 544 | protected function _loadMetadatas($id) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Save metadatas to disk |
||
| 557 | * |
||
| 558 | * @param string $id Cache id |
||
| 559 | * @param array $metadatas Associative array |
||
| 560 | * @return boolean True if no problem |
||
| 561 | */ |
||
| 562 | protected function _saveMetadatas($id, $metadatas) |
||
| 571 | |||
| 572 | /** |
||
| 573 | * Make and return a file name (with path) for metadatas |
||
| 574 | * |
||
| 575 | * @param string $id Cache id |
||
| 576 | * @return string Metadatas file name (with path) |
||
| 577 | */ |
||
| 578 | protected function _metadatasFile($id) |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Check if the given filename is a metadatas one |
||
| 587 | * |
||
| 588 | * @param string $fileName File name |
||
| 589 | * @return boolean True if it's a metadatas one |
||
| 590 | */ |
||
| 591 | protected function _isMetadatasFile($fileName) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Remove a file |
||
| 603 | * |
||
| 604 | * If we can't remove the file (because of locks or any problem), we will touch |
||
| 605 | * the file to invalidate it |
||
| 606 | * |
||
| 607 | * @param string $file Complete file path |
||
| 608 | * @return boolean True if ok |
||
| 609 | */ |
||
| 610 | protected function _remove($file) |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Clean some cache records (protected method used for recursive stuff) |
||
| 625 | * |
||
| 626 | * Available modes are : |
||
| 627 | * Zend_Cache::CLEANING_MODE_ALL (default) => remove all cache entries ($tags is not used) |
||
| 628 | * Zend_Cache::CLEANING_MODE_OLD => remove too old cache entries ($tags is not used) |
||
| 629 | * Zend_Cache::CLEANING_MODE_MATCHING_TAG => remove cache entries matching all given tags |
||
| 630 | * ($tags can be an array of strings or a single string) |
||
| 631 | * Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG => remove cache entries not {matching one of the given tags} |
||
| 632 | * ($tags can be an array of strings or a single string) |
||
| 633 | * Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG => remove cache entries matching any given tags |
||
| 634 | * ($tags can be an array of strings or a single string) |
||
| 635 | * |
||
| 636 | * @param string $dir Directory to clean |
||
| 637 | * @param string $mode Clean mode |
||
| 638 | * @param array $tags Array of tags |
||
| 639 | * @throws Zend_Cache_Exception |
||
| 640 | * @return boolean True if no problem |
||
| 641 | */ |
||
| 642 | protected function _clean($dir, $mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array()) |
||
| 734 | |||
| 735 | protected function _get($dir, $mode, $tags = array()) |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Compute & return the expire time |
||
| 821 | * |
||
| 822 | * @return int expire time (unix timestamp) |
||
| 823 | */ |
||
| 824 | protected function _expireTime($lifetime) |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Make a control key with the string containing datas |
||
| 834 | * |
||
| 835 | * @param string $data Data |
||
| 836 | * @param string $controlType Type of control 'md5', 'crc32' or 'strlen' |
||
| 837 | * @throws Zend_Cache_Exception |
||
| 838 | * @return string Control key |
||
| 839 | */ |
||
| 840 | protected function _hash($data, $controlType) |
||
| 855 | |||
| 856 | /** |
||
| 857 | * Transform a cache id into a file name and return it |
||
| 858 | * |
||
| 859 | * @param string $id Cache id |
||
| 860 | * @return string File name |
||
| 861 | */ |
||
| 862 | protected function _idToFileName($id) |
||
| 868 | |||
| 869 | /** |
||
| 870 | * Make and return a file name (with path) |
||
| 871 | * |
||
| 872 | * @param string $id Cache id |
||
| 873 | * @return string File name (with path) |
||
| 874 | */ |
||
| 875 | protected function _file($id) |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Return the complete directory path of a filename (including hashedDirectoryStructure) |
||
| 884 | * |
||
| 885 | * @param string $id Cache id |
||
| 886 | * @param boolean $parts if true, returns array of directory parts instead of single string |
||
| 887 | * @return string Complete directory path |
||
| 888 | */ |
||
| 889 | protected function _path($id, $parts = false) |
||
| 907 | |||
| 908 | /** |
||
| 909 | * Make the directory strucuture for the given id |
||
| 910 | * |
||
| 911 | * @param string $id cache id |
||
| 912 | * @return boolean true |
||
| 913 | */ |
||
| 914 | protected function _recursiveMkdirAndChmod($id) |
||
| 928 | |||
| 929 | /** |
||
| 930 | * Test if the given cache id is available (and still valid as a cache record) |
||
| 931 | * |
||
| 932 | * @param string $id Cache id |
||
| 933 | * @param boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested |
||
| 934 | * @return boolean|mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record |
||
| 935 | */ |
||
| 936 | protected function _test($id, $doNotTestCacheValidity) |
||
| 947 | |||
| 948 | /** |
||
| 949 | * Return the file content of the given file |
||
| 950 | * |
||
| 951 | * @param string $file File complete path |
||
| 952 | * @return string File content (or false if problem) |
||
| 953 | */ |
||
| 954 | protected function _fileGetContents($file) |
||
| 969 | |||
| 970 | /** |
||
| 971 | * Put the given string into the given file |
||
| 972 | * |
||
| 973 | * @param string $file File complete path |
||
| 974 | * @param string $string String to put in file |
||
| 975 | * @return boolean true if no problem |
||
| 976 | */ |
||
| 977 | protected function _filePutContents($file, $string) |
||
| 994 | |||
| 995 | /** |
||
| 996 | * Transform a file name into cache id and return it |
||
| 997 | * |
||
| 998 | * @param string $fileName File name |
||
| 999 | * @return string Cache id |
||
| 1000 | */ |
||
| 1001 | protected function _fileNameToId($fileName) |
||
| 1006 | |||
| 1007 | } |
||
| 1008 |