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