1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class CacheDriverAbstract |
4
|
|
|
* |
5
|
|
|
* @filesource CacheDriverAbstract.php |
6
|
|
|
* @created 25.05.2017 |
7
|
|
|
* @package chillerlan\SimpleCache |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\SimpleCache; |
14
|
|
|
|
15
|
|
|
use chillerlan\Settings\SettingsContainerInterface; |
16
|
|
|
use Psr\Log\{LoggerAwareInterface, LoggerAwareTrait, NullLogger}; |
17
|
|
|
use Psr\SimpleCache\CacheInterface; |
18
|
|
|
use DateInterval, DateTime, Traversable; |
19
|
|
|
|
20
|
|
|
abstract class CacheDriverAbstract implements CacheInterface, LoggerAwareInterface{ |
21
|
|
|
use LoggerAwareTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \chillerlan\SimpleCache\CacheOptions |
25
|
|
|
*/ |
26
|
|
|
protected $options; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* CacheDriverAbstract constructor. |
30
|
|
|
* |
31
|
|
|
* @param \chillerlan\Settings\SettingsContainerInterface|null $options |
32
|
|
|
*/ |
33
|
|
|
public function __construct(SettingsContainerInterface $options = null){ |
34
|
|
|
$this->options = $options ?? new CacheOptions; |
35
|
|
|
$this->logger = new NullLogger; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** @inheritdoc */ |
39
|
|
|
public function has($key):bool{ |
40
|
|
|
return (bool)$this->get($key); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @inheritdoc */ |
44
|
|
|
public function getMultiple($keys, $default = null):array{ |
45
|
|
|
$data = []; |
46
|
|
|
|
47
|
|
|
foreach($this->getData($keys) as $key){ |
48
|
|
|
$data[$key] = $this->get($key, $default); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $data; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** @inheritdoc */ |
55
|
|
|
public function setMultiple($values, $ttl = null):bool{ |
56
|
|
|
$return = []; |
57
|
|
|
|
58
|
|
|
foreach($this->getData($values) as $key => $value){ |
59
|
|
|
$return[] = $this->set($key, $value, $ttl); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->checkReturn($return); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** @inheritdoc */ |
66
|
|
|
public function deleteMultiple($keys):bool{ |
67
|
|
|
$return = []; |
68
|
|
|
|
69
|
|
|
foreach($this->getData($keys) as $key){ |
70
|
|
|
$return[] = $this->delete($key); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->checkReturn($return); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $key |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
81
|
|
|
*/ |
82
|
|
|
protected function checkKey($key):void{ |
83
|
|
|
|
84
|
|
|
if(!is_string($key) || empty($key)){ |
85
|
|
|
$msg = 'invalid cache key: "'.$key.'"'; |
86
|
|
|
$this->logger->error($msg); |
87
|
|
|
|
88
|
|
|
throw new InvalidArgumentException($msg); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param array $keys |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
protected function checkKeyArray(array $keys):void{ |
99
|
|
|
|
100
|
|
|
foreach($keys as $key){ |
101
|
|
|
$this->checkKey($key); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $data |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
111
|
|
|
*/ |
112
|
|
|
protected function getData($data):array{ |
113
|
|
|
|
114
|
|
|
if(is_array($data)){ |
115
|
|
|
return $data; |
116
|
|
|
} |
117
|
|
|
elseif($data instanceof Traversable){ |
118
|
|
|
return iterator_to_array($data); // @codeCoverageIgnore |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$msg = 'invalid data'; |
122
|
|
|
$this->logger->error($msg); |
123
|
|
|
|
124
|
|
|
throw new InvalidArgumentException($msg); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param mixed $ttl |
129
|
|
|
* |
130
|
|
|
* @return int|null |
131
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
132
|
|
|
*/ |
133
|
|
|
protected function getTTL($ttl):?int{ |
134
|
|
|
|
135
|
|
|
if($ttl instanceof DateInterval){ |
136
|
|
|
return (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
137
|
|
|
} |
138
|
|
|
else if(is_int($ttl) || $ttl === null){ |
139
|
|
|
return $ttl; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$msg = 'invalid ttl'; |
143
|
|
|
$this->logger->error($msg); |
144
|
|
|
|
145
|
|
|
throw new InvalidArgumentException($msg); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param bool[] $booleans |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
protected function checkReturn(array $booleans):bool{ |
154
|
|
|
|
155
|
|
|
foreach($booleans as $bool){ |
156
|
|
|
|
157
|
|
|
if(!(bool)$bool){ |
158
|
|
|
return false; // @codeCoverageIgnore |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return true; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
} |
167
|
|
|
|