1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace voku\cache; |
6
|
|
|
|
7
|
|
|
class CacheChain implements iCache |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var Cache[] |
11
|
|
|
*/ |
12
|
|
|
private $caches = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* __construct |
16
|
|
|
* |
17
|
|
|
* @param array $caches |
18
|
|
|
*/ |
19
|
1 |
|
public function __construct(array $caches = []) |
20
|
|
|
{ |
21
|
1 |
|
\array_map( |
22
|
|
|
[ |
23
|
1 |
|
$this, |
24
|
1 |
|
'addCache', |
25
|
|
|
], |
26
|
1 |
|
$caches |
27
|
|
|
); |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* get caches |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function getCaches(): array |
36
|
|
|
{ |
37
|
|
|
return $this->caches; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* add cache |
42
|
|
|
* |
43
|
|
|
* @param Cache $cache |
44
|
|
|
* @param bool $prepend |
45
|
|
|
*/ |
46
|
1 |
|
public function addCache(Cache $cache, $prepend = true) |
47
|
|
|
{ |
48
|
1 |
|
if ($prepend) { |
49
|
1 |
|
\array_unshift($this->caches, $cache); |
50
|
|
|
} else { |
51
|
|
|
$this->caches[] = $cache; |
52
|
|
|
} |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function getItem(string $key) |
59
|
|
|
{ |
60
|
|
|
foreach ($this->caches as $cache) { |
61
|
|
|
if ($cache->existsItem($key)) { |
62
|
|
|
return $cache->getItem($key); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the "isReady" state. |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
1 |
|
public function getCacheIsReady(): bool |
75
|
|
|
{ |
76
|
1 |
|
foreach ($this->caches as $cache) { |
77
|
1 |
|
if (!$cache->getCacheIsReady()) { |
78
|
1 |
|
return false; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function setItem(string $key, $value, $ttl = null): bool |
89
|
|
|
{ |
90
|
|
|
// init |
91
|
|
|
$results = []; |
92
|
|
|
|
93
|
|
|
foreach ($this->caches as $cache) { |
94
|
|
|
$results[] = $cache->setItem($key, $value, $ttl); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return !\in_array(false, $results, true); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function setItemToDate(string $key, $value, \DateTimeInterface $date): bool |
104
|
|
|
{ |
105
|
|
|
// init |
106
|
|
|
$results = []; |
107
|
|
|
|
108
|
|
|
foreach ($this->caches as $cache) { |
109
|
|
|
$results[] = $cache->setItemToDate($key, $value, $date); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return !\in_array(false, $results, true); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* !!! Set the prefix. !!! |
117
|
|
|
* |
118
|
|
|
* WARNING: Do not use if you don't know what you do. Because this will overwrite the default prefix. |
119
|
|
|
* |
120
|
|
|
* @param string $prefix |
121
|
|
|
*/ |
122
|
|
|
public function setPrefix(string $prefix) |
123
|
|
|
{ |
124
|
|
|
foreach ($this->caches as $cache) { |
125
|
|
|
$cache->setPrefix($prefix); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
public function removeItem(string $key): bool |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
// init |
135
|
|
|
$results = []; |
136
|
|
|
|
137
|
|
|
foreach ($this->caches as $cache) { |
138
|
|
|
$results[] = $cache->removeItem($key); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return !\in_array(false, $results, true); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function existsItem(string $key): bool |
148
|
|
|
{ |
149
|
|
|
foreach ($this->caches as $cache) { |
150
|
|
|
if ($cache->existsItem($key)) { |
151
|
|
|
return true; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
public function removeAll(): bool |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
// init |
164
|
|
|
$results = []; |
165
|
|
|
|
166
|
|
|
foreach ($this->caches as $cache) { |
167
|
|
|
$results[] = $cache->removeAll(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return !\in_array(false, $results, true); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.