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 Container キャッシュ依存コンテナ |
19
|
|
|
*/ |
20
|
|
|
private $cacheContainer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function __construct(Container $cacheContainer) |
26
|
|
|
{ |
27
|
|
|
$this->cacheContainer = $cacheContainer; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function add($key, $value, $ttl = 0, $overwrite = false): bool |
34
|
|
|
{ |
35
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
$key = $this->cacheContainer->cachePrefix . $key; |
|
|
|
|
39
|
|
|
$result = null; |
40
|
|
|
|
41
|
|
|
if ($ttl > 0) { |
42
|
|
View Code Duplication |
if ($overwrite) { |
|
|
|
|
43
|
|
|
if ($this->cacheContainer->driver->replace($key, $value, $ttl) === false) { |
|
|
|
|
44
|
|
|
$result = $this->cacheContainer->driver->set($key, $value, $ttl); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
} else { |
47
|
|
|
$result = $this->cacheContainer->driver->set($key, $value, $ttl); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
} else { |
50
|
|
View Code Duplication |
if ($overwrite) { |
|
|
|
|
51
|
|
|
if ($this->cacheContainer->driver->replace($key, $value) === false) { |
|
|
|
|
52
|
|
|
$result = $this->cacheContainer->driver->set($key, $value); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
} else { |
55
|
|
|
$result = $this->cacheContainer->driver->set($key, $value); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->logger->info("Execute cache save: " . $key); |
|
|
|
|
60
|
|
|
$this->verifyReturnCode($this->cacheContainer->codes['success']); |
|
|
|
|
61
|
|
|
|
62
|
|
|
return $result; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function get($key) |
69
|
|
|
{ |
70
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
$key = $this->cacheContainer->cachePrefix . $key; |
|
|
|
|
74
|
|
|
$result = $this->cacheContainer->driver->get($key); |
|
|
|
|
75
|
|
|
|
76
|
|
View Code Duplication |
if ($result !== false) { |
|
|
|
|
77
|
|
|
$this->logger->info("Execute cache read: " . $key); |
|
|
|
|
78
|
|
|
} else { |
79
|
|
|
$this->logger->warn("Failed to read cache: " . $key); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->verifyReturnCode($this->cacheContainer->codes['success']) ? $result : null; |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function delete($key): bool |
89
|
|
|
{ |
90
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
$key = $this->cacheContainer->cachePrefix . $key; |
|
|
|
|
94
|
|
|
$this->cacheContainer->driver->delete($key); |
|
|
|
|
95
|
|
|
|
96
|
|
|
if ($this->verifyReturnCode($this->cacheContainer->codes['notfound'])) { |
|
|
|
|
97
|
|
|
$this->logger->info("Execute cache cleared: " . $key); |
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} else { |
100
|
|
|
$this->logger->warn("Failed to clear cache: " . $key); |
|
|
|
|
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function clear(): bool |
109
|
|
|
{ |
110
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
$allKeys = $this->cacheContainer->driver->getAllKeys(); |
|
|
|
|
114
|
|
|
if ($allKeys === false) { |
115
|
|
|
$this->logger->warn("Can't get cache keys: " . $this->cacheContainer->cachePrefix . "*"); |
|
|
|
|
116
|
|
|
$this->cacheContainer->driver->flush(); |
|
|
|
|
117
|
|
|
|
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$prefixLength = strlen($this->cacheContainer->cachePrefix); |
|
|
|
|
122
|
|
|
$targetKeys = []; |
123
|
|
|
foreach ($allKeys as $key) { |
124
|
|
|
if (substr($key, 0, $prefixLength)) { |
125
|
|
|
$targetKeys[] = $key; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->deleteMulti($targetKeys); |
|
|
|
|
130
|
|
|
|
131
|
|
|
if ($this->verifyReturnCode($this->cacheContainer->codes['notfound'])) { |
|
|
|
|
132
|
|
|
$this->logger->info("Execute all cache cleared: " . $this->cacheContainer->cachePrefix . "*"); |
|
|
|
|
133
|
|
|
return true; |
134
|
|
|
} else { |
135
|
|
|
$this->logger->warn("Failed to clear all cache: " . $this->cacheContainer->cachePrefix . "*"); |
|
|
|
|
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* リターンコードを検証する |
142
|
|
|
* @param int $code 想定コード |
143
|
|
|
* @return bool 検証結果 |
144
|
|
|
*/ |
145
|
|
|
private function verifyReturnCode(int $code): bool |
146
|
|
|
{ |
147
|
|
|
if ($code !== $this->cacheContainer->driver->getResultCode()) { |
|
|
|
|
148
|
|
|
$message = $this->cacheContainer->driver->getResultMessage(); |
|
|
|
|
149
|
|
|
$this->logger->warn("Error $code interacting with memcached: $message"); |
|
|
|
|
150
|
|
|
|
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return true; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* キャッシュライブラリが使用可能か検査する |
159
|
|
|
* @return bool 使用可能でtrue |
160
|
|
|
*/ |
161
|
|
|
private function isAvailableCacheLibrary(): bool |
162
|
|
|
{ |
163
|
|
|
if ($this->cacheContainer->available) { |
|
|
|
|
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$this->logger->warn("Memcached cache library is unavailable."); |
|
|
|
|
168
|
|
|
|
169
|
|
|
return false; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read 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.