1
|
|
|
<?php |
2
|
|
|
/* Copyright (C) NAVER <http://www.navercorp.com> */ |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Cache class for memcache |
6
|
|
|
* |
7
|
|
|
* @author NAVER ([email protected]) |
8
|
|
|
*/ |
9
|
|
|
class CacheMemcache extends CacheBase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* instance of Memcahe |
13
|
|
|
* @var Memcahe |
14
|
|
|
*/ |
15
|
|
|
var $Memcache; |
16
|
|
|
var $SelectedExtension; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get instance of CacheMemcache |
20
|
|
|
* |
21
|
|
|
* @param string $url url of memcache |
22
|
|
|
* @return CacheMemcache instance of CacheMemcache |
23
|
|
|
*/ |
24
|
|
|
function getInstance($url) |
25
|
|
|
{ |
26
|
|
|
if(!$GLOBALS['__CacheMemcache__']) |
27
|
|
|
{ |
28
|
|
|
$GLOBALS['__CacheMemcache__'] = new CacheMemcache($url); |
29
|
|
|
} |
30
|
|
|
return $GLOBALS['__CacheMemcache__']; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Construct |
35
|
|
|
* |
36
|
|
|
* Do not use this directly. You can use getInstance() instead. |
37
|
|
|
* @param string $url url of memcache |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
function CacheMemcache($url) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
//$config['url'] = array('memcache://localhost:11211'); |
43
|
|
|
$config['url'] = is_array($url) ? $url : array($url); |
|
|
|
|
44
|
|
|
if(class_exists('Memcached')) |
45
|
|
|
{ |
46
|
|
|
$this->Memcache = new Memcached; |
|
|
|
|
47
|
|
|
$this->SelectedExtension = 'Memcached'; |
48
|
|
|
} |
49
|
|
|
elseif(class_exists('Memcache')) |
50
|
|
|
{ |
51
|
|
|
$this->Memcache = new Memcache; |
|
|
|
|
52
|
|
|
$this->SelectedExtension = 'Memcache'; |
53
|
|
|
} |
54
|
|
|
else |
55
|
|
|
{ |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
foreach($config['url'] as $url) |
60
|
|
|
{ |
61
|
|
|
$info = parse_url($url); |
62
|
|
|
$this->Memcache->addServer($info['host'], $info['port']); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return whether support or not support cache |
68
|
|
|
* |
69
|
|
|
* @return bool Return true on support or false on not support |
70
|
|
|
*/ |
71
|
|
|
function isSupport() |
72
|
|
|
{ |
73
|
|
|
if(isset($GLOBALS['XE_MEMCACHE_SUPPORT'])) |
74
|
|
|
{ |
75
|
|
|
return $GLOBALS['XE_MEMCACHE_SUPPORT']; |
76
|
|
|
} |
77
|
|
|
if($this->SelectedExtension === 'Memcached') |
78
|
|
|
{ |
79
|
|
|
return $GLOBALS['XE_MEMCACHE_SUPPORT'] = $this->Memcache->set('xe', 'xe', 1); |
80
|
|
|
} |
81
|
|
|
elseif($this->SelectedExtension === 'Memcache') |
82
|
|
|
{ |
83
|
|
|
return $GLOBALS['XE_MEMCACHE_SUPPORT'] = $this->Memcache->set('xe', 'xe', MEMCACHE_COMPRESSED, 1); |
84
|
|
|
} |
85
|
|
|
else |
86
|
|
|
{ |
87
|
|
|
return $GLOBALS['XE_MEMCACHE_SUPPORT'] = false; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get unique key of given key by path of XE |
93
|
|
|
* |
94
|
|
|
* @param string $key Cache key |
95
|
|
|
* @return string Return unique key |
96
|
|
|
*/ |
97
|
|
|
function getKey($key) |
98
|
|
|
{ |
99
|
|
|
return md5(_XE_PATH_ . $key); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Store data at the server |
104
|
|
|
* |
105
|
|
|
* CacheMemcache::put() stores an item $buff with $key on the memcached server. |
106
|
|
|
* Parameter $valid_time is expiration time in seconds. If it's 0, the item never expires |
107
|
|
|
* (but memcached server doesn't guarantee this item to be stored all the time, it could be delete from the cache to make place for other items). |
108
|
|
|
* |
109
|
|
|
* Remember that resource variables (i.e. file and connection descriptors) cannot be stored in the cache, |
110
|
|
|
* because they can not be adequately represented in serialized state. |
111
|
|
|
* |
112
|
|
|
* @param string $key The key that will be associated with the item. |
113
|
|
|
* @param mixed $buff The variable to store. Strings and integers are stored as is, other types are stored serialized. |
114
|
|
|
* @param int $valid_time Expiration time of the item. |
115
|
|
|
* You can also use Unix timestamp or a number of seconds starting from current time, but in the latter case the number of seconds may not exceed 2592000 (30 days). |
116
|
|
|
* If it's equal to zero, use the default valid time CacheMemcache::valid_time. |
117
|
|
|
* @return bool Returns true on success or false on failure. |
118
|
|
|
*/ |
119
|
|
|
function put($key, $buff, $valid_time = 0) |
120
|
|
|
{ |
121
|
|
|
if($valid_time == 0) |
122
|
|
|
{ |
123
|
|
|
$valid_time = $this->valid_time; |
124
|
|
|
} |
125
|
|
|
if($this->SelectedExtension === 'Memcached') |
126
|
|
|
{ |
127
|
|
|
return $this->Memcache->set($this->getKey($key), array($_SERVER['REQUEST_TIME'], $buff), $valid_time); |
128
|
|
|
} |
129
|
|
|
else |
130
|
|
|
{ |
131
|
|
|
return $this->Memcache->set($this->getKey($key), array($_SERVER['REQUEST_TIME'], $buff), MEMCACHE_COMPRESSED, $valid_time); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Return whether cache is valid or invalid |
137
|
|
|
* |
138
|
|
|
* @param string $key Cache key |
139
|
|
|
* @param int $modified_time Unix time of data modified. |
140
|
|
|
* If stored time is older then modified time, the data is invalid. |
141
|
|
|
* @return bool Return true on valid or false on invalid. |
142
|
|
|
*/ |
143
|
|
View Code Duplication |
function isValid($key, $modified_time = 0) |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
$_key = $this->getKey($key); |
146
|
|
|
|
147
|
|
|
$obj = $this->Memcache->get($_key); |
148
|
|
|
if(!$obj || !is_array($obj)) |
149
|
|
|
{ |
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
unset($obj[1]); |
153
|
|
|
|
154
|
|
|
if($modified_time > 0 && $modified_time > $obj[0]) |
155
|
|
|
{ |
156
|
|
|
$this->_delete($_key); |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return true; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Retrieve item from the server |
165
|
|
|
* |
166
|
|
|
* CacheMemcache::get() returns previously stored data if an item with such $key exists on the server at this moment. |
167
|
|
|
* |
168
|
|
|
* @param string $key The key to fetch |
169
|
|
|
* @param int $modified_time Unix time of data modified. |
170
|
|
|
* If stored time is older then modified time, return false. |
171
|
|
|
* @return false|mixed Return false on failure or older then modified time. Return the string associated with the $key on success. |
172
|
|
|
*/ |
173
|
|
View Code Duplication |
function get($key, $modified_time = 0) |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
$_key = $this->getKey($key); |
176
|
|
|
$obj = $this->Memcache->get($_key); |
177
|
|
|
if(!$obj || !is_array($obj)) |
178
|
|
|
{ |
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if($modified_time > 0 && $modified_time > $obj[0]) |
183
|
|
|
{ |
184
|
|
|
$this->_delete($_key); |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
unset($obj[0]); |
189
|
|
|
|
190
|
|
|
return $obj[1]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Delete item from the server |
195
|
|
|
* |
196
|
|
|
* CacheMemcache::delete() deletes an item with tey $key. |
197
|
|
|
* |
198
|
|
|
* @param string $key The key associated with the item to delete. |
199
|
|
|
* @return void |
200
|
|
|
*/ |
201
|
|
|
function delete($key) |
202
|
|
|
{ |
203
|
|
|
$_key = $this->getKey($key); |
204
|
|
|
$this->_delete($_key); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Delete item from the server(private) |
209
|
|
|
* |
210
|
|
|
* @see CacheMemcache::delete() |
211
|
|
|
* @param string $_key The key associated with the item to delete. |
212
|
|
|
* @return void |
213
|
|
|
*/ |
214
|
|
|
function _delete($_key) |
215
|
|
|
{ |
216
|
|
|
$this->Memcache->delete($_key); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Flush all existing items at the server |
221
|
|
|
* |
222
|
|
|
* CacheMemcache::truncate() immediately invalidates all existing items. |
223
|
|
|
* CacheMemcache::truncate() doesn't actually free any resources, it only marks all the items as expired, |
224
|
|
|
* so occupied memory will be overwitten by new items. |
225
|
|
|
* |
226
|
|
|
* @return bool Returns true on success or false on failure. |
227
|
|
|
*/ |
228
|
|
|
function truncate() |
229
|
|
|
{ |
230
|
|
|
return $this->Memcache->flush(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
} |
234
|
|
|
/* End of file CacheMemcache.class.php */ |
235
|
|
|
/* Location: ./classes/cache/CacheMemcache.class.php */ |
236
|
|
|
|