1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vectorface\Cache; |
4
|
|
|
|
5
|
|
|
use Vectorface\Cache\Common\PSR16Util; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This cache is ridiculously fast, according to basic benchmarks: |
9
|
|
|
* |
10
|
|
|
* Parameters: |
11
|
|
|
* APC 3.0.19 |
12
|
|
|
* 9-byte key |
13
|
|
|
* 151-byte value |
14
|
|
|
* 10000-iteration test |
15
|
|
|
* |
16
|
|
|
* Result: |
17
|
|
|
* 0.065223 seconds |
18
|
|
|
* |
19
|
|
|
* Conclusion: |
20
|
|
|
* Capable of approximately 150000 requests/second |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Implements the Cache interface on top of APC or APCu. |
25
|
|
|
*/ |
26
|
|
|
class APCCache implements Cache, AtomicCounter |
27
|
|
|
{ |
28
|
|
|
use PSR16Util; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The module name that defines the APC methods. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $apcModule = 'apcu'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
40
|
14 |
|
public function get($key, $default = null) |
41
|
|
|
{ |
42
|
14 |
|
$value = $this->call('fetch', $this->key($key)); |
43
|
13 |
|
return ($value === false) ? $default : $value; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritDoc |
48
|
|
|
*/ |
49
|
15 |
|
public function set($key, $value, $ttl = null) |
50
|
|
|
{ |
51
|
15 |
|
return $this->call('store', $this->key($key), $value, $this->ttl($ttl)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritDoc |
56
|
|
|
*/ |
57
|
3 |
|
public function delete($key) |
58
|
|
|
{ |
59
|
3 |
|
return $this->call('delete', $this->key($key)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
1 |
|
public function clean() |
66
|
|
|
{ |
67
|
1 |
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritDoc |
72
|
|
|
*/ |
73
|
6 |
|
public function flush() |
74
|
|
|
{ |
75
|
6 |
|
return $this->call('clear_cache'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
3 |
|
public function clear() |
82
|
|
|
{ |
83
|
3 |
|
return $this->flush(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
3 |
|
public function getMultiple($keys, $default = null) |
90
|
|
|
{ |
91
|
3 |
|
$keys = $this->keys($keys); |
92
|
2 |
|
return $this->defaults( |
93
|
2 |
|
$keys, |
94
|
2 |
|
$this->call('fetch', $keys), |
95
|
2 |
|
$default |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritDoc |
101
|
|
|
*/ |
102
|
3 |
|
public function setMultiple($values, $ttl = null) |
103
|
|
|
{ |
104
|
3 |
|
$results = $this->call( |
105
|
3 |
|
'store', |
106
|
3 |
|
$this->values($values), |
107
|
2 |
|
null, |
108
|
2 |
|
$this->ttl($ttl) |
109
|
|
|
); |
110
|
|
|
return array_reduce($results, function($carry, $item) { return $carry && $item; }, true); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritDoc |
115
|
|
|
*/ |
116
|
2 |
|
public function deleteMultiple($keys) |
117
|
|
|
{ |
118
|
2 |
|
$success = true; |
119
|
2 |
|
foreach ($this->keys($keys) as $key) { |
120
|
2 |
|
$success = $this->call('delete', $key) && $success; |
121
|
|
|
} |
122
|
|
|
|
123
|
2 |
|
return $success; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritDoc |
128
|
|
|
*/ |
129
|
1 |
|
public function has($key) |
130
|
|
|
{ |
131
|
1 |
|
return $this->call('exists', $this->key($key)); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritDoc |
136
|
|
|
*/ |
137
|
1 |
|
public function increment($key, $step = 1) |
138
|
|
|
{ |
139
|
1 |
|
return $this->call('inc', $this->key($key), $this->step($step)); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritDoc |
144
|
|
|
*/ |
145
|
1 |
|
public function decrement($key, $step = 1) |
146
|
|
|
{ |
147
|
1 |
|
return $this->call('dec', $this->key($key), $this->step($step)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Pass a call through to APC or APCu |
152
|
|
|
* @param string $call Transformed to a function apc(u)_$call |
153
|
|
|
* @param mixed ...$args Function arguments |
154
|
|
|
* @return mixed The result passed through from apc(u)_$call |
155
|
|
|
*/ |
156
|
17 |
|
private function call($call, ...$args) |
157
|
|
|
{ |
158
|
17 |
|
$function = "{$this->apcModule}_{$call}"; |
159
|
|
|
|
160
|
17 |
|
return $function(...$args); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|