Skip to content

Commit 1b68d6d

Browse files
committed
Added a base exception and logging (#51)
Added a base exception and make sure we do not throw any other exception that this, Added loggers to the pools to log exceptions
1 parent f395529 commit 1b68d6d

File tree

4 files changed

+115
-27
lines changed

4 files changed

+115
-27
lines changed

CachePoolChain.php

Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,26 @@
1111

1212
namespace Cache\Adapter\Chain;
1313

14+
use Cache\Adapter\Chain\Exception\NoPoolAvailableException;
15+
use Cache\Adapter\Chain\Exception\PoolFailedException;
16+
use Cache\Adapter\Common\Exception\CachePoolException;
1417
use Cache\Taggable\TaggablePoolInterface;
1518
use Psr\Cache\CacheItemInterface;
1619
use Psr\Cache\CacheItemPoolInterface;
20+
use Psr\Log\LoggerAwareInterface;
21+
use Psr\Log\LoggerInterface;
1722
use Symfony\Component\OptionsResolver\OptionsResolver;
1823

1924
/**
2025
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2126
*/
22-
class CachePoolChain implements CacheItemPoolInterface, TaggablePoolInterface
27+
class CachePoolChain implements CacheItemPoolInterface, TaggablePoolInterface, LoggerAwareInterface
2328
{
29+
/**
30+
* @type LoggerInterface
31+
*/
32+
private $logger;
33+
2434
/**
2535
* @type CacheItemPoolInterface[]
2636
*/
@@ -32,7 +42,10 @@ class CachePoolChain implements CacheItemPoolInterface, TaggablePoolInterface
3242

3343
/**
3444
* @param array $pools
35-
* @param array $options
45+
* @param array $options {
46+
*
47+
* @type bool $skip_on_failure If true we will remove a pool form the chain if it fails.
48+
* }
3649
*/
3750
public function __construct(array $pools, array $options = [])
3851
{
@@ -46,16 +59,24 @@ public function __construct(array $pools, array $options = [])
4659
}
4760

4861
/**
49-
* @param mixed $poolKey
50-
* @param \Exception $exception
62+
* @param string $poolKey
63+
* @param string $operation
64+
* @param CachePoolException $exception
5165
*
52-
* @throws \Exception
66+
* @throws PoolFailedException
5367
*/
54-
private function onPoolException($poolKey, \Exception $exception)
68+
private function handleException($poolKey, $operation, CachePoolException $exception)
5569
{
5670
if (!$this->options['skip_on_failure']) {
5771
throw $exception;
5872
}
73+
74+
$this->log(
75+
'warning',
76+
sprintf('Removing pool "%s" from chain because it threw an exception when executing "%s"', $poolKey, $operation),
77+
['exception' => $exception]
78+
);
79+
5980
unset($this->pools[$poolKey]);
6081
}
6182

@@ -75,7 +96,7 @@ public function configureOptions(OptionsResolver $resolver)
7596
public function getPools()
7697
{
7798
if (empty($this->pools)) {
78-
throw new \LogicException('No valid cache pool available for the chain.');
99+
throw new NoPoolAvailableException('No valid cache pool available for the chain.');
79100
}
80101

81102
return $this->pools;
@@ -101,8 +122,8 @@ public function getItem($key)
101122
}
102123

103124
$needsSave[] = $pool;
104-
} catch (\Exception $e) {
105-
$this->onPoolException($poolKey, $e);
125+
} catch (CachePoolException $e) {
126+
$this->handleException($poolKey, __FUNCTION__, $e);
106127
}
107128
}
108129

@@ -138,8 +159,8 @@ public function getItems(array $keys = [])
138159
if (count($hits) === count($keys)) {
139160
return $hits;
140161
}
141-
} catch (\Exception $e) {
142-
$this->onPoolException($poolKey, $e);
162+
} catch (CachePoolException $e) {
163+
$this->handleException($poolKey, __FUNCTION__, $e);
143164
}
144165
}
145166

@@ -157,8 +178,8 @@ public function hasItem($key)
157178
if ($pool->hasItem($key)) {
158179
return true;
159180
}
160-
} catch (\Exception $e) {
161-
$this->onPoolException($poolKey, $e);
181+
} catch (CachePoolException $e) {
182+
$this->handleException($poolKey, __FUNCTION__, $e);
162183
}
163184
}
164185

@@ -174,8 +195,8 @@ public function clear()
174195
foreach ($this->getPools() as $poolKey => $pool) {
175196
try {
176197
$result = $result && $pool->clear();
177-
} catch (\Exception $e) {
178-
$this->onPoolException($poolKey, $e);
198+
} catch (CachePoolException $e) {
199+
$this->handleException($poolKey, __FUNCTION__, $e);
179200
}
180201
}
181202

@@ -191,8 +212,8 @@ public function deleteItem($key)
191212
foreach ($this->getPools() as $poolKey => $pool) {
192213
try {
193214
$result = $result && $pool->deleteItem($key);
194-
} catch (\Exception $e) {
195-
$this->onPoolException($poolKey, $e);
215+
} catch (CachePoolException $e) {
216+
$this->handleException($poolKey, __FUNCTION__, $e);
196217
}
197218
}
198219

@@ -208,8 +229,8 @@ public function deleteItems(array $keys)
208229
foreach ($this->getPools() as $poolKey => $pool) {
209230
try {
210231
$result = $result && $pool->deleteItems($keys);
211-
} catch (\Exception $e) {
212-
$this->onPoolException($poolKey, $e);
232+
} catch (CachePoolException $e) {
233+
$this->handleException($poolKey, __FUNCTION__, $e);
213234
}
214235
}
215236

@@ -225,8 +246,8 @@ public function save(CacheItemInterface $item)
225246
foreach ($this->getPools() as $poolKey => $pool) {
226247
try {
227248
$result = $result && $pool->save($item);
228-
} catch (\Exception $e) {
229-
$this->onPoolException($poolKey, $e);
249+
} catch (CachePoolException $e) {
250+
$this->handleException($poolKey, __FUNCTION__, $e);
230251
}
231252
}
232253

@@ -242,8 +263,8 @@ public function saveDeferred(CacheItemInterface $item)
242263
foreach ($this->getPools() as $poolKey => $pool) {
243264
try {
244265
$result = $result && $pool->saveDeferred($item);
245-
} catch (\Exception $e) {
246-
$this->onPoolException($poolKey, $e);
266+
} catch (CachePoolException $e) {
267+
$this->handleException($poolKey, __FUNCTION__, $e);
247268
}
248269
}
249270

@@ -259,8 +280,8 @@ public function commit()
259280
foreach ($this->getPools() as $poolKey => $pool) {
260281
try {
261282
$result = $result && $pool->commit();
262-
} catch (\Exception $e) {
263-
$this->onPoolException($poolKey, $e);
283+
} catch (CachePoolException $e) {
284+
$this->handleException($poolKey, __FUNCTION__, $e);
264285
}
265286
}
266287

@@ -277,12 +298,34 @@ public function clearTags(array $tags)
277298
if ($pool instanceof TaggablePoolInterface) {
278299
try {
279300
$result = $result && $pool->clearTags($tags);
280-
} catch (\Exception $e) {
281-
$this->onPoolException($poolKey, $e);
301+
} catch (CachePoolException $e) {
302+
$this->handleException($poolKey, __FUNCTION__, $e);
282303
}
283304
}
284305
}
285306

286307
return $result;
287308
}
309+
310+
/**
311+
* @param LoggerInterface $logger
312+
*/
313+
public function setLogger(LoggerInterface $logger)
314+
{
315+
$this->logger = $logger;
316+
}
317+
318+
/**
319+
* Logs with an arbitrary level if the logger exists.
320+
*
321+
* @param mixed $level
322+
* @param string $message
323+
* @param array $context
324+
*/
325+
protected function log($level, $message, array $context = [])
326+
{
327+
if ($this->logger !== null) {
328+
$this->logger->log($level, $message, $context);
329+
}
330+
}
288331
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Chain\Exception;
13+
14+
use Cache\Adapter\Common\Exception\CachePoolException;
15+
16+
/**
17+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
18+
*/
19+
class NoPoolAvailableException extends CachePoolException
20+
{
21+
}

Exception/PoolFailedException.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Adapter\Chain\Exception;
13+
14+
use Cache\Adapter\Common\Exception\CachePoolException;
15+
16+
/**
17+
* When a cache pool fails with its operation.
18+
*
19+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
20+
*/
21+
class PoolFailedException extends CachePoolException
22+
{
23+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"require": {
2727
"php": "^5.5|^7.0",
2828
"psr/cache": "^1.0",
29+
"psr/log": "^1.0",
2930
"cache/adapter-common": "^0.3",
3031
"cache/taggable-cache": "^0.4",
3132
"symfony/options-resolver": "^2.7|^3.0"

0 commit comments

Comments
 (0)