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