Complex classes like Cache_Lite 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_Lite, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Cache_Lite |
||
30 | { |
||
31 | |||
32 | // --- Private properties --- |
||
33 | |||
34 | /** |
||
35 | * Directory where to put the cache files |
||
36 | * (make sure to add a trailing slash) |
||
37 | * |
||
38 | * @var string $_cacheDir |
||
39 | */ |
||
40 | var $_cacheDir = '/tmp/'; |
||
41 | |||
42 | /** |
||
43 | * Enable / disable caching |
||
44 | * |
||
45 | * (can be very usefull for the debug of cached scripts) |
||
46 | * |
||
47 | * @var boolean $_caching |
||
48 | */ |
||
49 | var $_caching = true; |
||
50 | |||
51 | /** |
||
52 | * Cache lifetime (in seconds) |
||
53 | * |
||
54 | * If null, the cache is valid forever. |
||
55 | * |
||
56 | * @var int $_lifeTime |
||
57 | */ |
||
58 | var $_lifeTime = 3600; |
||
59 | |||
60 | /** |
||
61 | * Enable / disable fileLocking |
||
62 | * |
||
63 | * (can avoid cache corruption under bad circumstances) |
||
64 | * |
||
65 | * @var boolean $_fileLocking |
||
66 | */ |
||
67 | var $_fileLocking = true; |
||
68 | |||
69 | /** |
||
70 | * Timestamp of the last valid cache |
||
71 | * |
||
72 | * @var int $_refreshTime |
||
73 | */ |
||
74 | var $_refreshTime; |
||
75 | |||
76 | /** |
||
77 | * File name (with path) |
||
78 | * |
||
79 | * @var string $_file |
||
80 | */ |
||
81 | var $_file; |
||
82 | |||
83 | /** |
||
84 | * File name (without path) |
||
85 | * |
||
86 | * @var string $_fileName |
||
87 | */ |
||
88 | var $_fileName; |
||
89 | |||
90 | /** |
||
91 | * Enable / disable write control (the cache is read just after writing to detect corrupt entries) |
||
92 | * |
||
93 | * Enable write control will lightly slow the cache writing but not the cache reading |
||
94 | * Write control can detect some corrupt cache files but maybe it's not a perfect control |
||
95 | * |
||
96 | * @var boolean $_writeControl |
||
97 | */ |
||
98 | var $_writeControl = true; |
||
99 | |||
100 | /** |
||
101 | * Enable / disable read control |
||
102 | * |
||
103 | * If enabled, a control key is embeded in cache file and this key is compared with the one |
||
104 | * calculated after the reading. |
||
105 | * |
||
106 | * @var boolean $_writeControl |
||
107 | */ |
||
108 | var $_readControl = false; |
||
109 | |||
110 | /** |
||
111 | * Type of read control (only if read control is enabled) |
||
112 | * |
||
113 | * Available values are : |
||
114 | * 'md5' for a md5 hash control (best but slowest) |
||
115 | * 'crc32' for a crc32 hash control (lightly less safe but faster, better choice) |
||
116 | * 'strlen' for a length only test (fastest) |
||
117 | * |
||
118 | * @var boolean $_readControlType |
||
119 | */ |
||
120 | var $_readControlType = 'crc32'; |
||
121 | |||
122 | /** |
||
123 | * Pear error mode (when raiseError is called) |
||
124 | * |
||
125 | * (see PEAR doc) |
||
126 | * |
||
127 | * @see setToDebug() |
||
128 | * @var int $_pearErrorMode |
||
129 | */ |
||
130 | var $_pearErrorMode = CACHE_LITE_ERROR_RETURN; |
||
131 | |||
132 | /** |
||
133 | * Current cache id |
||
134 | * |
||
135 | * @var string $_id |
||
136 | */ |
||
137 | var $_id; |
||
138 | |||
139 | /** |
||
140 | * Current cache group |
||
141 | * |
||
142 | * @var string $_group |
||
143 | */ |
||
144 | var $_group; |
||
145 | |||
146 | /** |
||
147 | * Enable / Disable "Memory Caching" |
||
148 | * |
||
149 | * NB : There is no lifetime for memory caching ! |
||
150 | * |
||
151 | * @var boolean $_memoryCaching |
||
152 | */ |
||
153 | var $_memoryCaching = false; |
||
154 | |||
155 | /** |
||
156 | * Enable / Disable "Only Memory Caching" |
||
157 | * (be carefull, memory caching is "beta quality") |
||
158 | * |
||
159 | * @var boolean $_onlyMemoryCaching |
||
160 | */ |
||
161 | var $_onlyMemoryCaching = false; |
||
162 | |||
163 | /** |
||
164 | * Memory caching array |
||
165 | * |
||
166 | * @var array $_memoryCachingArray |
||
167 | */ |
||
168 | var $_memoryCachingArray = array(); |
||
169 | |||
170 | /** |
||
171 | * Memory caching counter |
||
172 | * |
||
173 | * @var int $memoryCachingCounter |
||
174 | */ |
||
175 | var $_memoryCachingCounter = 0; |
||
176 | |||
177 | /** |
||
178 | * Memory caching limit |
||
179 | * |
||
180 | * @var int $memoryCachingLimit |
||
181 | */ |
||
182 | var $_memoryCachingLimit = 1000; |
||
183 | |||
184 | /** |
||
185 | * File Name protection |
||
186 | * |
||
187 | * if set to true, you can use any cache id or group name |
||
188 | * if set to false, it can be faster but cache ids and group names |
||
189 | * will be used directly in cache file names so be carefull with |
||
190 | * special characters... |
||
191 | * |
||
192 | * @var boolean $fileNameProtection |
||
193 | */ |
||
194 | var $_fileNameProtection = true; |
||
195 | |||
196 | /** |
||
197 | * Enable / disable automatic serialization |
||
198 | * |
||
199 | * it can be used to save directly datas which aren't strings |
||
200 | * (but it's slower) |
||
201 | * |
||
202 | * @var boolean $_serialize |
||
203 | */ |
||
204 | var $_automaticSerialization = false; |
||
205 | |||
206 | /** |
||
207 | * Disable / Tune the automatic cleaning process |
||
208 | * |
||
209 | * The automatic cleaning process destroy too old (for the given life time) |
||
210 | * cache files when a new cache file is written. |
||
211 | * 0 => no automatic cache cleaning |
||
212 | * 1 => systematic cache cleaning |
||
213 | * x (integer) > 1 => automatic cleaning randomly 1 times on x cache write |
||
214 | * |
||
215 | * @var int $_automaticCleaning |
||
216 | */ |
||
217 | var $_automaticCleaningFactor = 0; |
||
218 | |||
219 | /** |
||
220 | * Nested directory level |
||
221 | * |
||
222 | * Set the hashed directory structure level. 0 means "no hashed directory |
||
223 | * structure", 1 means "one level of directory", 2 means "two levels"... |
||
224 | * This option can speed up Cache_Lite only when you have many thousands of |
||
225 | * cache file. Only specific benchs can help you to choose the perfect value |
||
226 | * for you. Maybe, 1 or 2 is a good start. |
||
227 | * |
||
228 | * @var int $_hashedDirectoryLevel |
||
229 | */ |
||
230 | var $_hashedDirectoryLevel = 0; |
||
231 | |||
232 | /** |
||
233 | * Umask for hashed directory structure |
||
234 | * |
||
235 | * @var int $_hashedDirectoryUmask |
||
236 | */ |
||
237 | var $_hashedDirectoryUmask = 0770; |
||
238 | |||
239 | /** |
||
240 | * API break for error handling in CACHE_LITE_ERROR_RETURN mode |
||
241 | * |
||
242 | * In CACHE_LITE_ERROR_RETURN mode, error handling was not good because |
||
243 | * for example save() method always returned a boolean (a PEAR_Error object |
||
244 | * would be better in CACHE_LITE_ERROR_RETURN mode). To correct this without |
||
245 | * breaking the API, this option (false by default) can change this handling. |
||
246 | * |
||
247 | * @var boolean |
||
248 | */ |
||
249 | var $_errorHandlingAPIBreak = false; |
||
250 | |||
251 | // --- Public methods --- |
||
252 | |||
253 | /** |
||
254 | * Constructor |
||
255 | * |
||
256 | * $options is an assoc. Available options are : |
||
257 | * $options = array( |
||
258 | * 'cacheDir' => directory where to put the cache files (string), |
||
259 | * 'caching' => enable / disable caching (boolean), |
||
260 | * 'lifeTime' => cache lifetime in seconds (int), |
||
261 | * 'fileLocking' => enable / disable fileLocking (boolean), |
||
262 | * 'writeControl' => enable / disable write control (boolean), |
||
263 | * 'readControl' => enable / disable read control (boolean), |
||
264 | * 'readControlType' => type of read control 'crc32', 'md5', 'strlen' (string), |
||
265 | * 'pearErrorMode' => pear error mode (when raiseError is called) (cf PEAR doc) (int), |
||
266 | * 'memoryCaching' => enable / disable memory caching (boolean), |
||
267 | * 'onlyMemoryCaching' => enable / disable only memory caching (boolean), |
||
268 | * 'memoryCachingLimit' => max nbr of records to store into memory caching (int), |
||
269 | * 'fileNameProtection' => enable / disable automatic file name protection (boolean), |
||
270 | * 'automaticSerialization' => enable / disable automatic serialization (boolean), |
||
271 | * 'automaticCleaningFactor' => distable / tune automatic cleaning process (int), |
||
272 | * 'hashedDirectoryLevel' => level of the hashed directory system (int), |
||
273 | * 'hashedDirectoryUmask' => umask for hashed directory structure (int), |
||
274 | * 'errorHandlingAPIBreak' => API break for better error handling ? (boolean) |
||
275 | * ); |
||
276 | * |
||
277 | * @param array $options options |
||
278 | * @access public |
||
279 | */ |
||
280 | function __construct($options = array(NULL)) |
||
286 | |||
287 | /** |
||
288 | * Generic way to set a Cache_Lite option |
||
289 | * |
||
290 | * see Cache_Lite constructor for available options |
||
291 | * |
||
292 | * @var string $name name of the option |
||
293 | * @var mixed $value value of the option |
||
294 | * @access public |
||
295 | */ |
||
296 | function setOption($name, $value) |
||
304 | |||
305 | /** |
||
306 | * Test if a cache is available and (if yes) return it |
||
307 | * |
||
308 | * @param string $id cache id |
||
309 | * @param string $group name of the cache group |
||
310 | * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested |
||
311 | * @return string data of the cache (else : false) |
||
312 | * @access public |
||
313 | */ |
||
314 | function get($id, $group = 'default', $doNotTestCacheValidity = false) |
||
353 | |||
354 | /** |
||
355 | * Save some data in a cache file |
||
356 | * |
||
357 | * @param string $data data to put in cache (can be another type than strings if automaticSerialization is on) |
||
358 | * @param string $id cache id |
||
359 | * @param string $group name of the cache group |
||
360 | * @return boolean true if no problem (else : false or a PEAR_Error object) |
||
361 | * @access public |
||
362 | */ |
||
363 | function save($data, $id = NULL, $group = 'default') |
||
407 | |||
408 | /** |
||
409 | * Remove a cache file |
||
410 | * |
||
411 | * @param string $id cache id |
||
412 | * @param string $group name of the cache group |
||
413 | * @return boolean true if no problem |
||
414 | * @access public |
||
415 | */ |
||
416 | function remove($id, $group = 'default') |
||
430 | |||
431 | /** |
||
432 | * Clean the cache |
||
433 | * |
||
434 | * if no group is specified all cache files will be destroyed |
||
435 | * else only cache files of the specified group will be destroyed |
||
436 | * |
||
437 | * @param string $group name of the cache group |
||
438 | * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup', |
||
439 | * 'callback_myFunction' |
||
440 | * @return boolean true if no problem |
||
441 | * @access public |
||
442 | */ |
||
443 | function clean($group = false, $mode = 'ingroup') |
||
447 | |||
448 | /** |
||
449 | * Set to debug mode |
||
450 | * |
||
451 | * When an error is found, the script will stop and the message will be displayed |
||
452 | * (in debug mode only). |
||
453 | * |
||
454 | * @access public |
||
455 | */ |
||
456 | function setToDebug() |
||
460 | |||
461 | /** |
||
462 | * Set a new life time |
||
463 | * |
||
464 | * @param int $newLifeTime new life time (in seconds) |
||
465 | * @access public |
||
466 | */ |
||
467 | function setLifeTime($newLifeTime) |
||
472 | |||
473 | /** |
||
474 | * Save the state of the caching memory array into a cache file cache |
||
475 | * |
||
476 | * @param string $id cache id |
||
477 | * @param string $group name of the cache group |
||
478 | * @access public |
||
479 | */ |
||
480 | function saveMemoryCachingState($id, $group = 'default') |
||
491 | |||
492 | /** |
||
493 | * Load the state of the caching memory array from a given cache file cache |
||
494 | * |
||
495 | * @param string $id cache id |
||
496 | * @param string $group name of the cache group |
||
497 | * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested |
||
498 | * @access public |
||
499 | */ |
||
500 | function getMemoryCachingState($id, $group = 'default', $doNotTestCacheValidity = false) |
||
510 | |||
511 | /** |
||
512 | * Return the cache last modification time |
||
513 | * |
||
514 | * BE CAREFUL : THIS METHOD IS FOR HACKING ONLY ! |
||
515 | * |
||
516 | * @return int last modification time |
||
517 | */ |
||
518 | function lastModified() |
||
522 | |||
523 | /** |
||
524 | * Trigger error |
||
525 | * |
||
526 | * @param string $msg error message |
||
527 | * @param int $code error code |
||
528 | * @access public |
||
529 | */ |
||
530 | function raiseError($msg, $code) |
||
534 | |||
535 | /** |
||
536 | * Extend the life of a valid cache file |
||
537 | * |
||
538 | * see http://pear.php.net/bugs/bug.php?id=6681 |
||
539 | * |
||
540 | * @access public |
||
541 | */ |
||
542 | function extendLife() |
||
546 | |||
547 | // --- Private methods --- |
||
548 | |||
549 | /** |
||
550 | * Compute & set the refresh time |
||
551 | * |
||
552 | * @access private |
||
553 | */ |
||
554 | function _setRefreshTime() |
||
562 | |||
563 | /** |
||
564 | * Remove a file |
||
565 | * |
||
566 | * @param string $file complete file path and name |
||
567 | * @return boolean true if no problem |
||
568 | * @access private |
||
569 | */ |
||
570 | function _unlink($file) |
||
577 | |||
578 | /** |
||
579 | * Recursive function for cleaning cache file in the given directory |
||
580 | * |
||
581 | * @param string $dir directory complete path (with a trailing slash) |
||
582 | * @param string $group name of the cache group |
||
583 | * @param string $mode flush cache mode : 'old', 'ingroup', 'notingroup', |
||
584 | 'callback_myFunction' |
||
585 | * @return boolean true if no problem |
||
586 | * @access private |
||
587 | */ |
||
588 | function _cleanDir($dir, $group = false, $mode = 'ingroup') |
||
651 | |||
652 | /** |
||
653 | * Add some date in the memory caching array |
||
654 | * |
||
655 | * @param string $data data to cache |
||
656 | * @access private |
||
657 | */ |
||
658 | function _memoryCacheAdd($data) |
||
668 | |||
669 | /** |
||
670 | * Make a file name (with path) |
||
671 | * |
||
672 | * @param string $id cache id |
||
673 | * @param string $group name of the group |
||
674 | * @access private |
||
675 | */ |
||
676 | function _setFileName($id, $group) |
||
693 | |||
694 | /** |
||
695 | * Read the cache file and return the content |
||
696 | * |
||
697 | * @return string content of the cache file (else : false or a PEAR_Error object) |
||
698 | * @access private |
||
699 | */ |
||
700 | function _read() |
||
736 | |||
737 | /** |
||
738 | * Write the given data in the cache file |
||
739 | * |
||
740 | * @param string $data data to put in cache |
||
741 | * @return boolean true if ok (a PEAR_Error object else) |
||
742 | * @access private |
||
743 | */ |
||
744 | function _write($data) |
||
773 | |||
774 | /** |
||
775 | * Write the given data in the cache file and control it just after to avoir corrupted cache entries |
||
776 | * |
||
777 | * @param string $data data to put in cache |
||
778 | * @return boolean true if the test is ok (else : false or a PEAR_Error object) |
||
779 | * @access private |
||
780 | */ |
||
781 | function _writeAndControl($data) |
||
796 | |||
797 | /** |
||
798 | * Make a control key with the string containing datas |
||
799 | * |
||
800 | * @param string $data data |
||
801 | * @param string $controlType type of control 'md5', 'crc32' or 'strlen' |
||
802 | * @return string control key |
||
803 | * @access private |
||
804 | */ |
||
805 | function _hash($data, $controlType) |
||
818 | |||
819 | } |
||
820 | |||
822 |