@@ -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.
@@ -114,7 +116,7 @@ public function getItem($key)
114116 $ this ->commit ();
115117 }
116118 if (isset ($ this ->deferred [$ key ])) {
117- return $ this ->deferred [$ key ];
119+ return clone $ this ->deferred [$ key ];
118120 }
119121
120122 $ f = $ this ->createCacheItem ;
@@ -136,40 +138,40 @@ public function getItems(array $keys = array())
136138 if ($ this ->deferred ) {
137139 $ this ->commit ();
138140 }
139- $ f = $ this ->createCacheItem ;
140141 $ ids = array ();
141- $ items = array ();
142+ $ deferred = array ();
142143
143144 foreach ($ keys as $ key ) {
144145 $ id = $ this ->getId ($ key );
145146
146147 if (isset ($ this ->deferred [$ key ])) {
147- $ items [ $ key ] = $ this -> deferred [ $ key] ;
148+ $ deferred [ ] = $ key ;
148149 } else {
149- $ ids [$ key ] = $ id ;
150+ $ ids [$ id ] = $ key ;
150151 }
151152 }
152153
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 ;
154+ return $ this ->generateItems ($ deferred , $ ids );
161155 }
162156
163157 /**
164158 * {@inheritdoc}
165159 */
166160 public function hasItem ($ key )
167161 {
168- if ($ this ->deferred ) {
169- $ this ->commit ();
162+ $ id = $ this ->getId ($ key );
163+
164+ if (isset ($ this ->deferred [$ key ])) {
165+ $ item = (array ) $ this ->deferred [$ key ];
166+ $ ok = $ this ->doSave (array ($ item [CacheItem::CAST_PREFIX .'key ' ] => $ item [CacheItem::CAST_PREFIX .'value ' ]), $ item [CacheItem::CAST_PREFIX .'lifetime ' ]);
167+ unset($ this ->deferred [$ key ]);
168+
169+ if (true === $ ok || array () === $ ok ) {
170+ return true ;
171+ }
170172 }
171173
172- return $ this ->doHave ($ this -> getId ( $ key ) );
174+ return $ this ->doHave ($ id );
173175 }
174176
175177 /**
@@ -179,7 +181,7 @@ public function clear()
179181 {
180182 $ this ->deferred = array ();
181183
182- return $ this ->doClear ();
184+ return $ this ->doClear ($ this -> namespace );
183185 }
184186
185187 /**
@@ -289,4 +291,24 @@ private function getId($key)
289291
290292 return $ this ->namespace .$ key ;
291293 }
294+
295+ private function generateItems ($ deferred , $ ids )
296+ {
297+ $ f = $ this ->createCacheItem ;
298+
299+ foreach ($ deferred as $ key ) {
300+ yield $ key => clone $ this ->deferred [$ key ];
301+ }
302+
303+ foreach ($ this ->doFetch (array_keys ($ ids )) as $ id => $ value ) {
304+ if (isset ($ ids [$ id ])) {
305+ yield $ ids [$ id ] => $ f ($ ids [$ id ], $ value , true );
306+ unset($ ids [$ id ]);
307+ }
308+ }
309+
310+ foreach ($ ids as $ id => $ key ) {
311+ yield $ key => $ f ($ key , null , false );
312+ }
313+ }
292314}
0 commit comments