1 | <?php |
||
13 | class Apcu implements ICache |
||
14 | { |
||
15 | use injector; |
||
16 | |||
17 | /** |
||
18 | * @var Container キャッシュ依存コンテナ |
||
19 | */ |
||
20 | private $cacheContainer; |
||
21 | |||
22 | /** |
||
23 | * @var string キャッシュ接頭辞 |
||
24 | */ |
||
25 | private $cachePrefix; |
||
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | public function __construct(Container $cacheContainer) |
||
31 | { |
||
32 | $this->cacheContainer = $cacheContainer; |
||
33 | $this->cachePrefix = $this->cacheContainer->cachePrefix . '.'; |
||
34 | if (!empty($this->cacheContainer->classPrefix)) { |
||
35 | $this->cachePrefix .= $this->cacheContainer->classPrefix . '.'; |
||
36 | } |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | 4 | public function add($key, $value, $ttl = 0, $overwrite = false): bool |
|
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | 4 | public function get($key) |
|
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | 1 | public function delete($key): bool |
|
81 | { |
||
82 | 1 | if (!$this->isAvailableCacheLibrary()) { |
|
83 | return false; |
||
84 | } |
||
85 | 1 | $key = $this->cachePrefix . $key; |
|
86 | |||
87 | 1 | if ($this->cacheContainer->driver->delegate("apcu_delete", [$key])) { |
|
88 | 1 | $this->logger->info("Execute cache cleared: " . $key); |
|
89 | 1 | return true; |
|
90 | } else { |
||
91 | $this->logger->warn("Failed to clear cache: " . $key); |
||
92 | return false; |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | 1 | public function clear(): bool |
|
100 | { |
||
101 | 1 | if (!$this->isAvailableCacheLibrary()) { |
|
102 | return false; |
||
103 | } |
||
104 | |||
105 | 1 | if (class_exists('\APCUIterator')) { |
|
106 | 1 | $obj = new \APCUIterator('/^' . $this->cachePrefix . '/', APC_ITER_KEY); |
|
107 | 1 | if ($this->cacheContainer->driver->delegate("apcu_delete", [$obj])) { |
|
108 | 1 | $this->logger->info("Execute all cache cleared: " . $this->cachePrefix . "*"); |
|
109 | 1 | return true; |
|
110 | } |
||
111 | } elseif ($this->cacheContainer->driver->delegate("apcu_clear_cache")) { |
||
112 | $this->logger->info("Execute all cache cleared: " . $this->cachePrefix . "*"); |
||
113 | return true; |
||
114 | } |
||
115 | |||
116 | $this->logger->warn("Failed to clear all cache: " . $this->cachePrefix . "*"); |
||
117 | return false; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * キャッシュライブラリが使用可能か検査する |
||
122 | * @return bool 使用可能でtrue |
||
123 | */ |
||
124 | 4 | private function isAvailableCacheLibrary(): bool |
|
134 | } |
||
135 |