1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\caching; |
9
|
|
|
|
10
|
|
|
use yii\base\InvalidConfigException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* ApcCache provides APC caching in terms of an application component. |
14
|
|
|
* |
15
|
|
|
* To use this application component, the [APC PHP extension](http://www.php.net/apc) must be loaded. |
16
|
|
|
* Alternatively [APCu PHP extension](http://www.php.net/apcu) could be used via setting `useApcu` to `true`. |
17
|
|
|
* In order to enable APC or APCu for CLI you should add "apc.enable_cli = 1" to your php.ini. |
18
|
|
|
* |
19
|
|
|
* See [[Cache]] for common cache operations that ApcCache supports. |
20
|
|
|
* |
21
|
|
|
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview). |
22
|
|
|
* |
23
|
|
|
* @author Qiang Xue <[email protected]> |
24
|
|
|
* @since 2.0 |
25
|
|
|
*/ |
26
|
|
|
class ApcCache extends Cache |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var bool whether to use apcu or apc as the underlying caching extension. |
30
|
|
|
* If true, [apcu](http://pecl.php.net/package/apcu) will be used. |
31
|
|
|
* If false, [apc](http://pecl.php.net/package/apc) will be used. |
32
|
|
|
* Defaults to false. |
33
|
|
|
* @since 2.0.7 |
34
|
|
|
*/ |
35
|
|
|
public $useApcu = false; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Initializes this application component. |
40
|
|
|
* It checks if extension required is loaded. |
41
|
|
|
*/ |
42
|
16 |
|
public function init() |
43
|
|
|
{ |
44
|
16 |
|
parent::init(); |
45
|
16 |
|
$extension = $this->useApcu ? 'apcu' : 'apc'; |
46
|
16 |
|
if (!extension_loaded($extension)) { |
47
|
16 |
|
throw new InvalidConfigException("ApcCache requires PHP $extension extension to be loaded."); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Checks whether a specified key exists in the cache. |
53
|
|
|
* This can be faster than getting the value from the cache if the data is big. |
54
|
|
|
* Note that this method does not check whether the dependency associated |
55
|
|
|
* with the cached data, if there is any, has changed. So a call to [[get]] |
56
|
|
|
* may return false while exists returns true. |
57
|
|
|
* @param mixed $key a key identifying the cached value. This can be a simple string or |
58
|
|
|
* a complex data structure consisting of factors representing the key. |
59
|
|
|
* @return bool true if a value exists in cache, false if the value is not in the cache or expired. |
60
|
|
|
*/ |
61
|
|
|
public function exists($key) |
62
|
|
|
{ |
63
|
|
|
$key = $this->buildKey($key); |
64
|
|
|
|
65
|
|
|
return $this->useApcu ? apcu_exists($key) : apc_exists($key); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Retrieves a value from cache with a specified key. |
70
|
|
|
* This is the implementation of the method declared in the parent class. |
71
|
|
|
* @param string $key a unique key identifying the cached value |
72
|
|
|
* @return mixed|false the value stored in cache, false if the value is not in the cache or expired. |
73
|
|
|
*/ |
74
|
|
|
protected function getValue($key) |
75
|
|
|
{ |
76
|
|
|
return $this->useApcu ? apcu_fetch($key) : apc_fetch($key); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Retrieves multiple values from cache with the specified keys. |
81
|
|
|
* @param array $keys a list of keys identifying the cached values |
82
|
|
|
* @return array a list of cached values indexed by the keys |
83
|
|
|
*/ |
84
|
|
|
protected function getValues($keys) |
85
|
|
|
{ |
86
|
|
|
$values = $this->useApcu ? apcu_fetch($keys) : apc_fetch($keys); |
87
|
|
|
return is_array($values) ? $values : []; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Stores a value identified by a key in cache. |
92
|
|
|
* This is the implementation of the method declared in the parent class. |
93
|
|
|
* |
94
|
|
|
* @param string $key the key identifying the value to be cached |
95
|
|
|
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]], |
96
|
|
|
* it could be something else. |
97
|
|
|
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire. |
98
|
|
|
* @return bool true if the value is successfully stored into cache, false otherwise. |
99
|
|
|
*/ |
100
|
|
|
protected function setValue($key, $value, $duration) |
101
|
|
|
{ |
102
|
|
|
return $this->useApcu ? apcu_store($key, $value, $duration) : apc_store($key, $value, $duration); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Stores multiple key-value pairs in cache. |
107
|
|
|
* @param array $data array where key corresponds to cache key while value |
108
|
|
|
* @param int $duration the number of seconds in which the cached values will expire. 0 means never expire. |
109
|
|
|
* @return array array of failed keys |
110
|
|
|
*/ |
111
|
|
|
protected function setValues($data, $duration) |
112
|
|
|
{ |
113
|
|
|
$result = $this->useApcu ? apcu_store($data, null, $duration) : apc_store($data, null, $duration); |
114
|
|
|
return is_array($result) ? array_keys($result) : []; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Stores a value identified by a key into cache if the cache does not contain this key. |
119
|
|
|
* This is the implementation of the method declared in the parent class. |
120
|
|
|
* @param string $key the key identifying the value to be cached |
121
|
|
|
* @param mixed $value the value to be cached. Most often it's a string. If you have disabled [[serializer]], |
122
|
|
|
* it could be something else. |
123
|
|
|
* @param int $duration the number of seconds in which the cached value will expire. 0 means never expire. |
124
|
|
|
* @return bool true if the value is successfully stored into cache, false otherwise |
125
|
|
|
*/ |
126
|
|
|
protected function addValue($key, $value, $duration) |
127
|
|
|
{ |
128
|
|
|
return $this->useApcu ? apcu_add($key, $value, $duration) : apc_add($key, $value, $duration); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Adds multiple key-value pairs to cache. |
133
|
|
|
* @param array $data array where key corresponds to cache key while value is the value stored |
134
|
|
|
* @param int $duration the number of seconds in which the cached values will expire. 0 means never expire. |
135
|
|
|
* @return array array of failed keys |
136
|
|
|
*/ |
137
|
|
|
protected function addValues($data, $duration) |
138
|
|
|
{ |
139
|
|
|
$result = $this->useApcu ? apcu_add($data, null, $duration) : apc_add($data, null, $duration); |
140
|
|
|
return is_array($result) ? array_keys($result) : []; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Deletes a value with the specified key from cache |
145
|
|
|
* This is the implementation of the method declared in the parent class. |
146
|
|
|
* @param string $key the key of the value to be deleted |
147
|
|
|
* @return bool if no error happens during deletion |
148
|
|
|
*/ |
149
|
|
|
protected function deleteValue($key) |
150
|
|
|
{ |
151
|
|
|
return $this->useApcu ? apcu_delete($key) : apc_delete($key); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Deletes all values from cache. |
156
|
|
|
* This is the implementation of the method declared in the parent class. |
157
|
|
|
* @return bool whether the flush operation was successful. |
158
|
|
|
*/ |
159
|
|
|
protected function flushValues() |
160
|
|
|
{ |
161
|
|
|
return $this->useApcu ? apcu_clear_cache() : apc_clear_cache('user'); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|