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:
Complex classes like TransientCacheStorage 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 TransientCacheStorage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class TransientCacheStorage implements CacheStorageInterface |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * wp-option option_name for tracking transients |
||
| 24 | * |
||
| 25 | * @type string |
||
| 26 | */ |
||
| 27 | const TRANSIENT_SCHEDULE_OPTIONS_KEY = 'ee_transient_schedule'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var int $current_time |
||
| 31 | */ |
||
| 32 | private $current_time; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * how often to perform transient cleanup |
||
| 36 | * |
||
| 37 | * @var string $transient_cleanup_frequency |
||
| 38 | */ |
||
| 39 | private $transient_cleanup_frequency; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * options for how often to perform transient cleanup |
||
| 43 | * |
||
| 44 | * @var array $transient_cleanup_frequency_options |
||
| 45 | */ |
||
| 46 | private $transient_cleanup_frequency_options = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array $transients |
||
| 50 | */ |
||
| 51 | private $transients; |
||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | /** |
||
| 56 | * TransientCacheStorage constructor. |
||
| 57 | */ |
||
| 58 | public function __construct() |
||
| 59 | { |
||
| 60 | $this->transient_cleanup_frequency = $this->setTransientCleanupFrequency(); |
||
| 61 | // round current time down to closest 5 minutes to simplify scheduling |
||
| 62 | $this->current_time = $this->roundTimestamp(time(), '5-minutes', false); |
||
| 63 | $this->transients = (array)get_option(TransientCacheStorage::TRANSIENT_SCHEDULE_OPTIONS_KEY, array()); |
||
| 64 | if ( ! (defined('DOING_AJAX') && DOING_AJAX) && $this->transient_cleanup_frequency !== 'off') { |
||
| 65 | add_action('shutdown', array($this, 'checkTransientCleanupSchedule'), 999); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * Sets how often transient cleanup occurs |
||
| 73 | * |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | private function setTransientCleanupFrequency() |
||
| 77 | { |
||
| 78 | // sets how often transients are cleaned up |
||
| 79 | $this->transient_cleanup_frequency_options = apply_filters( |
||
| 80 | 'FHEE__TransientCacheStorage__transient_cleanup_schedule_options', |
||
| 81 | array( |
||
| 82 | 'off', |
||
| 83 | '15-minutes', |
||
| 84 | 'hour', |
||
| 85 | '12-hours', |
||
| 86 | 'day', |
||
| 87 | ) |
||
| 88 | ); |
||
| 89 | $transient_cleanup_frequency = apply_filters( |
||
| 90 | 'FHEE__TransientCacheStorage__transient_cleanup_schedule', |
||
| 91 | 'hour' |
||
| 92 | ); |
||
| 93 | return in_array( |
||
| 94 | $transient_cleanup_frequency, |
||
| 95 | $this->transient_cleanup_frequency_options, |
||
| 96 | true |
||
| 97 | ) |
||
| 98 | ? $transient_cleanup_frequency |
||
| 99 | : 'hour'; |
||
| 100 | } |
||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * we need to be able to round timestamps off to match the set transient cleanup frequency |
||
| 106 | * so if a transient is set to expire at 1:17 pm for example, and our cleanup schedule is every hour, |
||
| 107 | * then that timestamp needs to be rounded up to 2:00 pm so that it is removed |
||
| 108 | * during the next scheduled cleanup after its expiration. |
||
| 109 | * We also round off the current time timestamp to the closest 5 minutes |
||
| 110 | * just to make the timestamps a little easier to round which helps with debugging. |
||
| 111 | * |
||
| 112 | * @param int $timestamp [required] |
||
| 113 | * @param string $cleanup_frequency |
||
| 114 | * @param bool $round_up |
||
| 115 | * @return int |
||
| 116 | */ |
||
| 117 | private function roundTimestamp($timestamp, $cleanup_frequency = 'hour', $round_up = true) |
||
| 118 | { |
||
| 119 | $cleanup_frequency = $cleanup_frequency ? $cleanup_frequency : $this->transient_cleanup_frequency; |
||
| 120 | // in order to round the time to the closest xx minutes (or hours), |
||
| 121 | // we take the minutes (or hours) portion of the timestamp and divide it by xx, |
||
| 122 | // round down to a whole number, then multiply by xx to bring us almost back up to where we were |
||
| 123 | // why round down ? so the minutes (or hours) don't go over 60 (or 24) |
||
| 124 | // and bump the hour, which could bump the day, which could bump the month, etc, |
||
| 125 | // which would be bad because we don't always want to round up, |
||
| 126 | // but when we do we can easily achieve that by simply adding the desired offset, |
||
| 127 | $minutes = '00'; |
||
| 128 | $hours = 'H'; |
||
| 129 | switch ($cleanup_frequency) { |
||
| 130 | View Code Duplication | case '5-minutes' : |
|
| 131 | $minutes = floor((int)date('i', $timestamp) / 5) * 5; |
||
| 132 | $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
||
| 133 | $offset = MINUTE_IN_SECONDS * 5; |
||
| 134 | break; |
||
| 135 | View Code Duplication | case '15-minutes' : |
|
| 136 | $minutes = floor((int)date('i', $timestamp) / 15) * 15; |
||
| 137 | $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT); |
||
| 138 | $offset = MINUTE_IN_SECONDS * 15; |
||
| 139 | break; |
||
| 140 | View Code Duplication | case '12-hours' : |
|
| 141 | $hours = floor((int)date('H', $timestamp) / 12) * 12; |
||
| 142 | $hours = str_pad($hours, 2, '0', STR_PAD_LEFT); |
||
| 143 | $offset = HOUR_IN_SECONDS * 12; |
||
| 144 | break; |
||
| 145 | case 'day' : |
||
| 146 | $hours = '03'; // run cleanup at 3:00 am (or first site hit after that) |
||
| 147 | $offset = DAY_IN_SECONDS; |
||
| 148 | break; |
||
| 149 | case 'hour' : |
||
| 150 | default : |
||
| 151 | $offset = HOUR_IN_SECONDS; |
||
| 152 | break; |
||
| 153 | } |
||
| 154 | $rounded_timestamp = (int) strtotime(date("Y-m-d {$hours}:{$minutes}:00", $timestamp)); |
||
| 155 | $rounded_timestamp += $round_up ? $offset : 0; |
||
| 156 | return apply_filters( |
||
| 157 | 'FHEE__TransientCacheStorage__roundTimestamp__timestamp', |
||
| 158 | $rounded_timestamp, |
||
| 159 | $timestamp, |
||
| 160 | $cleanup_frequency, |
||
| 161 | $round_up |
||
| 162 | ); |
||
| 163 | } |
||
| 164 | |||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Saves supplied data to a transient |
||
| 169 | * if an expiration is set, then it automatically schedules the transient for cleanup |
||
| 170 | * |
||
| 171 | * @param string $transient_key [required] |
||
| 172 | * @param string $data [required] |
||
| 173 | * @param int $expiration number of seconds until the cache expires |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function add($transient_key, $data, $expiration = 0) |
||
| 177 | { |
||
| 178 | $expiration = (int)abs($expiration); |
||
| 179 | $saved = set_transient($transient_key, $data, $expiration); |
||
| 180 | if ($saved && $expiration) { |
||
| 181 | $this->scheduleTransientCleanup($transient_key, $expiration); |
||
| 182 | } |
||
| 183 | return $saved; |
||
| 184 | } |
||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | /** |
||
| 189 | * retrieves transient data |
||
| 190 | * automatically triggers early cache refresh for standard cache items |
||
| 191 | * in order to avoid cache stampedes on busy sites. |
||
| 192 | * For non-standard cache items like PHP Session data where early refreshing is not wanted, |
||
| 193 | * the $standard_cache parameter should be set to false when retrieving data |
||
| 194 | * |
||
| 195 | * @param string $transient_key [required] |
||
| 196 | * @param bool $standard_cache |
||
| 197 | * @return mixed|null |
||
| 198 | */ |
||
| 199 | public function get($transient_key, $standard_cache = true) |
||
| 200 | { |
||
| 201 | // to avoid cache stampedes (AKA:dogpiles) for standard cache items, |
||
| 202 | // check if known cache expires within the next minute, |
||
| 203 | // and if so, remove it from our tracking and and return nothing. |
||
| 204 | // this should trigger the cache content to be regenerated during this request, |
||
| 205 | // while allowing any following requests to still access the existing cache |
||
| 206 | // until it gets replaced with the refreshed content |
||
| 207 | if ( |
||
| 208 | $standard_cache |
||
| 209 | && isset($this->transients[$transient_key]) |
||
| 210 | && $this->transients[$transient_key] - time() <= MINUTE_IN_SECONDS |
||
| 211 | ) { |
||
| 212 | unset($this->transients[$transient_key]); |
||
| 213 | $this->updateTransients(); |
||
| 214 | return null; |
||
| 215 | } |
||
| 216 | $content = get_transient($transient_key); |
||
| 217 | return $content !== false ? $content : null; |
||
| 218 | } |
||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * delete a single transient and remove tracking |
||
| 224 | * |
||
| 225 | * @param string $transient_key [required] full or partial transient key to be deleted |
||
| 226 | */ |
||
| 227 | public function delete($transient_key) |
||
| 231 | |||
| 232 | |||
| 233 | |||
| 234 | /** |
||
| 235 | * delete multiple transients and remove tracking |
||
| 236 | * |
||
| 237 | * @param array $transient_keys [required] array of full or partial transient keys to be deleted |
||
| 238 | * @param bool $force_delete [optional] if true, then will not check incoming keys against those being tracked |
||
| 239 | * and proceed directly to deleting those entries from the cache storage |
||
| 240 | */ |
||
| 241 | public function deleteMany(array $transient_keys, $force_delete = false) |
||
| 242 | { |
||
| 243 | $full_transient_keys = $force_delete ? $transient_keys : array(); |
||
| 244 | if(empty($full_transient_keys)){ |
||
| 245 | foreach ($this->transients as $transient_key => $expiration) { |
||
| 246 | foreach ($transient_keys as $transient_key_to_delete) { |
||
| 247 | if (strpos($transient_key, $transient_key_to_delete) !== false) { |
||
| 248 | $full_transient_keys[] = $transient_key; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 | if ($this->deleteTransientKeys($full_transient_keys)) { |
||
| 254 | $this->updateTransients(); |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * sorts transients numerically by timestamp |
||
| 262 | * then saves the transient schedule to a WP option |
||
| 263 | */ |
||
| 264 | private function updateTransients() |
||
| 272 | |||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * schedules a transient for cleanup by adding it to the transient tracking |
||
| 277 | * |
||
| 278 | * @param string $transient_key [required] |
||
| 279 | * @param int $expiration [required] |
||
| 280 | */ |
||
| 281 | private function scheduleTransientCleanup($transient_key, $expiration) |
||
| 291 | |||
| 292 | |||
| 293 | |||
| 294 | /** |
||
| 295 | * Since our tracked transients are sorted by their timestamps |
||
| 296 | * we can grab the first transient and see when it is scheduled for cleanup. |
||
| 297 | * If that timestamp is less than or equal to the current time, |
||
| 298 | * then cleanup is triggered |
||
| 299 | */ |
||
| 300 | public function checkTransientCleanupSchedule() |
||
| 315 | |||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * loops through the array of tracked transients, |
||
| 320 | * compiles a list of those that have expired, and sends that list off for deletion. |
||
| 321 | * Also removes any bad records from the transients array |
||
| 322 | * |
||
| 323 | * @return bool |
||
| 324 | */ |
||
| 325 | private function cleanupExpiredTransients() |
||
| 355 | |||
| 356 | |||
| 357 | |||
| 358 | /** |
||
| 359 | * calls delete_transient() on each transient key provided, up to the specified limit |
||
| 360 | * |
||
| 361 | * @param array $transient_keys [required] |
||
| 362 | * @param int $limit |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | private function deleteTransientKeys(array $transient_keys, $limit = 50) |
||
| 389 | |||
| 390 | |||
| 391 | |||
| 392 | } |
||
| 393 | // End of file TransientCacheStorage.php |
||
| 395 |