1 | <?php |
||
2 | |||
3 | namespace WebStream\Cache\Driver; |
||
4 | |||
5 | use WebStream\Container\Container; |
||
0 ignored issues
–
show
|
|||
6 | use WebStream\DI\Injector; |
||
0 ignored issues
–
show
The type
WebStream\DI\Injector was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
7 | |||
8 | /** |
||
9 | * Redis |
||
10 | * @author Ryuichi Tanaka |
||
11 | * @since 2015/07/09 |
||
12 | * @version 0.7 |
||
13 | */ |
||
14 | class Redis implements ICache |
||
15 | { |
||
16 | use injector; |
||
17 | |||
18 | /** |
||
19 | * @var Container キャッシュ依存コンテナ |
||
20 | */ |
||
21 | private Container $cacheContainer; |
||
22 | |||
23 | /** |
||
24 | * @var string キャッシュ接頭辞 |
||
25 | */ |
||
26 | private string $cachePrefix; |
||
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | public function __construct(Container $cacheContainer) |
||
32 | { |
||
33 | $this->cacheContainer = $cacheContainer; |
||
34 | $this->cachePrefix = $this->cacheContainer->cachePrefix . '.' . $this->cacheContainer->classPrefix . '.'; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | public function add($key, $value, $ttl = 0, $overwrite = false): bool |
||
41 | { |
||
42 | if (!$this->isAvailableCacheLibrary()) { |
||
43 | return false; |
||
44 | } |
||
45 | |||
46 | if (is_array($value)) { |
||
47 | $value = json_encode($value); |
||
48 | } |
||
49 | |||
50 | $result = false; |
||
51 | if ($ttl > 0) { |
||
52 | if ($overwrite) { |
||
53 | $result = $this->cacheContainer->driver->setEx($key, $ttl, $value); |
||
54 | } else { |
||
55 | $result = $this->cacheContainer->driver->set($key, $value, ['nx', 'ex' => $ttl]); |
||
56 | } |
||
57 | } else { |
||
58 | if ($overwrite) { |
||
59 | $result = $this->cacheContainer->driver->set($key, $value); |
||
60 | } else { |
||
61 | $result = $this->cacheContainer->driver->setNx($key, $value); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | $this->logger->info("Execute cache save: " . $key); |
||
66 | |||
67 | return $result; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function get($key) |
||
74 | { |
||
75 | if (!$this->isAvailableCacheLibrary()) { |
||
76 | return null; |
||
77 | } |
||
78 | |||
79 | $result = $this->cacheContainer->driver->get($key); |
||
80 | $this->logger->info("Execute cache read: " . $key); |
||
81 | |||
82 | if ($result !== false) { |
||
83 | $this->logger->info("Execute cache read: " . $key); |
||
84 | } else { |
||
85 | $this->logger->warn("Failed to read cache: " . $key); |
||
86 | $result = null; |
||
87 | } |
||
88 | |||
89 | return $result; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function delete($key): bool |
||
96 | { |
||
97 | if (!$this->isAvailableCacheLibrary()) { |
||
98 | return false; |
||
99 | } |
||
100 | |||
101 | if ($this->cacheContainer->driver->delete($key) > 0) { |
||
102 | $this->logger->info("Execute cache cleared: " . $key); |
||
103 | return true; |
||
104 | } else { |
||
105 | $this->logger->warn("Failed to clear cache: " . $key); |
||
106 | return false; |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | public function clear(): bool |
||
114 | { |
||
115 | if (!$this->isAvailableCacheLibrary()) { |
||
116 | return false; |
||
117 | } |
||
118 | |||
119 | $it = null; |
||
120 | $result = 1; |
||
121 | $this->cacheContainer->driver->setOption($this->cacheContainer->redisOptPrefix, ""); |
||
122 | while ($keys = $this->cacheContainer->driver->scan($it, "*")) { |
||
123 | $result *= $this->cacheContainer->driver->delete($keys); |
||
124 | } |
||
125 | $this->cacheContainer->driver->setOption($this->cacheContainer->redisOptPrefix, $this->cachePrefix); |
||
126 | |||
127 | if ($result > 0) { |
||
128 | $this->logger->info("Execute all cache cleared: " . $this->cachePrefix . "*"); |
||
129 | return true; |
||
130 | } else { |
||
131 | $this->logger->warn("Failed to clear all cache: " . $this->cachePrefix . "*"); |
||
132 | return false; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * キャッシュライブラリが使用可能か検査する |
||
138 | * @return bool 使用可能でtrue |
||
139 | */ |
||
140 | private function isAvailableCacheLibrary(): bool |
||
141 | { |
||
142 | if ($this->cacheContainer->available) { |
||
143 | return true; |
||
144 | } |
||
145 | |||
146 | $this->logger->warn("Redis cache library is unavailable."); |
||
147 | |||
148 | return false; |
||
149 | } |
||
150 | } |
||
151 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths