13
13
14
14
use Psr \Cache \CacheItemInterface ;
15
15
use Psr \Cache \CacheItemPoolInterface ;
16
+ use Psr \Log \LoggerAwareInterface ;
17
+ use Psr \Log \LoggerAwareTrait ;
16
18
use Symfony \Component \Cache \CacheItem ;
17
19
use Symfony \Component \Cache \Exception \InvalidArgumentException ;
18
20
19
21
/**
20
22
* @author Nicolas Grekas <[email protected] >
21
23
*/
22
- abstract class AbstractAdapter implements CacheItemPoolInterface
24
+ abstract class AbstractAdapter implements CacheItemPoolInterface, LoggerAwareInterface
23
25
{
26
+ use LoggerAwareTrait;
27
+
24
28
private $ namespace ;
25
29
private $ deferred = array ();
26
30
private $ createCacheItem ;
@@ -119,8 +123,12 @@ public function getItem($key)
119
123
$ isHit = false ;
120
124
$ value = null ;
121
125
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 ));
124
132
}
125
133
126
134
return $ f ($ key , $ value , $ isHit );
@@ -139,7 +147,12 @@ public function getItems(array $keys = array())
139
147
foreach ($ keys as $ key ) {
140
148
$ ids [$ key ] = $ this ->getId ($ key );
141
149
}
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
+ }
143
156
$ ids = array_flip ($ ids );
144
157
145
158
return $ this ->generateItems ($ items , $ ids );
@@ -154,15 +167,28 @@ public function hasItem($key)
154
167
155
168
if (isset ($ this ->deferred [$ key ])) {
156
169
$ 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 ) {
162
180
}
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 ));
163
183
}
164
184
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
+ }
166
192
}
167
193
168
194
/**
@@ -172,7 +198,13 @@ public function clear()
172
198
{
173
199
$ this ->deferred = array ();
174
200
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
+ }
176
208
}
177
209
178
210
/**
@@ -195,7 +227,29 @@ public function deleteItems(array $keys)
195
227
unset($ this ->deferred [$ key ]);
196
228
}
197
229
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 ;
199
253
}
200
254
201
255
/**
@@ -225,9 +279,9 @@ public function saveDeferred(CacheItemInterface $item)
225
279
try {
226
280
$ item = clone $ item ;
227
281
} 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 )) ;
231
285
}
232
286
$ this ->deferred [$ item ->getKey ()] = $ item ;
233
287
@@ -243,8 +297,12 @@ public function commit()
243
297
$ ko = array ();
244
298
245
299
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 ;
248
306
}
249
307
if (false === $ ok ) {
250
308
$ ok = array_keys ($ values );
@@ -260,11 +318,15 @@ public function commit()
260
318
// When bulk-save failed, retry each item individually
261
319
foreach ($ ko as $ lifetime => $ values ) {
262
320
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 ) {
264
326
$ ok = false ;
265
-
266
327
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 ));
268
330
}
269
331
}
270
332
}
0 commit comments