1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license https://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\caching; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* XCache provides XCache caching in terms of an application component. |
12
|
|
|
* |
13
|
|
|
* To use this application component, the [XCache PHP extension](https://github.com/lighttpd/xcache) must be loaded. |
14
|
|
|
* Also note that the [[flush()]] functionality will work correctly only if "xcache.admin.enable_auth" |
15
|
|
|
* is set to "Off" in php.ini. |
16
|
|
|
* |
17
|
|
|
* See [[Cache]] for common cache operations that XCache supports. |
18
|
|
|
* |
19
|
|
|
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview). |
20
|
|
|
* |
21
|
|
|
* @author Qiang Xue <[email protected]> |
22
|
|
|
* @since 2.0 |
23
|
|
|
* @deprecated since 2.0.14. This class will be removed in 2.1.0. |
24
|
|
|
*/ |
25
|
|
|
class XCache extends Cache |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Checks whether a specified key exists in the cache. |
29
|
|
|
* This can be faster than getting the value from the cache if the data is big. |
30
|
|
|
* Note that this method does not check whether the dependency associated |
31
|
|
|
* with the cached data, if there is any, has changed. So a call to [[get]] |
32
|
|
|
* may return false while exists returns true. |
33
|
|
|
* @param mixed $key a key identifying the cached value. This can be a simple string or |
34
|
|
|
* a complex data structure consisting of factors representing the key. |
35
|
|
|
* @return bool true if a value exists in cache, false if the value is not in the cache or expired. |
36
|
|
|
*/ |
37
|
|
|
public function exists($key) |
38
|
|
|
{ |
39
|
|
|
$key = $this->buildKey($key); |
40
|
|
|
|
41
|
|
|
return xcache_isset($key); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Retrieves a value from cache with a specified key. |
46
|
|
|
* This is the implementation of the method declared in the parent class. |
47
|
|
|
* @param string $key a unique key identifying the cached value |
48
|
|
|
* @return mixed|false the value stored in cache, false if the value is not in the cache or expired. |
49
|
|
|
*/ |
50
|
|
|
protected function getValue($key) |
51
|
|
|
{ |
52
|
|
|
return xcache_isset($key) ? xcache_get($key) : false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Stores a value identified by a key in cache. |
57
|
|
|
* This is the implementation of the method declared in the parent class. |
58
|
|
|
* |
59
|
|
|
* @param string $key the key identifying the value to be cached |
60
|
|
|
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]], |
61
|
|
|
* it could be something else. |
62
|
|
|
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire. |
63
|
|
|
* @return bool true if the value is successfully stored into cache, false otherwise |
64
|
|
|
*/ |
65
|
|
|
protected function setValue($key, $value, $duration) |
66
|
|
|
{ |
67
|
|
|
return xcache_set($key, $value, $duration); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Stores a value identified by a key into cache if the cache does not contain this key. |
72
|
|
|
* This is the implementation of the method declared in the parent class. |
73
|
|
|
* |
74
|
|
|
* @param string $key the key identifying the value to be cached |
75
|
|
|
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]], |
76
|
|
|
* it could be something else. |
77
|
|
|
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire. |
78
|
|
|
* @return bool true if the value is successfully stored into cache, false otherwise |
79
|
|
|
*/ |
80
|
|
|
protected function addValue($key, $value, $duration) |
81
|
|
|
{ |
82
|
|
|
return !xcache_isset($key) ? $this->setValue($key, $value, $duration) : false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Deletes a value with the specified key from cache |
87
|
|
|
* This is the implementation of the method declared in the parent class. |
88
|
|
|
* @param string $key the key of the value to be deleted |
89
|
|
|
* @return bool if no error happens during deletion |
90
|
|
|
*/ |
91
|
|
|
protected function deleteValue($key) |
92
|
|
|
{ |
93
|
|
|
return xcache_unset($key); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Deletes all values from cache. |
98
|
|
|
* This is the implementation of the method declared in the parent class. |
99
|
|
|
* @return bool whether the flush operation was successful. |
100
|
|
|
*/ |
101
|
|
|
protected function flushValues() |
102
|
|
|
{ |
103
|
|
|
for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++) { |
|
|
|
|
104
|
|
|
if (xcache_clear_cache(XC_TYPE_VAR, $i) === false) { |
|
|
|
|
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|