@@ -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,41 @@ 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+ static $ prefix = "\0Symfony\Component\Cache\CacheItem \0" ;
166+ $ item = (array ) $ this ->deferred [$ key ];
167+ $ ok = $ this ->doSave (array ($ item [$ prefix .'key ' ] => $ item [$ prefix .'value ' ]), $ item [$ prefix .'lifetime ' ]);
168+ unset($ this ->deferred [$ key ]);
169+
170+ if (true === $ ok || array () === $ ok ) {
171+ return true ;
172+ }
170173 }
171174
172- return $ this ->doHave ($ this -> getId ( $ key ) );
175+ return $ this ->doHave ($ id );
173176 }
174177
175178 /**
@@ -179,7 +182,7 @@ public function clear()
179182 {
180183 $ this ->deferred = array ();
181184
182- return $ this ->doClear ();
185+ return $ this ->doClear ($ this -> namespace );
183186 }
184187
185188 /**
@@ -289,4 +292,22 @@ private function getId($key)
289292
290293 return $ this ->namespace .$ key ;
291294 }
295+
296+ private function generateItems ($ deferred , $ ids )
297+ {
298+ foreach ($ deferred as $ key ) {
299+ yield $ key => clone $ this ->deferred [$ key ];
300+ }
301+
302+ foreach ($ this ->doFetch (array_keys ($ ids )) as $ id => $ value ) {
303+ if (isset ($ ids [$ id ])) {
304+ yield $ ids [$ id ] => $ f ($ key , $ value , true );
305+ unset($ ids [$ id ]);
306+ }
307+ }
308+
309+ foreach ($ ids as $ id => $ key ) {
310+ yield $ key => $ f ($ key , null , false );
311+ }
312+ }
292313}
0 commit comments