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
|
|
|
* Application configuration example: |
19
|
|
|
* |
20
|
|
|
* ```php |
21
|
|
|
* return [ |
22
|
|
|
* 'components' => [ |
23
|
|
|
* 'cache' => [ |
24
|
|
|
* 'class' => yii\caching\Cache::class, |
25
|
|
|
* 'handler' => [ |
26
|
|
|
* 'class' => yii\caching\ApcCache::class, |
27
|
|
|
* ], |
28
|
|
|
* ], |
29
|
|
|
* // ... |
30
|
|
|
* ], |
31
|
|
|
* // ... |
32
|
|
|
* ]; |
33
|
|
|
* ``` |
34
|
|
|
* |
35
|
|
|
* See [[\Psr\SimpleCache\CacheInterface]] for common cache operations that ApcCache supports. |
36
|
|
|
* |
37
|
|
|
* For more details and usage information on Cache, see the [guide article on caching](guide:caching-overview). |
38
|
|
|
* |
39
|
|
|
* @author Qiang Xue <[email protected]> |
40
|
|
|
* @since 2.0 |
41
|
|
|
*/ |
42
|
|
|
class ApcCache extends SimpleCache |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* Initializes this application component. |
46
|
|
|
* It checks if extension required is loaded. |
47
|
|
|
* @throws \yii\base\InvalidConfigException |
48
|
|
|
*/ |
49
|
16 |
|
public function init() |
50
|
|
|
{ |
51
|
16 |
|
parent::init(); |
52
|
16 |
|
if (!extension_loaded('apcu')) { |
53
|
|
|
throw new InvalidConfigException('ApcCache requires PHP apcu extension to be loaded.'); |
54
|
|
|
} |
55
|
16 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
2 |
|
public function has($key) |
61
|
|
|
{ |
62
|
2 |
|
return apcu_exists($this->normalizeKey($key)); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
13 |
|
protected function getValue($key) |
69
|
|
|
{ |
70
|
13 |
|
return apcu_fetch($key); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
3 |
|
protected function getValues($keys) |
77
|
|
|
{ |
78
|
3 |
|
$values = apcu_fetch($keys); |
79
|
3 |
|
return is_array($values) ? $values : []; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
12 |
|
protected function setValue($key, $value, $ttl) |
86
|
|
|
{ |
87
|
12 |
|
return apcu_store($key, $value, $ttl); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
4 |
|
protected function setValues($values, $ttl) |
94
|
|
|
{ |
95
|
4 |
|
$result = apcu_store($values, null, $ttl); |
96
|
4 |
|
return is_array($result); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
1 |
|
protected function deleteValue($key) |
103
|
|
|
{ |
104
|
1 |
|
return apcu_delete($key); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
11 |
|
public function clear() |
111
|
|
|
{ |
112
|
11 |
|
return apcu_clear_cache(); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|