1
|
|
|
<?php |
2
|
|
|
/* Copyright (C) NAVER <http://www.navercorp.com> */ |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* CacheHandler |
6
|
|
|
* |
7
|
|
|
* @author NAVER ([email protected]) |
8
|
|
|
*/ |
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) |
32
|
|
|
{ |
33
|
|
|
$cache_handler_key = $target . ($always_use_file ? '_file' : ''); |
34
|
|
|
if(!$GLOBALS['__XE_CACHE_HANDLER__'][$cache_handler_key]) |
35
|
|
|
{ |
36
|
|
|
$GLOBALS['__XE_CACHE_HANDLER__'][$cache_handler_key] = new CacheHandler($target, $info, $always_use_file); |
37
|
|
|
} |
38
|
|
|
return $GLOBALS['__XE_CACHE_HANDLER__'][$cache_handler_key]; |
39
|
|
|
} |
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() |
123
|
|
|
{ |
124
|
|
|
if($this->handler && $this->handler->isSupport()) |
125
|
|
|
{ |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return false; |
130
|
|
|
} |
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) |
139
|
|
|
{ |
140
|
|
|
$key = str_replace('/', ':', $key); |
141
|
|
|
|
142
|
|
|
return __XE_VERSION__ . ':' . $key; |
143
|
|
|
} |
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) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
if(!$this->handler) |
156
|
|
|
{ |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$key = $this->getCacheKey($key); |
161
|
|
|
|
162
|
|
|
return $this->handler->get($key, $modified_time); |
163
|
|
|
} |
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) |
176
|
|
|
{ |
177
|
|
|
if(!$this->handler && !$key) |
178
|
|
|
{ |
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$key = $this->getCacheKey($key); |
183
|
|
|
|
184
|
|
|
return $this->handler->put($key, $obj, $valid_time); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Delete Cache |
189
|
|
|
* |
190
|
|
|
* @param string $key Cache key |
191
|
|
|
* @return void |
192
|
|
|
*/ |
193
|
|
View Code Duplication |
function delete($key) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
if(!$this->handler) |
196
|
|
|
{ |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$key = $this->getCacheKey($key); |
201
|
|
|
|
202
|
|
|
return $this->handler->delete($key); |
|
|
|
|
203
|
|
|
} |
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) |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
if(!$this->handler) |
216
|
|
|
{ |
217
|
|
|
return false; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$key = $this->getCacheKey($key); |
221
|
|
|
|
222
|
|
|
return $this->handler->isValid($key, $modified_time); |
223
|
|
|
} |
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() |
231
|
|
|
{ |
232
|
|
|
if(!$this->handler) |
233
|
|
|
{ |
234
|
|
|
return false; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $this->handler->truncate(); |
238
|
|
|
} |
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) |
257
|
|
|
{ |
258
|
|
|
if(!$this->keyGroupVersions[$keyGroupName]) |
259
|
|
|
{ |
260
|
|
|
$this->keyGroupVersions[$keyGroupName] = 1; |
261
|
|
|
$this->handler->put('key_group_versions', $this->keyGroupVersions, 0); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return 'cache_group_' . $this->keyGroupVersions[$keyGroupName] . ':' . $keyGroupName . ':' . $key; |
265
|
|
|
} |
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) |
274
|
|
|
{ |
275
|
|
|
$this->keyGroupVersions[$keyGroupName]++; |
276
|
|
|
$this->handler->put('key_group_versions', $this->keyGroupVersions, 0); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Base class of Cache |
283
|
|
|
* |
284
|
|
|
* @author NAVER ([email protected]) |
285
|
|
|
*/ |
286
|
|
|
class CacheBase |
287
|
|
|
{ |
288
|
|
|
/** |
289
|
|
|
* Default valid time |
290
|
|
|
* @var int |
291
|
|
|
*/ |
292
|
|
|
var $valid_time = 36000; |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Get cached data |
296
|
|
|
* |
297
|
|
|
* @param string $key Cache key |
298
|
|
|
* @param int $modified_time Unix time of data modified. |
299
|
|
|
* If stored time is older then modified time, return false. |
300
|
|
|
* @return false|mixed Return false on failure or older then modified time. Return the string associated with the $key on success. |
301
|
|
|
*/ |
302
|
|
|
function get($key, $modified_time = 0) |
303
|
|
|
{ |
304
|
|
|
return false; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Put data into cache |
309
|
|
|
* |
310
|
|
|
* @param string $key Cache key |
311
|
|
|
* @param mixed $obj Value of a variable to store. $value supports all data types except resources, such as file handlers. |
312
|
|
|
* @param int $valid_time Time for the variable to live in the cache in seconds. |
313
|
|
|
* After the value specified in ttl has passed the stored variable will be deleted from the cache. |
314
|
|
|
* If no ttl is supplied, use the default valid time. |
315
|
|
|
* @return bool|void Returns true on success or false on failure. If use CacheFile, returns void. |
316
|
|
|
*/ |
317
|
|
|
function put($key, $obj, $valid_time = 0) |
318
|
|
|
{ |
319
|
|
|
return false; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Return whether cache is valid or invalid |
324
|
|
|
* |
325
|
|
|
* @param string $key Cache key |
326
|
|
|
* @param int $modified_time Unix time of data modified. |
327
|
|
|
* If stored time is older then modified time, the data is invalid. |
328
|
|
|
* @return bool Return true on valid or false on invalid. |
329
|
|
|
*/ |
330
|
|
|
function isValid($key, $modified_time = 0) |
331
|
|
|
{ |
332
|
|
|
return false; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Return whether support or not support cache |
337
|
|
|
* |
338
|
|
|
* @return boolean |
339
|
|
|
*/ |
340
|
|
|
function isSupport() |
341
|
|
|
{ |
342
|
|
|
return false; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Truncate all cache |
347
|
|
|
* |
348
|
|
|
* @return bool|void Returns true on success or false on failure. If use CacheFile, returns void. |
349
|
|
|
*/ |
350
|
|
|
function truncate() |
351
|
|
|
{ |
352
|
|
|
return false; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
} |
356
|
|
|
/* End of file CacheHandler.class.php */ |
357
|
|
|
/* Location: ./classes/cache/CacheHandler.class.php */ |
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.