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 |
||
| 6 | class RedisCache implements CacheInterface |
||
| 7 | { |
||
| 8 | |||
| 9 | private static $defaults = [ |
||
| 10 | 'persistent' => null, |
||
| 11 | 'dbIndex' => 0, |
||
| 12 | 'port' => 6379, |
||
| 13 | 'timeout' => 2.5, |
||
| 14 | 'persistentId' => null, |
||
| 15 | 'reconnectAttempt' => 100 |
||
| 16 | ]; |
||
| 17 | |||
| 18 | public $handler = null; |
||
| 19 | /** |
||
| 20 | * Connect to Memcached service |
||
| 21 | * |
||
| 22 | * @param array $config Configuration values that has bucket name and hosts' IP addresses |
||
| 23 | * |
||
| 24 | */ |
||
| 25 | public function __construct(array $config) |
||
| 37 | |||
| 38 | View Code Duplication | private function connect( array $redisConfig){ |
|
| 48 | |||
| 49 | View Code Duplication | private function persistentConnect( array $redisConfig){ |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Fetch a value from the cache. |
||
| 62 | * |
||
| 63 | * @param string $key The unique key of this item in the cache |
||
| 64 | * |
||
| 65 | * @return mixed The value of the item from the cache, or null in case of cache miss |
||
| 66 | */ |
||
| 67 | public function get($key) |
||
| 72 | /** |
||
| 73 | * Persist data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
||
| 74 | * |
||
| 75 | * @param string $key The key of the item to store |
||
| 76 | * @param mixed $value The value of the item to store |
||
| 77 | * @param null|integer|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and the driver supports TTL |
||
| 78 | * then the library may set a default value for it or let the driver take care of that. |
||
| 79 | * |
||
| 80 | * @return bool True on success and false on failure |
||
| 81 | */ |
||
| 82 | public function set($key, $value, $ttl = null){ |
||
| 89 | /** |
||
| 90 | * Delete an item from the cache by its unique key |
||
| 91 | * |
||
| 92 | * @param string $key The unique cache key of the item to delete |
||
| 93 | * |
||
| 94 | * @return bool True on success and false on failure |
||
| 95 | */ |
||
| 96 | public function delete($key){ |
||
| 99 | /** |
||
| 100 | * Wipe clean the entire cache's keys |
||
| 101 | * |
||
| 102 | * @return bool True on success and false on failure |
||
| 103 | */ |
||
| 104 | public function clear(){ |
||
| 107 | /** |
||
| 108 | * Obtain multiple cache items by their unique keys |
||
| 109 | * |
||
| 110 | * @param array|Traversable $keys A list of keys that can obtained in a single operation. |
||
| 111 | * |
||
| 112 | * @return array An array of key => value pairs. Cache keys that do not exist or are stale will have a value of null. |
||
| 113 | */ |
||
| 114 | public function getMultiple($keys) |
||
| 118 | /** |
||
| 119 | * Persisting a set of key => value pairs in the cache, with an optional TTL. |
||
| 120 | * |
||
| 121 | * @param array|Traversable $items An array of key => value pairs for a multiple-set operation. |
||
| 122 | * @param null|integer|DateInterval $ttl Optional. The amount of seconds from the current time that the item will exist in the cache for. |
||
| 123 | * If this is null then the cache backend will fall back to its own default behaviour. |
||
| 124 | * |
||
| 125 | * @return bool True on success and false on failure |
||
| 126 | */ |
||
| 127 | public function setMultiple($items, $ttl = null) |
||
| 139 | /** |
||
| 140 | * Delete multiple cache items in a single operation |
||
| 141 | * |
||
| 142 | * @param array|Traversable $keys The array of string-based keys to be deleted |
||
| 143 | * |
||
| 144 | * @return bool True on success and false on failure |
||
| 145 | */ |
||
| 146 | public function deleteMultiple($keys) |
||
| 154 | /** |
||
| 155 | * Increment a value atomically in the cache by its step value, which defaults to 1 |
||
| 156 | * |
||
| 157 | * @param string $key The cache item key |
||
| 158 | * @param integer $step The value to increment by, defaulting to 1 |
||
| 159 | * |
||
| 160 | * @return int|bool The new value on success and false on failure |
||
| 161 | */ |
||
| 162 | public function increment($key, $step = 1) |
||
| 166 | /** |
||
| 167 | * Decrement a value atomically in the cache by its step value, which defaults to 1 |
||
| 168 | * |
||
| 169 | * @param string $key The cache item key |
||
| 170 | * @param integer $step The value to decrement by, defaulting to 1 |
||
| 171 | * |
||
| 172 | * @return int|bool The new value on success and false on failure |
||
| 173 | */ |
||
| 174 | public function decrement($key, $step = 1) |
||
| 178 | } |