1313
1414use Psr \Cache \CacheItemInterface ;
1515use Psr \Cache \CacheItemPoolInterface ;
16+ use Psr \Log \LoggerAwareInterface ;
17+ use Psr \Log \LoggerAwareTrait ;
1618use Symfony \Component \Cache \CacheItem ;
1719use Symfony \Component \Cache \Exception \InvalidArgumentException ;
1820
1921/**
2022 * @author Nicolas Grekas <p@tchwork.com>
2123 */
22- abstract class AbstractAdapter implements CacheItemPoolInterface
24+ abstract class AbstractAdapter implements CacheItemPoolInterface, LoggerAwareInterface
2325{
26+ use LoggerAwareTrait;
27+
2428 private $ namespace ;
2529 private $ deferred = array ();
2630 private $ createCacheItem ;
@@ -119,8 +123,12 @@ public function getItem($key)
119123 $ isHit = false ;
120124 $ value = null ;
121125
122- foreach ($ this ->doFetch (array ($ id )) as $ value ) {
123- $ isHit = true ;
126+ try {
127+ foreach ($ this ->doFetch (array ($ id )) as $ value ) {
128+ $ isHit = true ;
129+ }
130+ } catch (\Exception $ e ) {
131+ CacheItem::log ($ this ->logger , 'Failed to fetch key "{key}" ' , array ('key ' => $ key , 'exception ' => $ e ));
124132 }
125133
126134 return $ f ($ key , $ value , $ isHit );
@@ -139,7 +147,12 @@ public function getItems(array $keys = array())
139147 foreach ($ keys as $ key ) {
140148 $ ids [$ key ] = $ this ->getId ($ key );
141149 }
142- $ items = $ this ->doFetch ($ ids );
150+ try {
151+ $ items = $ this ->doFetch ($ ids );
152+ } catch (\Exception $ e ) {
153+ CacheItem::log ($ this ->logger , 'Failed to fetch requested items ' , array ('keys ' => $ keys , 'exception ' => $ e ));
154+ $ items = array ();
155+ }
143156 $ ids = array_flip ($ ids );
144157
145158 return $ this ->generateItems ($ items , $ ids );
@@ -154,15 +167,28 @@ public function hasItem($key)
154167
155168 if (isset ($ this ->deferred [$ key ])) {
156169 $ item = (array ) $ this ->deferred [$ key ];
157- $ ok = $ this ->doSave (array ($ item [CacheItem::CAST_PREFIX .'key ' ] => $ item [CacheItem::CAST_PREFIX .'value ' ]), $ item [CacheItem::CAST_PREFIX .'lifetime ' ]);
158- unset($ this ->deferred [$ key ]);
159-
160- if (true === $ ok || array () === $ ok ) {
161- return true ;
170+ try {
171+ $ e = null ;
172+ $ value = $ item [CacheItem::CAST_PREFIX .'value ' ];
173+ $ ok = $ this ->doSave (array ($ key => $ value ), $ item [CacheItem::CAST_PREFIX .'lifetime ' ]);
174+ unset($ this ->deferred [$ key ]);
175+
176+ if (true === $ ok || array () === $ ok ) {
177+ return true ;
178+ }
179+ } catch (\Exception $ e ) {
162180 }
181+ $ type = is_object ($ value ) ? get_class ($ value ) : gettype ($ value );
182+ CacheItem::log ($ this ->logger , 'Failed to save key "{key}" ({type}) ' , array ('key ' => $ key , 'type ' => $ type , 'exception ' => $ e ));
163183 }
164184
165- return $ this ->doHave ($ id );
185+ try {
186+ return $ this ->doHave ($ id );
187+ } catch (\Exception $ e ) {
188+ CacheItem::log ($ this ->logger , 'Failed to check if key "{key}" is cached ' , array ('key ' => $ key , 'exception ' => $ e ));
189+
190+ return false ;
191+ }
166192 }
167193
168194 /**
@@ -172,7 +198,13 @@ public function clear()
172198 {
173199 $ this ->deferred = array ();
174200
175- return $ this ->doClear ($ this ->namespace );
201+ try {
202+ return $ this ->doClear ($ this ->namespace );
203+ } catch (\Exception $ e ) {
204+ CacheItem::log ($ this ->logger , 'Failed to clear the cache ' , array ('exception ' => $ e ));
205+
206+ return false ;
207+ }
176208 }
177209
178210 /**
@@ -195,7 +227,29 @@ public function deleteItems(array $keys)
195227 unset($ this ->deferred [$ key ]);
196228 }
197229
198- return $ this ->doDelete ($ ids );
230+ try {
231+ if ($ this ->doDelete ($ ids )) {
232+ return true ;
233+ }
234+ } catch (\Exception $ e ) {
235+ }
236+
237+ $ ok = true ;
238+
239+ // When bulk-save failed, retry each item individually
240+ foreach ($ ids as $ key => $ id ) {
241+ try {
242+ $ e = null ;
243+ if ($ this ->doDelete (array ($ id ))) {
244+ continue ;
245+ }
246+ } catch (\Exception $ e ) {
247+ }
248+ CacheItem::log ($ this ->logger , 'Failed to delete key "{key}" ' , array ('key ' => $ key , 'exception ' => $ e ));
249+ $ ok = false ;
250+ }
251+
252+ return $ ok ;
199253 }
200254
201255 /**
@@ -225,9 +279,9 @@ public function saveDeferred(CacheItemInterface $item)
225279 try {
226280 $ item = clone $ item ;
227281 } catch (\Exception $ e ) {
228- @ trigger_error ( $ e -> __toString () );
229-
230- return false ;
282+ $ value = $ item -> get ( );
283+ $ type = is_object ( $ value ) ? get_class ( $ value ) : gettype ( $ value );
284+ CacheItem:: log ( $ this -> logger , ' Failed to clone key "{key}" ({type}) ' , array ( ' key ' => $ key , ' type ' => $ type , ' exception ' => $ e )) ;
231285 }
232286 $ this ->deferred [$ item ->getKey ()] = $ item ;
233287
@@ -243,8 +297,12 @@ public function commit()
243297 $ ko = array ();
244298
245299 foreach ($ f ($ this ->deferred , $ this ->namespace ) as $ lifetime => $ values ) {
246- if (true === $ ok = $ this ->doSave ($ values , $ lifetime )) {
247- continue ;
300+ try {
301+ if (true === $ ok = $ this ->doSave ($ values , $ lifetime )) {
302+ continue ;
303+ }
304+ } catch (\Exception $ e ) {
305+ $ ok = false ;
248306 }
249307 if (false === $ ok ) {
250308 $ ok = array_keys ($ values );
@@ -260,11 +318,15 @@ public function commit()
260318 // When bulk-save failed, retry each item individually
261319 foreach ($ ko as $ lifetime => $ values ) {
262320 foreach ($ values as $ v ) {
263- if (!in_array ($ this ->doSave ($ v , $ lifetime ), array (true , array ()), true )) {
321+ try {
322+ $ e = $ this ->doSave ($ v , $ lifetime );
323+ } catch (\Exception $ e ) {
324+ }
325+ if (true !== $ e && array () !== $ e ) {
264326 $ ok = false ;
265-
266327 foreach ($ v as $ key => $ value ) {
267- @trigger_error (sprintf ('Failed to cache key "%s" of type "%s" ' , is_object ($ value ) ? get_class ($ value ) : gettype ($ value )));
328+ $ type = is_object ($ value ) ? get_class ($ value ) : gettype ($ value );
329+ CacheItem::log ($ this ->logger , 'Failed to save key "{key}" ({type}) ' , array ('key ' => $ key , 'type ' => $ type , 'exception ' => $ e instanceof \Exception ? $ e : null ));
268330 }
269331 }
270332 }
0 commit comments