@@ -64,7 +64,7 @@ function ($deferred, $namespace) {
6464 *
6565 * @param array $ids The cache identifiers to fetch.
6666 *
67- * @return array The corresponding values found in the cache.
67+ * @return array|\Traversable The corresponding values found in the cache.
6868 */
6969 abstract protected function doFetch (array $ ids );
7070
@@ -80,9 +80,11 @@ abstract protected function doHave($id);
8080 /**
8181 * Deletes all items in the pool.
8282 *
83+ * @param string The prefix used for all identifiers managed by this pool.
84+ *
8385 * @return bool True if the pool was successfully cleared, false otherwise.
8486 */
85- abstract protected function doClear ();
87+ abstract protected function doClear ($ namespace );
8688
8789 /**
8890 * Removes multiple items from the pool.
@@ -108,14 +110,10 @@ abstract protected function doSave(array $values, $lifetime);
108110 */
109111 public function getItem ($ key )
110112 {
111- $ id = $ this ->getId ($ key );
112-
113113 if ($ this ->deferred ) {
114114 $ this ->commit ();
115115 }
116- if (isset ($ this ->deferred [$ key ])) {
117- return $ this ->deferred [$ key ];
118- }
116+ $ id = $ this ->getId ($ key );
119117
120118 $ f = $ this ->createCacheItem ;
121119 $ isHit = false ;
@@ -136,40 +134,35 @@ public function getItems(array $keys = array())
136134 if ($ this ->deferred ) {
137135 $ this ->commit ();
138136 }
139- $ f = $ this ->createCacheItem ;
140137 $ ids = array ();
141- $ items = array ();
142138
143139 foreach ($ keys as $ key ) {
144- $ id = $ this ->getId ($ key );
145-
146- if (isset ($ this ->deferred [$ key ])) {
147- $ items [$ key ] = $ this ->deferred [$ key ];
148- } else {
149- $ ids [$ key ] = $ id ;
150- }
140+ $ ids [$ key ] = $ this ->getId ($ key );
151141 }
142+ $ items = $ this ->doFetch ($ ids );
143+ $ ids = array_flip ($ ids );
152144
153- $ values = $ this ->doFetch ($ ids );
154-
155- foreach ($ ids as $ key => $ id ) {
156- $ isHit = isset ($ values [$ id ]);
157- $ items [$ key ] = $ f ($ key , $ isHit ? $ values [$ id ] : null , $ isHit );
158- }
159-
160- return $ items ;
145+ return $ this ->generateItems ($ items , $ ids );
161146 }
162147
163148 /**
164149 * {@inheritdoc}
165150 */
166151 public function hasItem ($ key )
167152 {
168- if ($ this ->deferred ) {
169- $ this ->commit ();
153+ $ id = $ this ->getId ($ key );
154+
155+ if (isset ($ this ->deferred [$ key ])) {
156+ $ 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 ;
162+ }
170163 }
171164
172- return $ this ->doHave ($ this -> getId ( $ key ) );
165+ return $ this ->doHave ($ id );
173166 }
174167
175168 /**
@@ -179,7 +172,7 @@ public function clear()
179172 {
180173 $ this ->deferred = array ();
181174
182- return $ this ->doClear ();
175+ return $ this ->doClear ($ this -> namespace );
183176 }
184177
185178 /**
@@ -198,7 +191,7 @@ public function deleteItems(array $keys)
198191 $ ids = array ();
199192
200193 foreach ($ keys as $ key ) {
201- $ ids [] = $ this ->getId ($ key );
194+ $ ids [$ key ] = $ this ->getId ($ key );
202195 unset($ this ->deferred [$ key ]);
203196 }
204197
@@ -213,11 +206,12 @@ public function save(CacheItemInterface $item)
213206 if (!$ item instanceof CacheItem) {
214207 return false ;
215208 }
216- $ key = $ item ->getKey ();
217- $ this ->deferred [$ key ] = $ item ;
218- $ this ->commit ();
209+ if ($ this ->deferred ) {
210+ $ this ->commit ();
211+ }
212+ $ this ->deferred [$ item ->getKey ()] = $ item ;
219213
220- return ! isset ( $ this ->deferred [ $ key ] );
214+ return $ this ->commit ( );
221215 }
222216
223217 /**
@@ -230,10 +224,7 @@ public function saveDeferred(CacheItemInterface $item)
230224 }
231225 try {
232226 $ item = clone $ item ;
233- } catch (\Error $ e ) {
234227 } catch (\Exception $ e ) {
235- }
236- if (isset ($ e )) {
237228 @trigger_error ($ e ->__toString ());
238229
239230 return false ;
@@ -250,7 +241,6 @@ public function commit()
250241 {
251242 $ f = $ this ->mergeByLifetime ;
252243 $ ko = array ();
253- $ namespaceLen = strlen ($ this ->namespace );
254244
255245 foreach ($ f ($ this ->deferred , $ this ->namespace ) as $ lifetime => $ values ) {
256246 if (true === $ ok = $ this ->doSave ($ values , $ lifetime )) {
@@ -259,13 +249,28 @@ public function commit()
259249 if (false === $ ok ) {
260250 $ ok = array_keys ($ values );
261251 }
262- foreach ($ ok as $ failedId ) {
263- $ key = substr ($ failedId , $ namespaceLen );
264- $ ko [$ key ] = $ this ->deferred [$ key ];
252+ foreach ($ ok as $ id ) {
253+ $ ko [$ lifetime ][] = array ($ id => $ values [$ id ]);
265254 }
266255 }
267256
268- return !$ this ->deferred = $ ko ;
257+ $ this ->deferred = array ();
258+ $ ok = true ;
259+
260+ // When bulk-save failed, retry each item individually
261+ foreach ($ ko as $ lifetime => $ values ) {
262+ foreach ($ values as $ v ) {
263+ if (!in_array ($ this ->doSave ($ v , $ lifetime ), array (true , array ()), true )) {
264+ $ ok = false ;
265+
266+ 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 )));
268+ }
269+ }
270+ }
271+ }
272+
273+ return $ ok ;
269274 }
270275
271276 public function __destruct ()
@@ -289,4 +294,18 @@ private function getId($key)
289294
290295 return $ this ->namespace .$ key ;
291296 }
297+
298+ private function generateItems ($ items , &$ keys )
299+ {
300+ $ f = $ this ->createCacheItem ;
301+
302+ foreach ($ items as $ id => $ value ) {
303+ yield $ keys [$ id ] => $ f ($ keys [$ id ], $ value , true );
304+ unset($ keys [$ id ]);
305+ }
306+
307+ foreach ($ keys as $ key ) {
308+ yield $ key => $ f ($ key , null , false );
309+ }
310+ }
292311}
0 commit comments