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 bool キャッシュ使用可能フラグ |
20
|
|
|
*/ |
21
|
|
|
private $isAvairable; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string キャッシュ接頭辞 |
25
|
|
|
*/ |
26
|
|
|
private $cachePrefix; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* constructor |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Container $container) |
32
|
|
|
{ |
33
|
|
|
$this->isAvairable = extension_loaded('apcu'); |
34
|
|
|
$this->cachePrefix = $container->cachePrefix; |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function add($key, $value, $ttl = 0, $overrite = false): bool |
41
|
|
|
{ |
42
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
$key = $this->cachePrefix . $key; |
46
|
|
|
|
47
|
|
|
$result = $overrite ? apcu_store($key, $value, $ttl) : apcu_add($key, $value, $ttl); |
48
|
|
|
$this->logger->info("Execute cache save: " . $key); |
|
|
|
|
49
|
|
|
|
50
|
|
|
return $result; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function get($key) |
57
|
|
|
{ |
58
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
$key = $this->cachePrefix . $key; |
62
|
|
|
$value = apcu_fetch($key, $isSuccess); |
|
|
|
|
63
|
|
|
|
64
|
|
|
if ($isSuccess) { |
65
|
|
|
$this->logger->info("Execute cache read: " . $key); |
|
|
|
|
66
|
|
|
} else { |
67
|
|
|
$this->logger->warn("Failed to read cache: " . $key); |
|
|
|
|
68
|
|
|
$value = null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function delete($key): bool |
78
|
|
|
{ |
79
|
|
|
if (!$this->isAvailableCacheLibrary()) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
$key = $this->cachePrefix . $key; |
83
|
|
|
|
84
|
|
|
return apcu_delete($key); |
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
|
|
|
return apcu_delete(new \APCUIterator('/^' . $this->cachePrefix . '/', APC_ITER_KEY)); |
98
|
|
|
} else { |
99
|
|
|
return apcu_clear_cache(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* キャッシュライブラリが使用可能か検査する |
105
|
|
|
* @return bool 使用可能でtrue |
106
|
|
|
*/ |
107
|
|
|
private function isAvailableCacheLibrary(): bool |
108
|
|
|
{ |
109
|
|
|
if ($this->isAvairable) { |
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->logger->warn("APCu cache library is unavailable."); |
|
|
|
|
114
|
|
|
|
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write 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.