@@ -64,7 +64,7 @@ function ($deferred, $namespace) {
64
64
*
65
65
* @param array $ids The cache identifiers to fetch.
66
66
*
67
- * @return array The corresponding values found in the cache.
67
+ * @return array|\Traversable The corresponding values found in the cache.
68
68
*/
69
69
abstract protected function doFetch (array $ ids );
70
70
@@ -80,9 +80,11 @@ abstract protected function doHave($id);
80
80
/**
81
81
* Deletes all items in the pool.
82
82
*
83
+ * @param string The prefix used for all identifiers managed by this pool.
84
+ *
83
85
* @return bool True if the pool was successfully cleared, false otherwise.
84
86
*/
85
- abstract protected function doClear ();
87
+ abstract protected function doClear ($ namespace );
86
88
87
89
/**
88
90
* Removes multiple items from the pool.
@@ -114,7 +116,7 @@ public function getItem($key)
114
116
$ this ->commit ();
115
117
}
116
118
if (isset ($ this ->deferred [$ key ])) {
117
- return $ this ->deferred [$ key ];
119
+ return clone $ this ->deferred [$ key ];
118
120
}
119
121
120
122
$ f = $ this ->createCacheItem ;
@@ -136,40 +138,41 @@ public function getItems(array $keys = array())
136
138
if ($ this ->deferred ) {
137
139
$ this ->commit ();
138
140
}
139
- $ f = $ this ->createCacheItem ;
140
141
$ ids = array ();
141
- $ items = array ();
142
+ $ deferred = array ();
142
143
143
144
foreach ($ keys as $ key ) {
144
145
$ id = $ this ->getId ($ key );
145
146
146
147
if (isset ($ this ->deferred [$ key ])) {
147
- $ items [ $ key ] = $ this -> deferred [ $ key] ;
148
+ $ deferred [ ] = $ key ;
148
149
} else {
149
- $ ids [$ key ] = $ id ;
150
+ $ ids [$ id ] = $ key ;
150
151
}
151
152
}
152
153
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 );
161
155
}
162
156
163
157
/**
164
158
* {@inheritdoc}
165
159
*/
166
160
public function hasItem ($ key )
167
161
{
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
+ }
170
173
}
171
174
172
- return $ this ->doHave ($ this -> getId ( $ key ) );
175
+ return $ this ->doHave ($ id );
173
176
}
174
177
175
178
/**
@@ -179,7 +182,7 @@ public function clear()
179
182
{
180
183
$ this ->deferred = array ();
181
184
182
- return $ this ->doClear ();
185
+ return $ this ->doClear ($ this -> namespace );
183
186
}
184
187
185
188
/**
@@ -289,4 +292,22 @@ private function getId($key)
289
292
290
293
return $ this ->namespace .$ key ;
291
294
}
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 ($ ids [$ id ], $ value , true );
305
+ unset($ ids [$ id ]);
306
+ }
307
+ }
308
+
309
+ foreach ($ ids as $ id => $ key ) {
310
+ yield $ key => $ f ($ key , null , false );
311
+ }
312
+ }
292
313
}
0 commit comments