|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Provides caching functionality for XCache |
|
5
|
|
|
* |
|
6
|
|
|
* PHP Version 5 |
|
7
|
|
|
* |
|
8
|
|
|
* @category Core |
|
9
|
|
|
* @package Cache |
|
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
11
|
|
|
* @copyright 2013 cSphere Team |
|
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
13
|
|
|
* @link http://www.csphere.eu |
|
14
|
|
|
**/ |
|
15
|
|
|
|
|
16
|
|
|
namespace csphere\core\cache; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Provides caching functionality for XCache |
|
20
|
|
|
* |
|
21
|
|
|
* @category Core |
|
22
|
|
|
* @package Cache |
|
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
24
|
|
|
* @copyright 2013 cSphere Team |
|
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
26
|
|
|
* @link http://www.csphere.eu |
|
27
|
|
|
**/ |
|
28
|
|
|
|
|
29
|
|
|
class Driver_Xcache extends Base |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Creates the cache handler object |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $config Configuration details as an array |
|
35
|
|
|
* |
|
36
|
|
|
* @throws \Exception |
|
37
|
|
|
* |
|
38
|
|
|
* @return \csphere\core\cache\Driver_XCache |
|
39
|
|
|
**/ |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
public function __construct(array $config) |
|
42
|
|
|
{ |
|
43
|
|
|
parent::__construct($config); |
|
44
|
|
|
|
|
45
|
|
|
if (!extension_loaded('xcache')) { |
|
46
|
|
|
|
|
47
|
|
|
throw new \Exception('Extension "xcache" not found'); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Clears the cache content |
|
53
|
|
|
* |
|
54
|
|
|
* @return boolean |
|
55
|
|
|
**/ |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
public function clear() |
|
58
|
|
|
{ |
|
59
|
|
|
$cache_count = xcache_count(XC_TYPE_VAR); |
|
60
|
|
|
|
|
61
|
|
|
for ($i = 0; $i < $cache_count; $i++) { |
|
62
|
|
|
|
|
63
|
|
|
xcache_clear_cache(XC_TYPE_VAR, $i); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Removes a cached key |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $key Name of the key |
|
73
|
|
|
* @param int $ttl Time to life used for the key |
|
74
|
|
|
* |
|
75
|
|
|
* @return boolean |
|
76
|
|
|
**/ |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
public function delete($key, $ttl = 0) |
|
79
|
|
|
{ |
|
80
|
|
|
$token = empty($ttl) ? $key : 'ttl_' . $key; |
|
81
|
|
|
|
|
82
|
|
|
if (xcache_isset($token)) { |
|
83
|
|
|
|
|
84
|
|
|
xcache_unset($token); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns a formatted array with statistics |
|
92
|
|
|
* |
|
93
|
|
|
* @return array |
|
94
|
|
|
**/ |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
public function info() |
|
97
|
|
|
{ |
|
98
|
|
|
$info = parent::info(); |
|
99
|
|
|
|
|
100
|
|
|
$info['version'] = phpversion('xcache'); |
|
101
|
|
|
$info['client'] = ''; |
|
102
|
|
|
$info['server'] = ''; |
|
103
|
|
|
$info['keys'] = xcache_count(XC_TYPE_VAR); |
|
104
|
|
|
|
|
105
|
|
|
return $info; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns a formatted array with all keys and additional information |
|
110
|
|
|
* |
|
111
|
|
|
* @return array |
|
112
|
|
|
**/ |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
public function keys() |
|
115
|
|
|
{ |
|
116
|
|
|
$form = []; |
|
117
|
|
|
|
|
118
|
|
|
$cache_count = xcache_count(XC_TYPE_VAR); |
|
119
|
|
|
|
|
120
|
|
|
for ($i = 0; $i < $cache_count; $i++) { |
|
121
|
|
|
|
|
122
|
|
|
$info = xcache_list(XC_TYPE_VAR, $i); |
|
123
|
|
|
|
|
124
|
|
|
foreach ($info['cache_list'] AS $num => $data) { |
|
125
|
|
|
|
|
126
|
|
|
$handle = $data['name'] . ' (' . $i . '.' . $num . ')'; |
|
127
|
|
|
|
|
128
|
|
|
$form[$handle] = ['name' => $handle, |
|
129
|
|
|
'time' => $data['ctime'], |
|
130
|
|
|
'size' => $data['size']]; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
ksort($form); |
|
135
|
|
|
|
|
136
|
|
|
return array_values($form); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Fetches the desired key |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $key Name of the key |
|
143
|
|
|
* @param int $ttl Time to life used for the key |
|
144
|
|
|
* |
|
145
|
|
|
* @return array |
|
146
|
|
|
**/ |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
public function load($key, $ttl = 0) |
|
149
|
|
|
{ |
|
150
|
|
|
$token = empty($ttl) ? $key : 'ttl_' . $key; |
|
151
|
|
|
|
|
152
|
|
|
if (xcache_isset($token)) { |
|
153
|
|
|
|
|
154
|
|
|
return xcache_get($token); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return false; |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Stores the key with its value in the cache |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $key Name of the key |
|
164
|
|
|
* @param array $value Content to be stored |
|
165
|
|
|
* @param int $ttl Time to life used for the key |
|
166
|
|
|
* |
|
167
|
|
|
* @return boolean |
|
168
|
|
|
**/ |
|
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
public function save($key, $value, $ttl = 0) |
|
171
|
|
|
{ |
|
172
|
|
|
$token = empty($ttl) ? $key : 'ttl_' . $key; |
|
173
|
|
|
|
|
174
|
|
|
xcache_set($token, $value, $ttl); |
|
175
|
|
|
|
|
176
|
|
|
$this->log($key); |
|
177
|
|
|
|
|
178
|
|
|
return true; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
Adding a
@returnannotation 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.