Complex classes like CacheMemcached 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 CacheMemcached, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class CacheMemcached extends CacheRAM { |
||
19 | /** |
||
20 | * The memcache var |
||
21 | * @var \Memcached |
||
22 | */ |
||
23 | private $memcached; |
||
24 | |||
25 | private $fetches = 0; /// number of times we had to hit memcache directly |
||
26 | |||
27 | /** |
||
28 | * Cache constructor (this is a singleton). |
||
29 | * |
||
30 | * @param $serves optional. If present, addServers() is called with this parameter |
||
31 | * but only if the singleton is being created for the first time. |
||
32 | * @return Cache The cache singleton. |
||
33 | * @codeCoverageIgnore |
||
34 | */ |
||
35 | static public function singleton($servers = []) { |
||
36 | static $instances; |
||
37 | if (!isset($instances)) { |
||
38 | $instances = new CacheMemcached(); |
||
39 | if (count($servers)) { |
||
40 | $instances->addServers($servers); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | return $instances; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Is memcached available in this system? |
||
49 | * |
||
50 | * @return boolean |
||
51 | */ |
||
52 | static public function hasMemcachedExt() { |
||
53 | return extension_loaded('memcached'); |
||
54 | } |
||
55 | |||
56 | // @codeCoverageIgnoreStart |
||
57 | // Prevent users to clone the instance |
||
58 | public function __clone() { |
||
59 | trigger_error('Cloning is not allowed.', LH_TRIGGER_UNEXPECTED); |
||
60 | } |
||
61 | // @codeCoverageIgnoreEnd |
||
62 | |||
63 | /** |
||
64 | * @codeCoverageIgnore |
||
65 | */ |
||
66 | public function errorCallback() { |
||
67 | // memcache error, probably offline. Logging to DB is bad (will overflow |
||
68 | // the DB). We should really restart memcached |
||
69 | // TODO: via Batch? |
||
70 | $this->disable(); |
||
71 | } |
||
72 | |||
73 | public function getFetches() { |
||
74 | return []; // TODO |
||
75 | foreach ($data as $item) { |
||
76 | $x = unserialize($item['keys']); |
||
77 | if ($x === false) { |
||
78 | continue; |
||
79 | } |
||
80 | |||
81 | parent::store($x, $item); |
||
82 | } |
||
83 | |||
84 | return $this->fetches; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Constructor. |
||
89 | * @codeCoverageIgnore |
||
90 | */ |
||
91 | private function __construct() { |
||
92 | if (!self::hasMemcachedExt()) { |
||
93 | $this->disable(); |
||
94 | return; |
||
95 | } |
||
96 | $this->memcached = new \Memcached; |
||
97 | if (!$this->memcached) { |
||
98 | $this->disable(); |
||
99 | return; |
||
100 | } |
||
101 | |||
102 | if (\Memcached::HAVE_IGBINARY) { |
||
103 | $this->memcached->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_IGBINARY); |
||
104 | } |
||
105 | $this->memcached->setOption(\Memcached::OPT_BINARY_PROTOCOL, true); |
||
106 | $this->lifetime = 3600; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * |
||
111 | * @param array $servers Each entry in servers is supposed to be an array |
||
112 | * containing hostname, port, and, optionally, weight of the server. |
||
113 | * $servers = array( |
||
114 | * array('mem1.domain.com', 11211, 33), |
||
115 | * array('mem2.domain.com', 11211, 67) |
||
116 | * ); |
||
117 | * @return boolean |
||
118 | * @codeCoverageIgnore |
||
119 | */ |
||
120 | public function addServers($servers) { |
||
121 | if (!self::hasMemcachedExt()) { |
||
122 | return false; |
||
123 | } |
||
124 | return $this->memcached->addServers($servers); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Converts cachekey to a string for the data group. |
||
129 | * |
||
130 | * @param CacheKey $k |
||
131 | * @return string |
||
132 | */ |
||
133 | private function getGroupString(CacheKey $k) { |
||
134 | return md5(strtr($this->namespace . $k->getBase() . $k->getId(), ' ', '_')); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * (non-PHPdoc) |
||
139 | * @see \Cachearium\Backend\CacheRAM::hashKey() |
||
140 | */ |
||
141 | protected function hashKey(CacheKey $k) { |
||
142 | $group = $this->getGroupString($k); |
||
143 | $ns_key = $this->memcached->get($group); |
||
144 | |||
145 | // if not set, initialize it |
||
146 | if ($ns_key == false) { |
||
147 | $ns_key = 1; |
||
148 | $this->memcached->set($group, $ns_key); |
||
149 | } |
||
150 | $group = $group . $ns_key; |
||
151 | |||
152 | if (!is_string($k->sub)) { |
||
153 | $sub = md5(serialize($k->sub)); |
||
154 | } |
||
155 | else { |
||
156 | $sub = $k->sub; |
||
157 | } |
||
158 | $group .= $sub; |
||
159 | |||
160 | return $group; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * (non-PHPdoc) |
||
165 | * @see \Cachearium\Backend\CacheRAM::get() |
||
166 | */ |
||
167 | public function get(CacheKey $k) { |
||
209 | |||
210 | /** |
||
211 | * (non-PHPdoc) |
||
212 | * @see \Cachearium\Backend\CacheRAM::increment() |
||
213 | */ |
||
214 | public function increment($value, CacheKey $k, $default = 0) { |
||
215 | // @codeCoverageIgnoreStart |
||
216 | if (!$this->enabled) { |
||
217 | return $default; |
||
218 | } |
||
219 | // @codeCoverageIgnoreEnd |
||
220 | |||
221 | $group = $this->hashKey($k); |
||
222 | |||
223 | $this->log(CacheLogEnum::SAVED, $k); |
||
224 | |||
225 | $x = $this->memcached->increment( |
||
226 | $group, $value, $default, $this->lifetime |
||
227 | ); |
||
228 | parent::store($x, $k, $this->lifetime); |
||
229 | |||
230 | return $x; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * (non-PHPdoc) |
||
235 | * @see \Cachearium\Backend\CacheRAM::store() |
||
236 | */ |
||
237 | public function store($data, CacheKey $k, $lifetime = 0) { |
||
238 | // @codeCoverageIgnoreStart |
||
239 | if (!$this->enabled) { |
||
240 | return false; |
||
241 | } |
||
242 | // @codeCoverageIgnoreEnd |
||
243 | |||
244 | $group = $this->hashKey($k); |
||
245 | |||
246 | $this->log(CacheLogEnum::SAVED, $k); |
||
247 | |||
248 | $x = $this->memcached->set( |
||
249 | $group, serialize($data), $lifetime ? $lifetime : $this->lifetime |
||
250 | ); |
||
251 | parent::store($data, $k, $lifetime); |
||
252 | |||
253 | return $x; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * (non-PHPdoc) |
||
258 | * @see \Cachearium\Backend\CacheRAM::delete() |
||
259 | */ |
||
260 | public function delete(CacheKey $k) { |
||
274 | |||
275 | /** |
||
276 | * (non-PHPdoc) |
||
277 | * @see \Cachearium\Backend\CacheRAM::cleanP() |
||
278 | */ |
||
279 | public function cleanP($base, $id) { |
||
280 | // @codeCoverageIgnoreStart |
||
281 | if (!$this->enabled) { |
||
282 | throw new NotCachedException(); |
||
283 | } |
||
284 | // @codeCoverageIgnoreEnd |
||
285 | |||
292 | |||
293 | /** |
||
294 | * (non-PHPdoc) |
||
295 | * @see \Cachearium\Backend\CacheRAM::clear() |
||
296 | */ |
||
297 | public function clear() { |
||
304 | |||
305 | public function prefetchKeys($keys) { |
||
310 | |||
311 | /** |
||
312 | * (non-PHPdoc) |
||
313 | * @see \Cachearium\Backend\CacheRAM::prefetch() |
||
314 | */ |
||
315 | public function prefetch($data) { |
||
326 | |||
327 | /** |
||
328 | * Clear prefetched data. This is rarely useful. |
||
329 | */ |
||
330 | public function prefetchClear() { |
||
333 | |||
334 | /** |
||
335 | * (non-PHPdoc) |
||
336 | * @see \Cachearium\Backend\CacheRAM::report() |
||
337 | * @codeCoverageIgnore |
||
338 | */ |
||
339 | public function report() { |
||
370 | } |
||
371 |