1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\Cache\Fallback; |
4
|
|
|
|
5
|
|
|
use Psr\SimpleCache\CacheInterface; |
6
|
|
|
|
7
|
|
|
final class CacheFallback implements CacheInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var CacheInterface |
11
|
|
|
*/ |
12
|
|
|
private $main; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var CacheInterface |
16
|
|
|
*/ |
17
|
|
|
private $fallback; |
18
|
|
|
|
19
|
|
|
public function __construct(CacheInterface $main, CacheInterface $fallback, CacheInterface ...$fallbacks) |
20
|
|
|
{ |
21
|
|
|
$this->main = $main; |
22
|
|
|
$nextFallback = \array_shift($fallbacks); |
23
|
|
|
$this->fallback = null !== $nextFallback ? new self($fallback, $nextFallback, ...$fallbacks) : $fallback; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
|
|
public function get($key, $default = null) |
30
|
|
|
{ |
31
|
|
|
try { |
32
|
|
|
return $this->main->get($key, $default); |
33
|
|
|
} catch (\Exception $e) { |
34
|
|
|
return $this->fallback->get($key, $default); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritDoc |
40
|
|
|
*/ |
41
|
|
|
public function set($key, $value, $ttl = null) |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
|
|
return $this->main->set($key, $value, $ttl); |
45
|
|
|
} catch (\Exception $e) { |
46
|
|
|
return $this->fallback->set($key, $value, $ttl); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritDoc |
52
|
|
|
*/ |
53
|
|
|
public function delete($key) |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
|
|
return $this->main->delete($key); |
57
|
|
|
} catch (\Exception $e) { |
58
|
|
|
return $this->fallback->delete($key); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
|
|
public function clear() |
66
|
|
|
{ |
67
|
|
|
try { |
68
|
|
|
return $this->main->clear(); |
69
|
|
|
} catch (\Exception $e) { |
70
|
|
|
return $this->fallback->clear(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @inheritDoc |
76
|
|
|
*/ |
77
|
|
|
public function getMultiple($keys, $default = null) |
78
|
|
|
{ |
79
|
|
|
try { |
80
|
|
|
return $this->main->getMultiple($keys, $default); |
81
|
|
|
} catch (\Exception $e) { |
82
|
|
|
return $this->fallback->getMultiple($keys, $default); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
|
|
public function setMultiple($values, $ttl = null) |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
|
|
return $this->main->setMultiple($values, $ttl); |
93
|
|
|
} catch (\Exception $e) { |
94
|
|
|
return $this->fallback->setMultiple($values, $ttl); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritDoc |
100
|
|
|
*/ |
101
|
|
|
public function deleteMultiple($keys) |
102
|
|
|
{ |
103
|
|
|
try { |
104
|
|
|
return $this->main->deleteMultiple($keys); |
105
|
|
|
} catch (\Exception $e) { |
106
|
|
|
return $this->fallback->deleteMultiple($keys); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritDoc |
112
|
|
|
*/ |
113
|
|
|
public function has($key) |
114
|
|
|
{ |
115
|
|
|
try { |
116
|
|
|
return $this->main->has($key); |
117
|
|
|
} catch (\Exception $e) { |
118
|
|
|
return $this->fallback->has($key); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|