Skip to content

Commit f395529

Browse files
thomasglachantNyholm
authored andcommitted
Add optional skiponfailure parameter to CachePoolChain
Add optional skiponfailure parameter top CachePoolChain
1 parent ec5e63f commit f395529

File tree

2 files changed

+129
-40
lines changed

2 files changed

+129
-40
lines changed

CachePoolChain.php

Lines changed: 127 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Cache\Taggable\TaggablePoolInterface;
1515
use Psr\Cache\CacheItemInterface;
1616
use Psr\Cache\CacheItemPoolInterface;
17+
use Symfony\Component\OptionsResolver\OptionsResolver;
1718

1819
/**
1920
* @author Tobias Nyholm <[email protected]>
@@ -24,16 +25,60 @@ class CachePoolChain implements CacheItemPoolInterface, TaggablePoolInterface
2425
* @type CacheItemPoolInterface[]
2526
*/
2627
private $pools;
28+
/**
29+
* @type array
30+
*/
31+
private $options;
2732

2833
/**
2934
* @param array $pools
35+
* @param array $options
36+
*/
37+
public function __construct(array $pools, array $options = [])
38+
{
39+
$this->pools = $pools;
40+
$this->options = $options;
41+
42+
$resolver = new OptionsResolver();
43+
$this->configureOptions($resolver);
44+
45+
$this->options = $resolver->resolve($options);
46+
}
47+
48+
/**
49+
* @param mixed $poolKey
50+
* @param \Exception $exception
51+
*
52+
* @throws \Exception
53+
*/
54+
private function onPoolException($poolKey, \Exception $exception)
55+
{
56+
if (!$this->options['skip_on_failure']) {
57+
throw $exception;
58+
}
59+
unset($this->pools[$poolKey]);
60+
}
61+
62+
/**
63+
* @param OptionsResolver $resolver
64+
*/
65+
public function configureOptions(OptionsResolver $resolver)
66+
{
67+
$resolver->setDefaults([
68+
'skip_on_failure' => false,
69+
]);
70+
}
71+
72+
/**
73+
* @return array|\Psr\Cache\CacheItemPoolInterface[]
3074
*/
31-
public function __construct(array $pools)
75+
public function getPools()
3276
{
33-
if (empty($pools)) {
34-
throw new \LogicException('At least one pool is required for the chain.');
77+
if (empty($this->pools)) {
78+
throw new \LogicException('No valid cache pool available for the chain.');
3579
}
36-
$this->pools = $pools;
80+
81+
return $this->pools;
3782
}
3883

3984
/**
@@ -45,15 +90,20 @@ public function getItem($key)
4590
$result = null;
4691
$needsSave = [];
4792

48-
foreach ($this->pools as $pool) {
49-
$item = $pool->getItem($key);
50-
if ($item->isHit()) {
51-
$found = true;
52-
$result = $item;
53-
break;
54-
}
93+
foreach ($this->getPools() as $poolKey => $pool) {
94+
try {
95+
$item = $pool->getItem($key);
96+
97+
if ($item->isHit()) {
98+
$found = true;
99+
$result = $item;
100+
break;
101+
}
55102

56-
$needsSave[] = $pool;
103+
$needsSave[] = $pool;
104+
} catch (\Exception $e) {
105+
$this->onPoolException($poolKey, $e);
106+
}
57107
}
58108

59109
if ($found) {
@@ -72,18 +122,24 @@ public function getItem($key)
72122
*/
73123
public function getItems(array $keys = [])
74124
{
75-
$hits = [];
76-
foreach ($this->pools as $pool) {
77-
$items = $pool->getItems($keys);
78-
/** @type CacheItemInterface $item */
79-
foreach ($items as $item) {
80-
if ($item->isHit()) {
81-
$hits[$item->getKey()] = $item;
125+
$hits = [];
126+
$items = [];
127+
foreach ($this->getPools() as $poolKey => $pool) {
128+
try {
129+
$items = $pool->getItems($keys);
130+
131+
/** @type CacheItemInterface $item */
132+
foreach ($items as $item) {
133+
if ($item->isHit()) {
134+
$hits[$item->getKey()] = $item;
135+
}
82136
}
83-
}
84137

85-
if (count($hits) === count($keys)) {
86-
return $hits;
138+
if (count($hits) === count($keys)) {
139+
return $hits;
140+
}
141+
} catch (\Exception $e) {
142+
$this->onPoolException($poolKey, $e);
87143
}
88144
}
89145

@@ -96,9 +152,13 @@ public function getItems(array $keys = [])
96152
*/
97153
public function hasItem($key)
98154
{
99-
foreach ($this->pools as $pool) {
100-
if ($pool->hasItem($key)) {
101-
return true;
155+
foreach ($this->getPools() as $poolKey => $pool) {
156+
try {
157+
if ($pool->hasItem($key)) {
158+
return true;
159+
}
160+
} catch (\Exception $e) {
161+
$this->onPoolException($poolKey, $e);
102162
}
103163
}
104164

@@ -111,8 +171,12 @@ public function hasItem($key)
111171
public function clear()
112172
{
113173
$result = true;
114-
foreach ($this->pools as $pool) {
115-
$result = $result && $pool->clear();
174+
foreach ($this->getPools() as $poolKey => $pool) {
175+
try {
176+
$result = $result && $pool->clear();
177+
} catch (\Exception $e) {
178+
$this->onPoolException($poolKey, $e);
179+
}
116180
}
117181

118182
return $result;
@@ -124,8 +188,12 @@ public function clear()
124188
public function deleteItem($key)
125189
{
126190
$result = true;
127-
foreach ($this->pools as $pool) {
128-
$result = $result && $pool->deleteItem($key);
191+
foreach ($this->getPools() as $poolKey => $pool) {
192+
try {
193+
$result = $result && $pool->deleteItem($key);
194+
} catch (\Exception $e) {
195+
$this->onPoolException($poolKey, $e);
196+
}
129197
}
130198

131199
return $result;
@@ -137,8 +205,12 @@ public function deleteItem($key)
137205
public function deleteItems(array $keys)
138206
{
139207
$result = true;
140-
foreach ($this->pools as $pool) {
141-
$result = $result && $pool->deleteItems($keys);
208+
foreach ($this->getPools() as $poolKey => $pool) {
209+
try {
210+
$result = $result && $pool->deleteItems($keys);
211+
} catch (\Exception $e) {
212+
$this->onPoolException($poolKey, $e);
213+
}
142214
}
143215

144216
return $result;
@@ -150,8 +222,12 @@ public function deleteItems(array $keys)
150222
public function save(CacheItemInterface $item)
151223
{
152224
$result = true;
153-
foreach ($this->pools as $pool) {
154-
$result = $result && $pool->save($item);
225+
foreach ($this->getPools() as $poolKey => $pool) {
226+
try {
227+
$result = $result && $pool->save($item);
228+
} catch (\Exception $e) {
229+
$this->onPoolException($poolKey, $e);
230+
}
155231
}
156232

157233
return $result;
@@ -163,8 +239,12 @@ public function save(CacheItemInterface $item)
163239
public function saveDeferred(CacheItemInterface $item)
164240
{
165241
$result = true;
166-
foreach ($this->pools as $pool) {
167-
$result = $result && $pool->saveDeferred($item);
242+
foreach ($this->getPools() as $poolKey => $pool) {
243+
try {
244+
$result = $result && $pool->saveDeferred($item);
245+
} catch (\Exception $e) {
246+
$this->onPoolException($poolKey, $e);
247+
}
168248
}
169249

170250
return $result;
@@ -176,8 +256,12 @@ public function saveDeferred(CacheItemInterface $item)
176256
public function commit()
177257
{
178258
$result = true;
179-
foreach ($this->pools as $pool) {
180-
$result = $result && $pool->commit();
259+
foreach ($this->getPools() as $poolKey => $pool) {
260+
try {
261+
$result = $result && $pool->commit();
262+
} catch (\Exception $e) {
263+
$this->onPoolException($poolKey, $e);
264+
}
181265
}
182266

183267
return $result;
@@ -189,9 +273,13 @@ public function commit()
189273
public function clearTags(array $tags)
190274
{
191275
$result = true;
192-
foreach ($this->pools as $pool) {
276+
foreach ($this->getPools() as $poolKey => $pool) {
193277
if ($pool instanceof TaggablePoolInterface) {
194-
$result = $result && $pool->clearTags($tags);
278+
try {
279+
$result = $result && $pool->clearTags($tags);
280+
} catch (\Exception $e) {
281+
$this->onPoolException($poolKey, $e);
282+
}
195283
}
196284
}
197285

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"php": "^5.5|^7.0",
2828
"psr/cache": "^1.0",
2929
"cache/adapter-common": "^0.3",
30-
"cache/taggable-cache": "^0.4"
30+
"cache/taggable-cache": "^0.4",
31+
"symfony/options-resolver": "^2.7|^3.0"
3132
},
3233
"require-dev": {
3334
"phpunit/phpunit": "^4.0|^5.1",

0 commit comments

Comments
 (0)