1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmp\Cache\Decorator; |
4
|
|
|
|
5
|
|
|
use Cmp\Cache\Backend\TaggedCache; |
6
|
|
|
use Cmp\Cache\Cache; |
7
|
|
|
use Cmp\Cache\Exceptions\BackendOperationFailedException; |
8
|
|
|
use Cmp\Cache\Exceptions\CacheException; |
9
|
|
|
use Cmp\Cache\TagCache; |
10
|
|
|
use Cmp\Cache\Traits\LoggerCacheTrait; |
11
|
|
|
use Exception; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use Psr\Log\LogLevel; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class LoggerCache |
17
|
|
|
* |
18
|
|
|
* @package Cmp\Cache\Decorator |
19
|
|
|
*/ |
20
|
|
|
class LoggerCache implements TagCache, CacheDecorator |
21
|
|
|
{ |
22
|
|
|
use CacheDecoratorTrait, LoggerCacheTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
private $withExceptions; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $logLevel; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* SilentCacheDecorator constructor. |
36
|
|
|
* |
37
|
|
|
* @param Cache $cache |
38
|
|
|
* @param bool $withExceptions |
39
|
|
|
* @param LoggerInterface $logger |
40
|
1 |
|
* @param string $logLevel |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
Cache $cache, |
44
|
|
|
$withExceptions = true, |
45
|
|
|
LoggerInterface $logger = null, |
46
|
1 |
|
$logLevel = LogLevel::ALERT |
47
|
1 |
|
) { |
48
|
1 |
|
$this->cache = $cache; |
49
|
1 |
|
$this->withExceptions = $withExceptions; |
50
|
1 |
|
$this->logger = $logger; |
51
|
|
|
$this->logLevel = $logLevel; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
1 |
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
1 |
|
public function set($key, $value, $timeToLive = null) |
58
|
|
|
{ |
59
|
1 |
|
return $this->call( |
60
|
1 |
|
function() use ($key, $value, $timeToLive) { |
61
|
1 |
|
return $this->cache->set($key, $value, $timeToLive); |
62
|
1 |
|
}, |
63
|
1 |
|
__METHOD__, |
64
|
|
|
[$key, $value, $timeToLive] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
1 |
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
1 |
|
public function has($key) |
72
|
|
|
{ |
73
|
1 |
|
return $this->call( |
74
|
1 |
|
function() use ($key) { |
75
|
1 |
|
return $this->cache->has($key); |
76
|
1 |
|
}, |
77
|
1 |
|
__METHOD__, |
78
|
|
|
[$key] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
1 |
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
1 |
|
public function get($key, $default = null) |
86
|
|
|
{ |
87
|
1 |
|
return $this->call( |
88
|
1 |
|
function() use ($key, $default) { |
89
|
1 |
|
return $this->cache->get($key, $default); |
90
|
1 |
|
}, |
91
|
1 |
|
__METHOD__, |
92
|
|
|
[$key, $default] |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
1 |
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
1 |
|
public function demand($key) |
100
|
|
|
{ |
101
|
1 |
|
return $this->call( |
102
|
1 |
|
function() use ($key) { |
103
|
1 |
|
return $this->cache->demand($key); |
104
|
1 |
|
}, |
105
|
1 |
|
__METHOD__, |
106
|
|
|
[$key] |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
1 |
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
1 |
|
public function delete($key) |
114
|
|
|
{ |
115
|
1 |
|
return $this->call( |
116
|
1 |
|
function() use ($key) { |
117
|
1 |
|
return $this->cache->delete($key); |
118
|
1 |
|
}, |
119
|
1 |
|
__METHOD__, |
120
|
|
|
[$key] |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
1 |
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
1 |
|
public function flush() |
128
|
|
|
{ |
129
|
1 |
|
return $this->call( |
130
|
1 |
|
function() { |
131
|
|
|
return $this->cache->flush(); |
132
|
1 |
|
}, |
133
|
|
|
__METHOD__ |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
1 |
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
1 |
|
public function setItems(array $items, $timeToLive = null) |
141
|
|
|
{ |
142
|
1 |
|
return $this->call( |
143
|
1 |
|
function() use ($items, $timeToLive) { |
144
|
1 |
|
return $this->cache->setItems($items, $timeToLive); |
145
|
1 |
|
}, |
146
|
1 |
|
__METHOD__, |
147
|
|
|
[$items, $timeToLive] |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
1 |
|
* {@inheritdoc} |
153
|
|
|
*/ |
154
|
1 |
|
public function getItems(array $keys) |
155
|
|
|
{ |
156
|
1 |
|
return $this->call( |
157
|
1 |
|
function() use ($keys) { |
158
|
1 |
|
return $this->cache->getItems($keys); |
159
|
1 |
|
}, |
160
|
1 |
|
__METHOD__, |
161
|
|
|
[$keys] |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
1 |
|
* {@inheritdoc} |
167
|
|
|
*/ |
168
|
1 |
|
public function deleteItems(array $keys) |
169
|
|
|
{ |
170
|
1 |
|
return $this->call( |
171
|
1 |
|
function() use ($keys) { |
172
|
1 |
|
return $this->cache->deleteItems($keys); |
173
|
1 |
|
}, |
174
|
1 |
|
__METHOD__, |
175
|
|
|
[$keys] |
176
|
|
|
); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Gets the remaining time to live for an item |
181
|
|
|
* |
182
|
|
|
* @param $key |
183
|
|
|
* |
184
|
1 |
|
* @return int|null |
185
|
|
|
*/ |
186
|
1 |
|
public function getTimeToLive($key) |
187
|
1 |
|
{ |
188
|
1 |
|
return $this->call( |
189
|
1 |
|
function() use($key) { |
190
|
|
|
return $this->cache->getTimeToLive($key); |
191
|
1 |
|
}, |
192
|
|
|
__METHOD__ |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* {@inheritdoc} |
198
|
|
|
*/ |
199
|
|
|
public function tag($tagName) |
200
|
|
|
{ |
201
|
|
|
return new TaggedCache($this, $tagName); |
202
|
|
|
} |
203
|
|
|
|
204
|
1 |
|
/** |
205
|
|
|
* @param callable $callable |
206
|
|
|
* |
207
|
1 |
|
* @param string $method |
208
|
1 |
|
* @param array $arguments |
209
|
1 |
|
* |
210
|
1 |
|
* @return mixed |
211
|
1 |
|
* @throws BackendOperationFailedException |
212
|
1 |
|
* @throws CacheException |
213
|
|
|
*/ |
214
|
|
|
protected function call(callable $callable, $method, $arguments = []) |
215
|
1 |
|
{ |
216
|
1 |
|
try { |
217
|
|
|
return $callable(); |
218
|
|
|
} catch (CacheException $exception) { |
219
|
1 |
|
$this->logException($exception, $method, $arguments); |
220
|
|
|
} catch (Exception $exception) { |
221
|
|
|
$this->logException($exception, $method, $arguments); |
222
|
|
|
$exception = new BackendOperationFailedException($this->getDecoratedCache(), $method, $exception); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
if ($this->withExceptions) { |
226
|
|
|
throw $exception; |
227
|
|
|
} |
228
|
|
|
|
229
|
1 |
|
return false; |
230
|
|
|
} |
231
|
1 |
|
|
232
|
1 |
|
/** |
233
|
1 |
|
* @param Exception $exception |
234
|
1 |
|
* @param string $method |
235
|
1 |
|
* @param array $arguments |
236
|
1 |
|
* |
237
|
1 |
|
* @return bool |
238
|
1 |
|
*/ |
239
|
|
|
protected function logException(Exception $exception, $method, array $arguments) |
240
|
|
|
{ |
241
|
|
|
$this->log($this->logLevel, "Cache $method on cache operation failed: ".$exception->getMessage(), [ |
242
|
|
|
'cache' => get_class($this->cache), |
243
|
|
|
'decorated' => get_class($this->getDecoratedCache()), |
244
|
|
|
'exception' => $exception, |
245
|
|
|
'method' => $method, |
246
|
|
|
'arguments' => $arguments, |
247
|
|
|
]); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|