1
|
|
|
<?php |
2
|
|
|
/** @noinspection PhpComposerExtensionStubsInspection */ |
3
|
|
|
|
4
|
|
|
namespace Vectorface\Cache; |
5
|
|
|
|
6
|
|
|
use Memcache; |
7
|
|
|
use Vectorface\Cache\Common\PSR16Util; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Implements the cache interface on top of Memcache |
11
|
|
|
* |
12
|
|
|
* This cache is very fast, according to basic benchmarks: |
13
|
|
|
* |
14
|
|
|
* Parameters: |
15
|
|
|
* Memcache 1.2.2, running locally |
16
|
|
|
* 9-byte key |
17
|
|
|
* 151-byte value |
18
|
|
|
* 10000-iteration test |
19
|
|
|
* |
20
|
|
|
* Result: |
21
|
|
|
* 0.859622001648 seconds |
22
|
|
|
* |
23
|
|
|
* Conclusion: |
24
|
|
|
* Capable of approximately 11678 requests/second |
25
|
|
|
*/ |
26
|
|
|
class MCCache implements Cache, AtomicCounter |
27
|
|
|
{ |
28
|
|
|
use PSR16Util; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Memcache instance; Connection to the memcached server. |
32
|
|
|
* |
33
|
|
|
* @var Memcache |
34
|
|
|
*/ |
35
|
|
|
private $mc; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new memcache-based cache. |
39
|
|
|
* |
40
|
|
|
* @param Memcache $mc The memcache instance, or null to try to build one. |
41
|
|
|
*/ |
42
|
20 |
|
public function __construct(Memcache $mc) |
43
|
|
|
{ |
44
|
20 |
|
$this->mc = $mc; |
45
|
20 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritDoc |
49
|
|
|
*/ |
50
|
15 |
|
public function get($key, $default = null) |
51
|
|
|
{ |
52
|
15 |
|
$value = $this->mc->get($this->key($key)); |
53
|
14 |
|
return ($value === false) ? $default : $value; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritDoc |
58
|
|
|
*/ |
59
|
17 |
|
public function set($key, $value, $ttl = null) |
60
|
|
|
{ |
61
|
17 |
|
return $this->mc->set($this->key($key), $value, null, $this->ttl($ttl)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritDoc |
66
|
|
|
*/ |
67
|
5 |
|
public function delete($key) |
68
|
|
|
{ |
69
|
5 |
|
return $this->mc->delete($this->key($key)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritDoc |
74
|
|
|
*/ |
75
|
1 |
|
public function clean() |
76
|
|
|
{ |
77
|
1 |
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritDoc |
82
|
|
|
*/ |
83
|
6 |
|
public function flush() |
84
|
|
|
{ |
85
|
6 |
|
return $this->mc->flush(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritDoc |
90
|
|
|
*/ |
91
|
3 |
|
public function clear() |
92
|
|
|
{ |
93
|
3 |
|
return $this->flush(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc |
98
|
|
|
*/ |
99
|
4 |
|
public function getMultiple($keys, $default = null) |
100
|
|
|
{ |
101
|
4 |
|
$keys = $this->keys($keys); |
102
|
3 |
|
$values = $this->mc->get($keys); |
103
|
|
|
|
104
|
3 |
|
if ($values === false) { |
105
|
1 |
|
$values = []; |
106
|
2 |
|
} elseif (is_string($values)) { |
107
|
|
|
$values = [$values]; // shouldn't technically happen if $keys is an array |
108
|
|
|
} |
109
|
|
|
|
110
|
3 |
|
foreach ($keys as $key) { |
111
|
3 |
|
if (!isset($values[$key]) || $values[$key] === false) { |
112
|
3 |
|
$values[$key] = $default; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
return $values; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritDoc |
121
|
|
|
*/ |
122
|
3 |
|
public function setMultiple($values, $ttl = null) |
123
|
|
|
{ |
124
|
3 |
|
$success = true; |
125
|
3 |
|
foreach ($this->values($values) as $key => $value) { |
126
|
2 |
|
$success = $this->set($key, $value, $ttl) && $success; |
127
|
|
|
} |
128
|
2 |
|
return $success; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritDoc |
133
|
|
|
*/ |
134
|
2 |
|
public function deleteMultiple($keys) |
135
|
|
|
{ |
136
|
2 |
|
$success = true; |
137
|
2 |
|
foreach ($this->keys($keys) as $key) { |
138
|
2 |
|
$success = $this->delete($key) && $success; |
139
|
|
|
} |
140
|
|
|
|
141
|
2 |
|
return $success; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @inheritDoc |
146
|
|
|
*/ |
147
|
1 |
|
public function has($key) |
148
|
|
|
{ |
149
|
1 |
|
return $this->get($this->key($key), null) !== null; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @inheritDoc |
154
|
|
|
*/ |
155
|
1 |
|
public function increment($key, $step = 1) |
156
|
|
|
{ |
157
|
1 |
|
$key = $this->key($key); |
158
|
|
|
|
159
|
|
|
// If the key already exists, this is a no-op, otherwise it ensures the key is created. |
160
|
|
|
// See https://www.php.net/manual/en/memcache.increment.php#90864 |
161
|
1 |
|
$this->mc->add($key, 0); |
162
|
|
|
|
163
|
1 |
|
return $this->mc->increment($key, $this->step($step)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @inheritDoc |
168
|
|
|
*/ |
169
|
1 |
|
public function decrement($key, $step = 1) |
170
|
|
|
{ |
171
|
1 |
|
$key = $this->key($key); |
172
|
|
|
|
173
|
|
|
// If the key already exists, this is a no-op, otherwise it ensures the key is created. |
174
|
|
|
// See https://www.php.net/manual/en/memcache.increment.php#90864 |
175
|
1 |
|
$this->mc->add($key, 0); |
176
|
|
|
|
177
|
1 |
|
return $this->mc->decrement($key, $this->step($step)); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|