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 |
||
9 | class CacheHandler extends Handler |
||
10 | { |
||
11 | /** |
||
12 | * instance of cache handler |
||
13 | * @var CacheBase |
||
14 | */ |
||
15 | var $handler = null; |
||
16 | |||
17 | /** |
||
18 | * Version of key group |
||
19 | * @var int |
||
20 | */ |
||
21 | var $keyGroupVersions = null; |
||
22 | |||
23 | /** |
||
24 | * Get a instance of CacheHandler(for singleton) |
||
25 | * |
||
26 | * @param string $target type of cache (object|template) |
||
27 | * @param object $info info. of DB |
||
28 | * @param boolean $always_use_file If set true, use a file cache always |
||
29 | * @return CacheHandler |
||
30 | */ |
||
31 | function &getInstance($target = 'object', $info = null, $always_use_file = false) |
||
40 | |||
41 | /** |
||
42 | * Constructor. |
||
43 | * |
||
44 | * Do not use this directly. You can use getInstance() instead. |
||
45 | * |
||
46 | * @see CacheHandler::getInstance |
||
47 | * @param string $target type of cache (object|template) |
||
48 | * @param object $info info. of DB |
||
49 | * @param boolean $always_use_file If set true, use a file cache always |
||
50 | * @return CacheHandler |
||
|
|||
51 | */ |
||
52 | function __construct($target, $info = null, $always_use_file = false) |
||
53 | { |
||
54 | if(!$info) |
||
55 | { |
||
56 | $info = Context::getDBInfo(); |
||
57 | } |
||
58 | |||
59 | if($info) |
||
60 | { |
||
61 | if($target == 'object') |
||
62 | { |
||
63 | if($info->use_object_cache == 'apc') |
||
64 | { |
||
65 | $type = 'apc'; |
||
66 | } |
||
67 | View Code Duplication | else if(substr($info->use_object_cache, 0, 8) == 'memcache') |
|
68 | { |
||
69 | $type = 'memcache'; |
||
70 | $url = $info->use_object_cache; |
||
71 | } |
||
72 | else if($info->use_object_cache == 'wincache') |
||
73 | { |
||
74 | $type = 'wincache'; |
||
75 | } |
||
76 | else if($info->use_object_cache == 'file') |
||
77 | { |
||
78 | $type = 'file'; |
||
79 | } |
||
80 | else if($always_use_file) |
||
81 | { |
||
82 | $type = 'file'; |
||
83 | } |
||
84 | } |
||
85 | View Code Duplication | else if($target == 'template') |
|
86 | { |
||
87 | if($info->use_template_cache == 'apc') |
||
88 | { |
||
89 | $type = 'apc'; |
||
90 | } |
||
91 | else if(substr($info->use_template_cache, 0, 8) == 'memcache') |
||
92 | { |
||
93 | $type = 'memcache'; |
||
94 | $url = $info->use_template_cache; |
||
95 | } |
||
96 | else if($info->use_template_cache == 'wincache') |
||
97 | { |
||
98 | $type = 'wincache'; |
||
99 | } |
||
100 | } |
||
101 | |||
102 | if($type) |
||
103 | { |
||
104 | $class = 'Cache' . ucfirst($type); |
||
105 | include_once sprintf('%sclasses/cache/%s.class.php', _XE_PATH_, $class); |
||
106 | $this->handler = call_user_func(array($class, 'getInstance'), $url); |
||
107 | $this->keyGroupVersions = $this->handler->get('key_group_versions', 0); |
||
108 | if(!$this->keyGroupVersions) |
||
109 | { |
||
110 | $this->keyGroupVersions = array(); |
||
111 | $this->handler->put('key_group_versions', $this->keyGroupVersions, 0); |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Return whether support or not support cache |
||
119 | * |
||
120 | * @return boolean |
||
121 | */ |
||
122 | function isSupport() |
||
131 | |||
132 | /** |
||
133 | * Get cache name by key |
||
134 | * |
||
135 | * @param string $key The key that will be associated with the item. |
||
136 | * @return string Returns cache name |
||
137 | */ |
||
138 | function getCacheKey($key) |
||
144 | |||
145 | /** |
||
146 | * Get cached data |
||
147 | * |
||
148 | * @param string $key Cache key |
||
149 | * @param int $modified_time Unix time of data modified. |
||
150 | * If stored time is older then modified time, return false. |
||
151 | * @return false|mixed Return false on failure or older then modified time. Return the string associated with the $key on success. |
||
152 | */ |
||
153 | View Code Duplication | function get($key, $modified_time = 0) |
|
164 | |||
165 | /** |
||
166 | * Put data into cache |
||
167 | * |
||
168 | * @param string $key Cache key |
||
169 | * @param mixed $obj Value of a variable to store. $value supports all data types except resources, such as file handlers. |
||
170 | * @param int $valid_time Time for the variable to live in the cache in seconds. |
||
171 | * After the value specified in ttl has passed the stored variable will be deleted from the cache. |
||
172 | * If no ttl is supplied, use the default valid time. |
||
173 | * @return bool|void Returns true on success or false on failure. If use CacheFile, returns void. |
||
174 | */ |
||
175 | function put($key, $obj, $valid_time = 0) |
||
186 | |||
187 | /** |
||
188 | * Delete Cache |
||
189 | * |
||
190 | * @param string $key Cache key |
||
191 | * @return void |
||
192 | */ |
||
193 | View Code Duplication | function delete($key) |
|
204 | |||
205 | /** |
||
206 | * Return whether cache is valid or invalid |
||
207 | * |
||
208 | * @param string $key Cache key |
||
209 | * @param int $modified_time Unix time of data modified. |
||
210 | * If stored time is older then modified time, the data is invalid. |
||
211 | * @return bool Return true on valid or false on invalid. |
||
212 | */ |
||
213 | View Code Duplication | function isValid($key, $modified_time = 0) |
|
224 | |||
225 | /** |
||
226 | * Truncate all cache |
||
227 | * |
||
228 | * @return bool|void Returns true on success or false on failure. If use CacheFile, returns void. |
||
229 | */ |
||
230 | function truncate() |
||
239 | |||
240 | /** |
||
241 | * Function used for generating keys for similar objects. |
||
242 | * |
||
243 | * Ex: 1:document:123 |
||
244 | * 1:document:777 |
||
245 | * |
||
246 | * This allows easily removing all object of type "document" |
||
247 | * from cache by simply invalidating the group key. |
||
248 | * |
||
249 | * The new key will be 2:document:123, thus forcing the document |
||
250 | * to be reloaded from the database. |
||
251 | * |
||
252 | * @param string $keyGroupName Group name |
||
253 | * @param string $key Cache key |
||
254 | * @return string |
||
255 | */ |
||
256 | function getGroupKey($keyGroupName, $key) |
||
266 | |||
267 | /** |
||
268 | * Make invalid group key (like delete group key) |
||
269 | * |
||
270 | * @param string $keyGroupName Group name |
||
271 | * @return void |
||
272 | */ |
||
273 | function invalidateGroupKey($keyGroupName) |
||
278 | |||
279 | } |
||
280 | |||
358 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.