1111
1212namespace Cache \Adapter \Common ;
1313
14+ use Cache \Adapter \Common \Exception \CacheException ;
15+ use Cache \Adapter \Common \Exception \CachePoolException ;
1416use Cache \Adapter \Common \Exception \InvalidArgumentException ;
1517use Psr \Cache \CacheItemInterface ;
1618use 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}
0 commit comments