1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Cache\Driver; |
3
|
|
|
|
4
|
|
|
use WebStream\DI\Injector; |
5
|
|
|
use WebStream\Module\Container; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Memcached |
9
|
|
|
* @author Ryuichi Tanaka |
10
|
|
|
* @since 2015/07/08 |
11
|
|
|
* @version 0.7 |
12
|
|
|
*/ |
13
|
|
|
class Memcached implements ICache |
14
|
|
|
{ |
15
|
|
|
use Injector; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \Memcached memcachedオブジェクト |
19
|
|
|
*/ |
20
|
|
|
private $cache; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool キャッシュ使用可能フラグ |
24
|
|
|
*/ |
25
|
|
|
private $isAvairable; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string キャッシュ接頭辞 |
29
|
|
|
*/ |
30
|
|
|
private $cachePrefix; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* constructor |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Container $container) |
36
|
|
|
{ |
37
|
|
|
$this->isAvairable = extension_loaded('memcached'); |
38
|
|
|
$this->cachePrefix = $container->cachePrefix; |
|
|
|
|
39
|
|
|
|
40
|
|
|
if ($this->isAvairable) { |
41
|
|
|
$this->cache = new \Memcached(); |
42
|
|
|
$this->cache->addServers($container->servers); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$defaultOptions = [ |
45
|
|
|
\Memcached::OPT_CONNECT_TIMEOUT => 50, |
46
|
|
|
\Memcached::OPT_RETRY_TIMEOUT => 50, |
47
|
|
|
\Memcached::OPT_SEND_TIMEOUT => 50, |
48
|
|
|
\Memcached::OPT_RECV_TIMEOUT => 50, |
49
|
|
|
\Memcached::OPT_POLL_TIMEOUT => 50, |
50
|
|
|
\Memcached::OPT_COMPRESSION => true, |
51
|
|
|
\Memcached::OPT_LIBKETAMA_COMPATIBLE => true, |
52
|
|
|
\Memcached::OPT_BINARY_PROTOCOL => true |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
if (\Memcached::HAVE_IGBINARY) { |
56
|
|
|
$defaultOptions[\Memcached::OPT_SERIALIZER] = \Memcached::SERIALIZER_IGBINARY; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->cache->setOptions($defaultOptions); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function add($key, $value, $ttl = 0, $overwrite = false): bool |
67
|
|
|
{ |
68
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
$key = $this->cachePrefix . $key; |
72
|
|
|
$result = null; |
73
|
|
|
|
74
|
|
|
if ($ttl > 0) { |
75
|
|
View Code Duplication |
if ($overwrite) { |
|
|
|
|
76
|
|
|
if ($this->cache->replace($key, $value, $ttl) === false) { |
77
|
|
|
$result = $this->cache->set($key, $value, $ttl); |
78
|
|
|
} |
79
|
|
|
} else { |
80
|
|
|
$result = $this->cache->set($key, $value, $ttl); |
81
|
|
|
} |
82
|
|
|
} else { |
83
|
|
View Code Duplication |
if ($overwrite) { |
|
|
|
|
84
|
|
|
if ($this->cache->replace($key, $value) === false) { |
85
|
|
|
$result = $this->cache->set($key, $value); |
86
|
|
|
} |
87
|
|
|
} else { |
88
|
|
|
$result = $this->cache->set($key, $value); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->logger->info("Execute cache save: " . $key); |
|
|
|
|
93
|
|
|
$this->verifyReturnCode(\Memcached::RES_SUCCESS); |
94
|
|
|
|
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function get($key) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
$key = $this->cachePrefix . $key; |
107
|
|
|
$value = $this->cache->get($key); |
108
|
|
|
$this->logger->info("Execute cache read: " . $key); |
|
|
|
|
109
|
|
|
|
110
|
|
|
return $this->verifyReturnCode(\Memcached::RES_SUCCESS) ? $value : null; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function delete($key): bool |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
$key = $this->cachePrefix . $key; |
122
|
|
|
$this->cache->delete($key); |
123
|
|
|
$this->logger->info("Execute cache cleared: " . $key); |
|
|
|
|
124
|
|
|
|
125
|
|
|
return $this->verifyReturnCode(\Memcached::RES_NOTFOUND); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function clear(): bool |
132
|
|
|
{ |
133
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
$allKeys = $this->cache->getAllKeys(); |
137
|
|
|
if ($allKeys === false) { |
138
|
|
|
$this->logger->warn(); |
|
|
|
|
139
|
|
|
$this->cache->flush(); |
140
|
|
|
|
141
|
|
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$prefixLength = strlen($this->cachePrefix); |
145
|
|
|
$targetKeys = []; |
146
|
|
|
foreach ($allKeys as $key) { |
147
|
|
|
if (substr($key, 0, $prefixLength)) { |
148
|
|
|
$targetKeys[] = $key; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->deleteMulti($targetKeys); |
|
|
|
|
153
|
|
|
$this->logger->info("Execute all cache cleared: " . $this->cachePrefix . "*"); |
|
|
|
|
154
|
|
|
|
155
|
|
|
return $this->verifyReturnCode(\Memcached::RES_NOTFOUND); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* リターンコードを検証する |
160
|
|
|
* @param int $code 想定コード |
161
|
|
|
* @return bool 検証結果 |
162
|
|
|
*/ |
163
|
|
|
private function verifyReturnCode(int $code) |
164
|
|
|
{ |
165
|
|
|
if ($code !== $this->cache->getResultCode()) { |
166
|
|
|
$message = $this->cache->getResultMessage(); |
167
|
|
|
$this->logger->warn("Error $code interacting with memcached: $message"); |
|
|
|
|
168
|
|
|
|
169
|
|
|
return false; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return true; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* キャッシュライブラリが使用可能か検査する |
177
|
|
|
* @return bool 使用可能でtrue |
178
|
|
|
*/ |
179
|
|
|
private function isAvailableCacheLibrary(): bool |
180
|
|
|
{ |
181
|
|
|
if ($this->isAvairable) { |
182
|
|
|
return true; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$this->logger->warn("Memcached cache library is unavailable."); |
|
|
|
|
186
|
|
|
|
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.