1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Cache\Driver; |
3
|
|
|
|
4
|
|
|
use WebStream\DI\Injector; |
5
|
|
|
use WebStream\Module\Container; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Redis |
9
|
|
|
* @author Ryuichi Tanaka |
10
|
|
|
* @since 2015/07/09 |
11
|
|
|
* @version 0.7 |
12
|
|
|
*/ |
13
|
|
|
class Redis 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, $overrite = false): bool |
34
|
|
|
{ |
35
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (is_array($value)) { |
40
|
|
|
$value = json_encode($value); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$result = false; |
|
|
|
|
44
|
|
|
if ($ttl > 0) { |
45
|
|
|
if ($overrite) { |
46
|
|
|
$result = $this->cacheContainer->driver->setEx($key, $ttl, $value); |
|
|
|
|
47
|
|
|
} else { |
48
|
|
|
$result = $this->cacheContainer->driver->set($key, $value, ['nx', 'ex' => $ttl]); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
} else { |
51
|
|
|
if ($overrite) { |
52
|
|
|
$result = $this->cacheContainer->driver->set($key, $value); |
|
|
|
|
53
|
|
|
} else { |
54
|
|
|
$result = $this->cacheContainer->driver->setNx($key, $value); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->logger->info("Execute cache save: " . $key); |
|
|
|
|
59
|
|
|
|
60
|
|
|
return $result; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function get($key) |
67
|
|
|
{ |
68
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$result = $this->cacheContainer->driver->get($key); |
|
|
|
|
73
|
|
|
$this->logger->info("Execute cache read: " . $key); |
|
|
|
|
74
|
|
|
|
75
|
|
View Code Duplication |
if ($result !== false) { |
|
|
|
|
76
|
|
|
$this->logger->info("Execute cache read: " . $key); |
|
|
|
|
77
|
|
|
} else { |
78
|
|
|
$this->logger->warn("Failed to read cache: " . $key); |
|
|
|
|
79
|
|
|
$result = null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $result; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function delete($key): bool |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($this->cacheContainer->driver->delete($key)) { |
|
|
|
|
95
|
|
|
$this->logger->info("Execute cache cleared: " . $key); |
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} else { |
98
|
|
|
$this->logger->warn("Failed to clear cache: " . $key); |
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function clear(): bool |
107
|
|
|
{ |
108
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$it = null; |
113
|
|
|
$result = 1; |
114
|
|
|
$this->cacheContainer->driver->setOption(\Redis::OPT_PREFIX, ""); |
|
|
|
|
115
|
|
|
while ($keys = $this->cacheContainer->driver->scan($it, "*")) { |
|
|
|
|
116
|
|
|
$result *= $this->cacheContainer->driver->delete($keys); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
$this->cacheContainer->driver->setOption(\Redis::OPT_PREFIX, $this->cacheContainer->cachePrefix); |
|
|
|
|
119
|
|
|
|
120
|
|
|
if ($result > 0) { |
121
|
|
|
$this->logger->info("Execute all cache cleared: " . $this->cacheContainer->cachePrefix . "*"); |
|
|
|
|
122
|
|
|
return true; |
123
|
|
|
} else { |
124
|
|
|
$this->logger->warn("Failed to clear all cache: " . $this->cacheContainer->cachePrefix . "*"); |
|
|
|
|
125
|
|
|
return false; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* キャッシュライブラリが使用可能か検査する |
131
|
|
|
* @return bool 使用可能でtrue |
132
|
|
|
*/ |
133
|
|
|
private function isAvailableCacheLibrary(): bool |
134
|
|
|
{ |
135
|
|
|
if ($this->cacheContainer->available) { |
|
|
|
|
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->logger->warn("APCu cache library is unavailable."); |
|
|
|
|
140
|
|
|
|
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.