Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 9 | class CacheMemcache extends CacheBase |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * instance of Memcahe |
||
| 13 | * @var Memcahe |
||
| 14 | */ |
||
| 15 | var $Memcache; |
||
| 16 | var $SelectedExtension; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Get instance of CacheMemcache |
||
| 20 | * |
||
| 21 | * @param string $url url of memcache |
||
| 22 | * @return CacheMemcache instance of CacheMemcache |
||
| 23 | */ |
||
| 24 | function getInstance($url) |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Construct |
||
| 35 | * |
||
| 36 | * Do not use this directly. You can use getInstance() instead. |
||
| 37 | * @param string $url url of memcache |
||
| 38 | * @return void |
||
| 39 | */ |
||
| 40 | function CacheMemcache($url) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Return whether support or not support cache |
||
| 68 | * |
||
| 69 | * @return bool Return true on support or false on not support |
||
| 70 | */ |
||
| 71 | function isSupport() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get unique key of given key by path of XE |
||
| 93 | * |
||
| 94 | * @param string $key Cache key |
||
| 95 | * @return string Return unique key |
||
| 96 | */ |
||
| 97 | function getKey($key) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Store data at the server |
||
| 104 | * |
||
| 105 | * CacheMemcache::put() stores an item $buff with $key on the memcached server. |
||
| 106 | * Parameter $valid_time is expiration time in seconds. If it's 0, the item never expires |
||
| 107 | * (but memcached server doesn't guarantee this item to be stored all the time, it could be delete from the cache to make place for other items). |
||
| 108 | * |
||
| 109 | * Remember that resource variables (i.e. file and connection descriptors) cannot be stored in the cache, |
||
| 110 | * because they can not be adequately represented in serialized state. |
||
| 111 | * |
||
| 112 | * @param string $key The key that will be associated with the item. |
||
| 113 | * @param mixed $buff The variable to store. Strings and integers are stored as is, other types are stored serialized. |
||
| 114 | * @param int $valid_time Expiration time of the item. |
||
| 115 | * You can also use Unix timestamp or a number of seconds starting from current time, but in the latter case the number of seconds may not exceed 2592000 (30 days). |
||
| 116 | * If it's equal to zero, use the default valid time CacheMemcache::valid_time. |
||
| 117 | * @return bool Returns true on success or false on failure. |
||
| 118 | */ |
||
| 119 | function put($key, $buff, $valid_time = 0) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Return whether cache is valid or invalid |
||
| 137 | * |
||
| 138 | * @param string $key Cache key |
||
| 139 | * @param int $modified_time Unix time of data modified. |
||
| 140 | * If stored time is older then modified time, the data is invalid. |
||
| 141 | * @return bool Return true on valid or false on invalid. |
||
| 142 | */ |
||
| 143 | View Code Duplication | function isValid($key, $modified_time = 0) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Retrieve item from the server |
||
| 165 | * |
||
| 166 | * CacheMemcache::get() returns previously stored data if an item with such $key exists on the server at this moment. |
||
| 167 | * |
||
| 168 | * @param string $key The key to fetch |
||
| 169 | * @param int $modified_time Unix time of data modified. |
||
| 170 | * If stored time is older then modified time, return false. |
||
| 171 | * @return false|mixed Return false on failure or older then modified time. Return the string associated with the $key on success. |
||
| 172 | */ |
||
| 173 | View Code Duplication | function get($key, $modified_time = 0) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Delete item from the server |
||
| 195 | * |
||
| 196 | * CacheMemcache::delete() deletes an item with tey $key. |
||
| 197 | * |
||
| 198 | * @param string $key The key associated with the item to delete. |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | function delete($key) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Delete item from the server(private) |
||
| 209 | * |
||
| 210 | * @see CacheMemcache::delete() |
||
| 211 | * @param string $_key The key associated with the item to delete. |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | function _delete($_key) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Flush all existing items at the server |
||
| 221 | * |
||
| 222 | * CacheMemcache::truncate() immediately invalidates all existing items. |
||
| 223 | * CacheMemcache::truncate() doesn't actually free any resources, it only marks all the items as expired, |
||
| 224 | * so occupied memory will be overwitten by new items. |
||
| 225 | * |
||
| 226 | * @return bool Returns true on success or false on failure. |
||
| 227 | */ |
||
| 228 | function truncate() |
||
| 232 | |||
| 233 | } |
||
| 234 | /* End of file CacheMemcache.class.php */ |
||
| 236 |