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 BagOStuff 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 BagOStuff, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface { |
||
| 46 | /** @var array[] Lock tracking */ |
||
| 47 | protected $locks = []; |
||
| 48 | |||
| 49 | /** @var integer ERR_* class constant */ |
||
| 50 | protected $lastError = self::ERR_NONE; |
||
| 51 | |||
| 52 | /** @var string */ |
||
| 53 | protected $keyspace = 'local'; |
||
| 54 | |||
| 55 | /** @var LoggerInterface */ |
||
| 56 | protected $logger; |
||
| 57 | |||
| 58 | /** @var callback|null */ |
||
| 59 | protected $asyncHandler; |
||
| 60 | |||
| 61 | /** @var bool */ |
||
| 62 | private $debugMode = false; |
||
| 63 | |||
| 64 | /** @var array */ |
||
| 65 | private $duplicateKeyLookups = []; |
||
| 66 | |||
| 67 | /** @var bool */ |
||
| 68 | private $reportDupes = false; |
||
| 69 | |||
| 70 | /** @var bool */ |
||
| 71 | private $dupeTrackScheduled = false; |
||
| 72 | |||
| 73 | /** @var integer[] Map of (ATTR_* class constant => QOS_* class constant) */ |
||
| 74 | protected $attrMap = []; |
||
| 75 | |||
| 76 | /** Possible values for getLastError() */ |
||
| 77 | const ERR_NONE = 0; // no error |
||
| 78 | const ERR_NO_RESPONSE = 1; // no response |
||
| 79 | const ERR_UNREACHABLE = 2; // can't connect |
||
| 80 | const ERR_UNEXPECTED = 3; // response gave some error |
||
| 81 | |||
| 82 | /** Bitfield constants for get()/getMulti() */ |
||
| 83 | const READ_LATEST = 1; // use latest data for replicated stores |
||
| 84 | const READ_VERIFIED = 2; // promise that caller can tell when keys are stale |
||
| 85 | /** Bitfield constants for set()/merge() */ |
||
| 86 | const WRITE_SYNC = 1; // synchronously write to all locations for replicated stores |
||
| 87 | const WRITE_CACHE_ONLY = 2; // Only change state of the in-memory cache |
||
| 88 | |||
| 89 | /** |
||
| 90 | * $params include: |
||
| 91 | * - logger: Psr\Log\LoggerInterface instance |
||
| 92 | * - keyspace: Default keyspace for $this->makeKey() |
||
| 93 | * - asyncHandler: Callable to use for scheduling tasks after the web request ends. |
||
| 94 | * In CLI mode, it should run the task immediately. |
||
| 95 | * - reportDupes: Whether to emit warning log messages for all keys that were |
||
| 96 | * requested more than once (requires an asyncHandler). |
||
| 97 | * @param array $params |
||
| 98 | */ |
||
| 99 | public function __construct( array $params = [] ) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param LoggerInterface $logger |
||
| 121 | * @return null |
||
| 122 | */ |
||
| 123 | public function setLogger( LoggerInterface $logger ) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param bool $bool |
||
| 129 | */ |
||
| 130 | public function setDebug( $bool ) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get an item with the given key, regenerating and setting it if not found |
||
| 136 | * |
||
| 137 | * If the callback returns false, then nothing is stored. |
||
| 138 | * |
||
| 139 | * @param string $key |
||
| 140 | * @param int $ttl Time-to-live (seconds) |
||
| 141 | * @param callable $callback Callback that derives the new value |
||
| 142 | * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional] |
||
| 143 | * @return mixed The cached value if found or the result of $callback otherwise |
||
| 144 | * @since 1.27 |
||
| 145 | */ |
||
| 146 | final public function getWithSetCallback( $key, $ttl, $callback, $flags = 0 ) { |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get an item with the given key |
||
| 164 | * |
||
| 165 | * If the key includes a determistic input hash (e.g. the key can only have |
||
| 166 | * the correct value) or complete staleness checks are handled by the caller |
||
| 167 | * (e.g. nothing relies on the TTL), then the READ_VERIFIED flag should be set. |
||
| 168 | * This lets tiered backends know they can safely upgrade a cached value to |
||
| 169 | * higher tiers using standard TTLs. |
||
| 170 | * |
||
| 171 | * @param string $key |
||
| 172 | * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional] |
||
| 173 | * @param integer $oldFlags [unused] |
||
| 174 | * @return mixed Returns false on failure and if the item does not exist |
||
| 175 | */ |
||
| 176 | public function get( $key, $flags = 0, $oldFlags = null ) { |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Track the number of times that a given key has been used. |
||
| 187 | * @param string $key |
||
| 188 | */ |
||
| 189 | private function trackDuplicateKeys( $key ) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $key |
||
| 220 | * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional] |
||
| 221 | * @return mixed Returns false on failure and if the item does not exist |
||
| 222 | */ |
||
| 223 | abstract protected function doGet( $key, $flags = 0 ); |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @note: This method is only needed if merge() uses mergeViaCas() |
||
| 227 | * |
||
| 228 | * @param string $key |
||
| 229 | * @param mixed $casToken |
||
| 230 | * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional] |
||
| 231 | * @return mixed Returns false on failure and if the item does not exist |
||
| 232 | * @throws Exception |
||
| 233 | */ |
||
| 234 | protected function getWithToken( $key, &$casToken, $flags = 0 ) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Set an item |
||
| 240 | * |
||
| 241 | * @param string $key |
||
| 242 | * @param mixed $value |
||
| 243 | * @param int $exptime Either an interval in seconds or a unix timestamp for expiry |
||
| 244 | * @param int $flags Bitfield of BagOStuff::WRITE_* constants |
||
| 245 | * @return bool Success |
||
| 246 | */ |
||
| 247 | abstract public function set( $key, $value, $exptime = 0, $flags = 0 ); |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Delete an item |
||
| 251 | * |
||
| 252 | * @param string $key |
||
| 253 | * @return bool True if the item was deleted or not found, false on failure |
||
| 254 | */ |
||
| 255 | abstract public function delete( $key ); |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Merge changes into the existing cache value (possibly creating a new one) |
||
| 259 | * |
||
| 260 | * The callback function returns the new value given the current value |
||
| 261 | * (which will be false if not present), and takes the arguments: |
||
| 262 | * (this BagOStuff, cache key, current value, TTL). |
||
| 263 | * The TTL parameter is reference set to $exptime. It can be overriden in the callback. |
||
| 264 | * |
||
| 265 | * @param string $key |
||
| 266 | * @param callable $callback Callback method to be executed |
||
| 267 | * @param int $exptime Either an interval in seconds or a unix timestamp for expiry |
||
| 268 | * @param int $attempts The amount of times to attempt a merge in case of failure |
||
| 269 | * @param int $flags Bitfield of BagOStuff::WRITE_* constants |
||
| 270 | * @return bool Success |
||
| 271 | * @throws InvalidArgumentException |
||
| 272 | */ |
||
| 273 | public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @see BagOStuff::merge() |
||
| 279 | * |
||
| 280 | * @param string $key |
||
| 281 | * @param callable $callback Callback method to be executed |
||
| 282 | * @param int $exptime Either an interval in seconds or a unix timestamp for expiry |
||
| 283 | * @param int $attempts The amount of times to attempt a merge in case of failure |
||
| 284 | * @return bool Success |
||
| 285 | */ |
||
| 286 | protected function mergeViaCas( $key, $callback, $exptime = 0, $attempts = 10 ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Check and set an item |
||
| 322 | * |
||
| 323 | * @param mixed $casToken |
||
| 324 | * @param string $key |
||
| 325 | * @param mixed $value |
||
| 326 | * @param int $exptime Either an interval in seconds or a unix timestamp for expiry |
||
| 327 | * @return bool Success |
||
| 328 | * @throws Exception |
||
| 329 | */ |
||
| 330 | protected function cas( $casToken, $key, $value, $exptime = 0 ) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @see BagOStuff::merge() |
||
| 336 | * |
||
| 337 | * @param string $key |
||
| 338 | * @param callable $callback Callback method to be executed |
||
| 339 | * @param int $exptime Either an interval in seconds or a unix timestamp for expiry |
||
| 340 | * @param int $attempts The amount of times to attempt a merge in case of failure |
||
| 341 | * @param int $flags Bitfield of BagOStuff::WRITE_* constants |
||
| 342 | * @return bool Success |
||
| 343 | */ |
||
| 344 | protected function mergeViaLock( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Acquire an advisory lock on a key string |
||
| 377 | * |
||
| 378 | * Note that if reentry is enabled, duplicate calls ignore $expiry |
||
| 379 | * |
||
| 380 | * @param string $key |
||
| 381 | * @param int $timeout Lock wait timeout; 0 for non-blocking [optional] |
||
| 382 | * @param int $expiry Lock expiry [optional]; 1 day maximum |
||
| 383 | * @param string $rclass Allow reentry if set and the current lock used this value |
||
| 384 | * @return bool Success |
||
| 385 | */ |
||
| 386 | public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Release an advisory lock on a key string |
||
| 436 | * |
||
| 437 | * @param string $key |
||
| 438 | * @return bool Success |
||
| 439 | */ |
||
| 440 | public function unlock( $key ) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Get a lightweight exclusive self-unlocking lock |
||
| 452 | * |
||
| 453 | * Note that the same lock cannot be acquired twice. |
||
| 454 | * |
||
| 455 | * This is useful for task de-duplication or to avoid obtrusive |
||
| 456 | * (though non-corrupting) DB errors like INSERT key conflicts |
||
| 457 | * or deadlocks when using LOCK IN SHARE MODE. |
||
| 458 | * |
||
| 459 | * @param string $key |
||
| 460 | * @param int $timeout Lock wait timeout; 0 for non-blocking [optional] |
||
| 461 | * @param int $expiry Lock expiry [optional]; 1 day maximum |
||
| 462 | * @param string $rclass Allow reentry if set and the current lock used this value |
||
| 463 | * @return ScopedCallback|null Returns null on failure |
||
| 464 | * @since 1.26 |
||
| 465 | */ |
||
| 466 | final public function getScopedLock( $key, $timeout = 6, $expiry = 30, $rclass = '' ) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Delete all objects expiring before a certain date. |
||
| 488 | * @param string $date The reference date in MW format |
||
| 489 | * @param callable|bool $progressCallback Optional, a function which will be called |
||
| 490 | * regularly during long-running operations with the percentage progress |
||
| 491 | * as the first parameter. |
||
| 492 | * |
||
| 493 | * @return bool Success, false if unimplemented |
||
| 494 | */ |
||
| 495 | public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) { |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Get an associative array containing the item for each of the keys that have items. |
||
| 502 | * @param array $keys List of strings |
||
| 503 | * @param integer $flags Bitfield; supports READ_LATEST [optional] |
||
| 504 | * @return array |
||
| 505 | */ |
||
| 506 | public function getMulti( array $keys, $flags = 0 ) { |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Batch insertion |
||
| 519 | * @param array $data $key => $value assoc array |
||
| 520 | * @param int $exptime Either an interval in seconds or a unix timestamp for expiry |
||
| 521 | * @return bool Success |
||
| 522 | * @since 1.24 |
||
| 523 | */ |
||
| 524 | public function setMulti( array $data, $exptime = 0 ) { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @param string $key |
||
| 536 | * @param mixed $value |
||
| 537 | * @param int $exptime |
||
| 538 | * @return bool Success |
||
| 539 | */ |
||
| 540 | public function add( $key, $value, $exptime = 0 ) { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Increase stored value of $key by $value while preserving its TTL |
||
| 549 | * @param string $key Key to increase |
||
| 550 | * @param int $value Value to add to $key (Default 1) |
||
| 551 | * @return int|bool New value or false on failure |
||
| 552 | */ |
||
| 553 | public function incr( $key, $value = 1 ) { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Decrease stored value of $key by $value while preserving its TTL |
||
| 571 | * @param string $key |
||
| 572 | * @param int $value |
||
| 573 | * @return int|bool New value or false on failure |
||
| 574 | */ |
||
| 575 | public function decr( $key, $value = 1 ) { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Increase stored value of $key by $value while preserving its TTL |
||
| 581 | * |
||
| 582 | * This will create the key with value $init and TTL $ttl instead if not present |
||
| 583 | * |
||
| 584 | * @param string $key |
||
| 585 | * @param int $ttl |
||
| 586 | * @param int $value |
||
| 587 | * @param int $init |
||
| 588 | * @return int|bool New value or false on failure |
||
| 589 | * @since 1.24 |
||
| 590 | */ |
||
| 591 | public function incrWithInit( $key, $ttl, $value = 1, $init = 1 ) { |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Get the "last error" registered; clearLastError() should be called manually |
||
| 607 | * @return int ERR_* constant for the "last error" registry |
||
| 608 | * @since 1.23 |
||
| 609 | */ |
||
| 610 | public function getLastError() { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Clear the "last error" registry |
||
| 616 | * @since 1.23 |
||
| 617 | */ |
||
| 618 | public function clearLastError() { |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Set the "last error" registry |
||
| 624 | * @param int $err ERR_* constant |
||
| 625 | * @since 1.23 |
||
| 626 | */ |
||
| 627 | protected function setLastError( $err ) { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Modify a cache update operation array for EventRelayer::notify() |
||
| 633 | * |
||
| 634 | * This is used for relayed writes, e.g. for broadcasting a change |
||
| 635 | * to multiple data-centers. If the array contains a 'val' field |
||
| 636 | * then the command involves setting a key to that value. Note that |
||
| 637 | * for simplicity, 'val' is always a simple scalar value. This method |
||
| 638 | * is used to possibly serialize the value and add any cache-specific |
||
| 639 | * key/values needed for the relayer daemon (e.g. memcached flags). |
||
| 640 | * |
||
| 641 | * @param array $event |
||
| 642 | * @return array |
||
| 643 | * @since 1.26 |
||
| 644 | */ |
||
| 645 | public function modifySimpleRelayEvent( array $event ) { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @param string $text |
||
| 651 | */ |
||
| 652 | protected function debug( $text ) { |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Convert an optionally relative time to an absolute time |
||
| 662 | * @param int $exptime |
||
| 663 | * @return int |
||
| 664 | */ |
||
| 665 | protected function convertExpiry( $exptime ) { |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Convert an optionally absolute expiry time to a relative time. If an |
||
| 675 | * absolute time is specified which is in the past, use a short expiry time. |
||
| 676 | * |
||
| 677 | * @param int $exptime |
||
| 678 | * @return int |
||
| 679 | */ |
||
| 680 | protected function convertToRelative( $exptime ) { |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Check if a value is an integer |
||
| 694 | * |
||
| 695 | * @param mixed $value |
||
| 696 | * @return bool |
||
| 697 | */ |
||
| 698 | protected function isInteger( $value ) { |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Construct a cache key. |
||
| 704 | * |
||
| 705 | * @since 1.27 |
||
| 706 | * @param string $keyspace |
||
| 707 | * @param array $args |
||
| 708 | * @return string |
||
| 709 | */ |
||
| 710 | public function makeKeyInternal( $keyspace, $args ) { |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Make a global cache key. |
||
| 721 | * |
||
| 722 | * @since 1.27 |
||
| 723 | * @param string ... Key component (variadic) |
||
| 724 | * @return string |
||
| 725 | */ |
||
| 726 | public function makeGlobalKey() { |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Make a cache key, scoped to this instance's keyspace. |
||
| 732 | * |
||
| 733 | * @since 1.27 |
||
| 734 | * @param string ... Key component (variadic) |
||
| 735 | * @return string |
||
| 736 | */ |
||
| 737 | public function makeKey() { |
||
| 740 | |||
| 741 | /** |
||
| 742 | * @param integer $flag ATTR_* class constant |
||
| 743 | * @return integer QOS_* class constant |
||
| 744 | * @since 1.28 |
||
| 745 | */ |
||
| 746 | public function getQoS( $flag ) { |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Merge the flag maps of one or more BagOStuff objects into a "lowest common denominator" map |
||
| 752 | * |
||
| 753 | * @param BagOStuff[] $bags |
||
| 754 | * @return integer[] Resulting flag map (class ATTR_* constant => class QOS_* constant) |
||
| 755 | */ |
||
| 756 | protected function mergeFlagMaps( array $bags ) { |
||
| 770 | } |
||
| 771 |