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 |
||
7 | class RedisCache implements CacheInterface |
||
8 | { |
||
9 | |||
10 | private static $defaults = [ |
||
11 | 'persistent' => null, |
||
12 | 'bucket' => 'default', |
||
13 | 'dbIndex' => 0, |
||
14 | 'port' => 6379, |
||
15 | 'timeout' => 2.5, |
||
16 | 'persistentId' => null, |
||
17 | 'reconnectAttempt' => 100 |
||
18 | ]; |
||
19 | |||
20 | private $serializer = Redis::SERIALIZER_PHP; |
||
21 | |||
22 | public $handler = null; |
||
23 | /** |
||
24 | * Connect to Redis service |
||
25 | * |
||
26 | * @param array $config Configuration values that has dbIndex name and host's IP address |
||
27 | * |
||
28 | */ |
||
29 | public function __construct(array $config) |
||
44 | |||
45 | View Code Duplication | private function connect( array $redisConfig){ |
|
55 | |||
56 | View Code Duplication | private function persistentConnect( array $redisConfig){ |
|
66 | |||
67 | /** |
||
68 | * Fetch a value from the cache. |
||
69 | * |
||
70 | * @param string $key The unique key of this item in the cache |
||
71 | * |
||
72 | * @return mixed The value of the item from the cache, or null in case of cache miss |
||
73 | */ |
||
74 | public function get($key) |
||
79 | /** |
||
80 | * Persist data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
||
81 | * |
||
82 | * @param string $key The key of the item to store |
||
83 | * @param mixed $value The value of the item to store |
||
84 | * @param null|integer|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and the driver supports TTL |
||
85 | * then the library may set a default value for it or let the driver take care of that. |
||
86 | * |
||
87 | * @return bool True on success and false on failure |
||
88 | */ |
||
89 | public function set($key, $value, $ttl = null){ |
||
97 | |||
98 | private function serialize($value){ |
||
101 | |||
102 | private function unserialize($value){ |
||
105 | /** |
||
106 | * Delete an item from the cache by its unique key |
||
107 | * |
||
108 | * @param string $key The unique cache key of the item to delete |
||
109 | * |
||
110 | * @return bool True on success and false on failure |
||
111 | */ |
||
112 | public function delete($key){ |
||
115 | /** |
||
116 | * Wipe clean the entire cache's keys |
||
117 | * |
||
118 | * @return bool True on success and false on failure |
||
119 | */ |
||
120 | public function clear(){ |
||
123 | /** |
||
124 | * Obtain multiple cache items by their unique keys |
||
125 | * |
||
126 | * @param array|Traversable $keys A list of keys that can obtained in a single operation. |
||
127 | * |
||
128 | * @return array An array of key => value pairs. Cache keys that do not exist or are stale will have a value of null. |
||
129 | */ |
||
130 | public function getMultiple($keys) |
||
134 | /** |
||
135 | * Persisting a set of key => value pairs in the cache, with an optional TTL. |
||
136 | * |
||
137 | * @param array|Traversable $items An array of key => value pairs for a multiple-set operation. |
||
138 | * @param null|integer|DateInterval $ttl Optional. The amount of seconds from the current time that the item will exist in the cache for. |
||
139 | * If this is null then the cache backend will fall back to its own default behaviour. |
||
140 | * |
||
141 | * @return bool True on success and false on failure |
||
142 | */ |
||
143 | public function setMultiple($items, $ttl = null) |
||
155 | /** |
||
156 | * Delete multiple cache items in a single operation |
||
157 | * |
||
158 | * @param array|Traversable $keys The array of string-based keys to be deleted |
||
159 | * |
||
160 | * @return bool True on success and false on failure |
||
161 | */ |
||
162 | public function deleteMultiple($keys) |
||
170 | /** |
||
171 | * Increment a value atomically in the cache by its step value, which defaults to 1 |
||
172 | * |
||
173 | * @param string $key The cache item key |
||
174 | * @param integer $step The value to increment by, defaulting to 1 |
||
175 | * |
||
176 | * @return int|bool The new value on success and false on failure |
||
177 | */ |
||
178 | public function increment($key, $step = 1) |
||
182 | /** |
||
183 | * Decrement a value atomically in the cache by its step value, which defaults to 1 |
||
184 | * |
||
185 | * @param string $key The cache item key |
||
186 | * @param integer $step The value to decrement by, defaulting to 1 |
||
187 | * |
||
188 | * @return int|bool The new value on success and false on failure |
||
189 | */ |
||
190 | public function decrement($key, $step = 1) |
||
194 | } |