Skip to content

Commit 6486cbd

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 5cfc235 commit 6486cbd

File tree

4 files changed

+125
-7
lines changed

4 files changed

+125
-7
lines changed

AbstractCachePool.php

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,25 @@
1111

1212
namespace Cache\Adapter\Common;
1313

14+
use Cache\Adapter\Common\Exception\CacheException;
15+
use Cache\Adapter\Common\Exception\CachePoolException;
1416
use Cache\Adapter\Common\Exception\InvalidArgumentException;
1517
use Psr\Cache\CacheItemInterface;
1618
use Psr\Cache\CacheItemPoolInterface;
19+
use Psr\Log\LoggerAwareInterface;
20+
use Psr\Log\LoggerInterface;
1721

1822
/**
1923
* @author Aaron Scherer <[email protected]>
2024
* @author Tobias Nyholm <[email protected]>
2125
*/
22-
abstract class AbstractCachePool implements CacheItemPoolInterface
26+
abstract class AbstractCachePool implements CacheItemPoolInterface, LoggerAwareInterface
2327
{
28+
/**
29+
* @type LoggerInterface
30+
*/
31+
private $logger;
32+
2433
/**
2534
* @type CacheItemInterface[] deferred
2635
*/
@@ -80,7 +89,11 @@ public function getItem($key)
8089
}
8190

8291
$func = function () use ($key) {
83-
return $this->fetchObjectFromCache($key);
92+
try {
93+
return $this->fetchObjectFromCache($key);
94+
} catch (\Exception $e) {
95+
$this->handleException($e, __FUNCTION__);
96+
}
8497
};
8598

8699
return new CacheItem($key, $func);
@@ -104,7 +117,11 @@ public function getItems(array $keys = [])
104117
*/
105118
public function hasItem($key)
106119
{
107-
return $this->getItem($key)->isHit();
120+
try {
121+
return $this->getItem($key)->isHit();
122+
} catch (\Exception $e) {
123+
$this->handleException($e, __FUNCTION__);
124+
}
108125
}
109126

110127
/**
@@ -115,15 +132,23 @@ public function clear()
115132
// Clear the deferred items
116133
$this->deferred = [];
117134

118-
return $this->clearAllObjectsFromCache();
135+
try {
136+
return $this->clearAllObjectsFromCache();
137+
} catch (\Exception $e) {
138+
$this->handleException($e, __FUNCTION__);
139+
}
119140
}
120141

121142
/**
122143
* {@inheritdoc}
123144
*/
124145
public function deleteItem($key)
125146
{
126-
return $this->deleteItems([$key]);
147+
try {
148+
return $this->deleteItems([$key]);
149+
} catch (\Exception $e) {
150+
$this->handleException($e, __FUNCTION__);
151+
}
127152
}
128153

129154
/**
@@ -158,7 +183,11 @@ public function save(CacheItemInterface $item)
158183
}
159184
}
160185

161-
return $this->storeItemInCache($item, $timeToLive);
186+
try {
187+
return $this->storeItemInCache($item, $timeToLive);
188+
} catch (\Exception $e) {
189+
$this->handleException($e, __FUNCTION__);
190+
}
162191
}
163192

164193
/**
@@ -207,4 +236,49 @@ protected function validateKey($key)
207236
));
208237
}
209238
}
239+
240+
/**
241+
* @param LoggerInterface $logger
242+
*/
243+
public function setLogger(LoggerInterface $logger)
244+
{
245+
$this->logger = $logger;
246+
}
247+
248+
/**
249+
* Logs with an arbitrary level if the logger exists.
250+
*
251+
* @param mixed $level
252+
* @param string $message
253+
* @param array $context
254+
*/
255+
protected function log($level, $message, array $context = [])
256+
{
257+
if ($this->logger !== null) {
258+
$this->logger->log($level, $message, $context);
259+
}
260+
}
261+
262+
/**
263+
* Log exception and rethrow it.
264+
*
265+
* @param \Exception $e
266+
* @param string $function
267+
*
268+
* @throws CachePoolException
269+
*/
270+
private function handleException(\Exception $e, $function)
271+
{
272+
$level = 'alert';
273+
if ($e instanceof InvalidArgumentException) {
274+
$level = 'warning';
275+
}
276+
277+
$this->log($level, $e->getMessage(), ['exception' => $e]);
278+
if (!$e instanceof CacheException) {
279+
$e = new CachePoolException(sprintf('Exception thrown when executing "%s". ', $function), 0, $e);
280+
}
281+
282+
throw $e;
283+
}
210284
}

Exception/CacheException.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 <[email protected]>, Tobias Nyholm <[email protected]>
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\Common\Exception;
13+
14+
use Psr\Cache\CacheException as CacheExceptionInterface;
15+
16+
/**
17+
* A base exception. All exceptions in this organization will extend this exception.
18+
*
19+
* @author Tobias Nyholm <[email protected]>
20+
*/
21+
abstract class CacheException extends \RuntimeException implements CacheExceptionInterface
22+
{
23+
}

Exception/CachePoolException.php

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 <[email protected]>, Tobias Nyholm <[email protected]>
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\Common\Exception;
13+
14+
/**
15+
* If an exception is caused by a pool or by the cache storage.
16+
*
17+
* @author Tobias Nyholm <[email protected]>
18+
*/
19+
class CachePoolException extends CacheException
20+
{
21+
}

Exception/InvalidArgumentException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
namespace Cache\Adapter\Common\Exception;
1313

14-
class InvalidArgumentException extends \InvalidArgumentException implements \Psr\Cache\InvalidArgumentException
14+
class InvalidArgumentException extends CacheException implements \Psr\Cache\InvalidArgumentException
1515
{
1616
}

0 commit comments

Comments
 (0)