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 |
||
| 29 | abstract class FileCacheBase { |
||
| 30 | protected $mKey; |
||
| 31 | protected $mType = 'object'; |
||
| 32 | protected $mExt = 'cache'; |
||
| 33 | protected $mFilePath; |
||
| 34 | protected $mUseGzip; |
||
| 35 | /* lazy loaded */ |
||
| 36 | protected $mCached; |
||
| 37 | |||
| 38 | /* @todo configurable? */ |
||
| 39 | const MISS_FACTOR = 15; // log 1 every MISS_FACTOR cache misses |
||
| 40 | const MISS_TTL_SEC = 3600; // how many seconds ago is "recent" |
||
| 41 | |||
| 42 | protected function __construct() { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Get the base file cache directory |
||
| 50 | * @return string |
||
| 51 | */ |
||
| 52 | final protected function baseCacheDirectory() { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get the base cache directory (not specific to this file) |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | abstract protected function cacheDirectory(); |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get the path to the cache file |
||
| 66 | * @return string |
||
| 67 | */ |
||
| 68 | protected function cachePath() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Check if the cache file exists |
||
| 89 | * @return bool |
||
| 90 | */ |
||
| 91 | public function isCached() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get the last-modified timestamp of the cache file |
||
| 101 | * @return string|bool TS_MW timestamp |
||
| 102 | */ |
||
| 103 | public function cacheTimestamp() { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Check if up to date cache file exists |
||
| 113 | * @param string $timestamp MW_TS timestamp |
||
| 114 | * |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | public function isCacheGood( $timestamp = '' ) { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Check if the cache is gzipped |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | protected function useGzip() { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Get the uncompressed text from the cache |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function fetchText() { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Save and compress text to the cache |
||
| 156 | * @param string $text |
||
| 157 | * @return string Compressed text |
||
| 158 | */ |
||
| 159 | public function saveText( $text ) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Clear the cache for this page |
||
| 179 | * @return void |
||
| 180 | */ |
||
| 181 | public function clearCache() { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Create parent directors of $this->cachePath() |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | protected function checkCacheDirs() { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get the cache type subdirectory (with trailing slash) |
||
| 198 | * An extending class could use that method to alter the type -> directory |
||
| 199 | * mapping. @see HTMLFileCache::typeSubdirectory() for an example. |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | protected function typeSubdirectory() { |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Return relative multi-level hash subdirectory (with trailing slash) |
||
| 209 | * or the empty string if not $wgFileCacheDepth |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | View Code Duplication | protected function hashSubdirectory() { |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Roughly increments the cache misses in the last hour by unique visitors |
||
| 228 | * @param WebRequest $request |
||
| 229 | * @return void |
||
| 230 | */ |
||
| 231 | public function incrMissesRecent( WebRequest $request ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Roughly gets the cache misses in the last hour by unique visitors |
||
| 263 | * @return int |
||
| 264 | */ |
||
| 265 | public function getMissesRecent() { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | protected function cacheMissKey() { |
||
| 277 | } |
||
| 278 |