1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Cache\Driver; |
3
|
|
|
|
4
|
|
|
use WebStream\DI\Injector; |
5
|
|
|
use WebStream\Container\Container; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Apcu |
9
|
|
|
* @author Ryuichi Tanaka |
10
|
|
|
* @since 2015/07/05 |
11
|
|
|
* @version 0.7 |
12
|
|
|
*/ |
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 |
43
|
|
|
{ |
44
|
4 |
|
if (!$this->isAvailableCacheLibrary()) { |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
4 |
|
$key = $this->cachePrefix . $key; |
48
|
|
|
|
49
|
4 |
|
$result = $overwrite ? $this->cacheContainer->driver->delegate("apcu_store", [$key, $value, $ttl]) : |
50
|
4 |
|
$this->cacheContainer->driver->delegate("apcu_add", [$key, $value, $ttl]); |
51
|
4 |
|
$this->logger->info("Execute cache save: " . $key); |
52
|
|
|
|
53
|
4 |
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
4 |
|
public function get($key) |
60
|
|
|
{ |
61
|
4 |
|
if (!$this->isAvailableCacheLibrary()) { |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
4 |
|
$key = $this->cachePrefix . $key; |
65
|
4 |
|
$value = $this->cacheContainer->driver->delegate("apcu_fetch", [$key]); |
66
|
|
|
|
67
|
4 |
|
if ($value !== false) { |
68
|
2 |
|
$this->logger->info("Execute cache read: " . $key); |
69
|
|
|
} else { |
70
|
2 |
|
$this->logger->warn("Failed to read cache: " . $key); |
71
|
2 |
|
$value = null; |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
return $value; |
75
|
|
|
} |
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 |
125
|
|
|
{ |
126
|
4 |
|
if ($this->cacheContainer->available) { |
127
|
4 |
|
return true; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$this->logger->warn("APCu cache library is unavailable."); |
131
|
|
|
|
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|