1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Cache |
4
|
|
|
* |
5
|
|
|
* @filesource Cache.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\SimpleCache\Drivers\CacheDriverInterface; |
16
|
|
|
use Psr\Log\{LoggerAwareInterface, LoggerInterface, NullLogger}; |
17
|
|
|
use Psr\SimpleCache\CacheInterface; |
18
|
|
|
use DateInterval, DateTime, Traversable; |
19
|
|
|
|
20
|
|
|
class Cache implements CacheInterface, LoggerAwareInterface{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The logger instance. |
24
|
|
|
* |
25
|
|
|
* @var LoggerInterface |
26
|
|
|
*/ |
27
|
|
|
protected $logger; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \chillerlan\SimpleCache\Drivers\CacheDriverInterface |
31
|
|
|
*/ |
32
|
|
|
protected $cacheDriver; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Cache constructor. |
36
|
|
|
* |
37
|
|
|
* @param \chillerlan\SimpleCache\Drivers\CacheDriverInterface $cacheDriver |
38
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
39
|
|
|
*/ |
40
|
|
|
public function __construct(CacheDriverInterface $cacheDriver, LoggerInterface $logger = null){ |
41
|
|
|
$this->cacheDriver = $cacheDriver; |
42
|
|
|
$this->setLogger($logger ?? new NullLogger); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Sets a logger. |
47
|
|
|
* |
48
|
|
|
* @param LoggerInterface $logger |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function setLogger(LoggerInterface $logger):void{ |
53
|
|
|
$this->logger = $logger; |
54
|
|
|
$this->cacheDriver->setLogger($logger); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @inheritdoc */ |
58
|
|
|
public function get($key, $default = null){ |
59
|
|
|
$this->checkKey($key); |
60
|
|
|
|
61
|
|
|
return $this->cacheDriver->get($key, $default); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @inheritdoc */ |
65
|
|
|
public function set($key, $value, $ttl = null):bool{ |
66
|
|
|
$this->checkKey($key); |
67
|
|
|
|
68
|
|
|
return $this->cacheDriver->set($key, $value, $this->getTTL($ttl)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @inheritdoc */ |
72
|
|
|
public function delete($key):bool{ |
73
|
|
|
$this->checkKey($key); |
74
|
|
|
|
75
|
|
|
return $this->cacheDriver->delete($key); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** @inheritdoc */ |
79
|
|
|
public function clear():bool{ |
80
|
|
|
return $this->cacheDriver->clear(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** @inheritdoc */ |
84
|
|
|
public function getMultiple($keys, $default = null):iterable{ |
85
|
|
|
$keys = $this->getData($keys); |
86
|
|
|
$this->checkKeyArray($keys); |
87
|
|
|
|
88
|
|
|
return $this->cacheDriver->getMultiple($keys, $default); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** @inheritdoc */ |
92
|
|
|
public function setMultiple($values, $ttl = null):bool{ |
93
|
|
|
$values = $this->getData($values); |
94
|
|
|
|
95
|
|
|
foreach($values as $key => $value){ |
96
|
|
|
$this->checkKey($key); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->cacheDriver->setMultiple($values, $this->getTTL($ttl)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** @inheritdoc */ |
103
|
|
|
public function deleteMultiple($keys):bool{ |
104
|
|
|
$keys = $this->getData($keys); |
105
|
|
|
$this->checkKeyArray($keys); |
106
|
|
|
|
107
|
|
|
return $this->cacheDriver->deleteMultiple($keys); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** @inheritdoc */ |
111
|
|
|
public function has($key):bool{ |
112
|
|
|
$this->checkKey($key); |
113
|
|
|
|
114
|
|
|
return $this->cacheDriver->has($key); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $key |
119
|
|
|
* |
120
|
|
|
* @return void |
121
|
|
|
* @throws \chillerlan\SimpleCache\InvalidArgumentException |
122
|
|
|
*/ |
123
|
|
|
protected function checkKey($key):void{ |
124
|
|
|
|
125
|
|
|
if(!is_string($key) || empty($key)){ |
126
|
|
|
$msg = 'invalid cache key: "'.$key.'"'; |
127
|
|
|
$this->logger->error($msg); |
128
|
|
|
|
129
|
|
|
throw new InvalidArgumentException($msg); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param array $keys |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
protected function checkKeyArray(array $keys):void{ |
140
|
|
|
|
141
|
|
|
foreach($keys as $key){ |
142
|
|
|
$this->checkKey($key); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param mixed $data |
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
* @throws \chillerlan\SimpleCache\InvalidArgumentException |
152
|
|
|
*/ |
153
|
|
|
protected function getData($data):array{ |
154
|
|
|
|
155
|
|
|
if($data instanceof Traversable){ |
156
|
|
|
return iterator_to_array($data); // @codeCoverageIgnore |
157
|
|
|
} |
158
|
|
|
else if(is_array($data)){ |
159
|
|
|
return $data; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$msg = 'invalid data'; |
163
|
|
|
$this->logger->error($msg); |
164
|
|
|
|
165
|
|
|
throw new InvalidArgumentException($msg); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param mixed $ttl |
170
|
|
|
* |
171
|
|
|
* @return int|null |
172
|
|
|
* @throws \chillerlan\SimpleCache\InvalidArgumentException |
173
|
|
|
*/ |
174
|
|
|
protected function getTTL($ttl):?int{ |
175
|
|
|
|
176
|
|
|
if($ttl instanceof DateInterval){ |
177
|
|
|
return (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
178
|
|
|
} |
179
|
|
|
else if(is_int($ttl) || $ttl === null){ |
180
|
|
|
return $ttl; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$msg = 'invalid ttl'; |
184
|
|
|
$this->logger->error($msg); |
185
|
|
|
|
186
|
|
|
throw new InvalidArgumentException($msg); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $message |
191
|
|
|
* |
192
|
|
|
* @throws \chillerlan\SimpleCache\CacheException |
193
|
|
|
*/ |
194
|
|
|
protected function throwException(string $message){ |
195
|
|
|
$this->logger->error($message); |
196
|
|
|
|
197
|
|
|
throw new InvalidArgumentException($message); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
|