1
|
|
|
<?php |
2
|
|
|
namespace WebStream\Cache\Driver; |
3
|
|
|
|
4
|
|
|
use WebStream\DI\Injector; |
5
|
|
|
use WebStream\Module\Container; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Apcu |
9
|
|
|
* TTLがリクエストによりずれる問題はAPCuの仕様なので対応しない |
10
|
|
|
* @author Ryuichi Tanaka |
11
|
|
|
* @since 2015/07/05 |
12
|
|
|
* @version 0.7 |
13
|
|
|
*/ |
14
|
|
|
class Apcu implements ICache |
15
|
|
|
{ |
16
|
|
|
use injector; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Container キャッシュ依存コンテナ |
20
|
|
|
*/ |
21
|
|
|
private $cacheContainer; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Container $cacheContainer) |
27
|
|
|
{ |
28
|
|
|
$this->cacheContainer = $cacheContainer; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function add($key, $value, $ttl = 0, $overrite = false): bool |
35
|
|
|
{ |
36
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
$key = $this->cacheContainer->cachePrefix . $key; |
|
|
|
|
40
|
|
|
|
41
|
|
|
$result = $overrite ? apcu_store($key, $value, $ttl) : apcu_add($key, $value, $ttl); |
42
|
|
|
$this->logger->info("Execute cache save: " . $key); |
|
|
|
|
43
|
|
|
|
44
|
|
|
return $result; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function get($key) |
51
|
|
|
{ |
52
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
$key = $this->cacheContainer->cachePrefix . $key; |
|
|
|
|
56
|
|
|
$value = apcu_fetch($key, $isSuccess); |
|
|
|
|
57
|
|
|
|
58
|
|
|
if ($isSuccess) { |
59
|
|
|
$this->logger->info("Execute cache read: " . $key); |
|
|
|
|
60
|
|
|
} else { |
61
|
|
|
$this->logger->warn("Failed to read cache: " . $key); |
|
|
|
|
62
|
|
|
$value = null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function delete($key): bool |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
$key = $this->cacheContainer->cachePrefix . $key; |
|
|
|
|
77
|
|
|
|
78
|
|
|
if (apcu_delete($key)) { |
79
|
|
|
$this->logger->info("Execute cache cleared: " . $key); |
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} else { |
82
|
|
|
$this->logger->warn("Failed to clear cache: " . $key); |
|
|
|
|
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function clear(): bool |
91
|
|
|
{ |
92
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (class_exists('\APCUIterator')) { |
97
|
|
|
if (apcu_delete(new \APCUIterator('/^' . $this->cacheContainer->cachePrefix . '/', APC_ITER_KEY))) { |
|
|
|
|
98
|
|
|
$this->logger->info("Execute all cache cleared: " . $key . "*"); |
|
|
|
|
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
} elseif (apcu_clear_cache()) { |
102
|
|
|
$this->logger->info("Execute all cache cleared: " . $key . "*"); |
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->logger->warn("Failed to clear all cache: " . $key . "*"); |
|
|
|
|
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* キャッシュライブラリが使用可能か検査する |
112
|
|
|
* @return bool 使用可能でtrue |
113
|
|
|
*/ |
114
|
|
|
private function isAvailableCacheLibrary(): bool |
115
|
|
|
{ |
116
|
|
|
if ($this->cacheContainer->available) { |
|
|
|
|
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->logger->warn("APCu cache library is unavailable."); |
|
|
|
|
121
|
|
|
|
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
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.