@@ -229,53 +229,70 @@ protected function doSave(array $values, $lifetime)
229
229
$ failed [] = $ id ;
230
230
}
231
231
}
232
-
233
232
if (!$ serialized ) {
234
233
return $ failed ;
235
234
}
236
- if ($ lifetime > 0 ) {
237
- if ($ this ->redis instanceof \RedisArray) {
238
- $ redis = array ();
239
- foreach ($ serialized as $ id => $ value ) {
240
- if (!isset ($ redis [$ h = $ this ->redis ->_target ($ id )])) {
241
- $ redis [$ h ] = $ this ->redis ->_instance ($ h );
242
- $ redis [$ h ]->multi (\Redis::PIPELINE );
243
- }
244
- $ redis [$ h ]->setEx ($ id , $ lifetime , $ value );
245
- }
246
- foreach ($ redis as $ h ) {
247
- if (!$ h ->exec ()) {
248
- $ failed = false ;
249
- }
250
- }
251
- } else {
252
- $ this ->pipeline (function ($ pipe ) use ($ serialized , $ lifetime ) {
253
- foreach ($ serialized as $ id => $ value ) {
254
- $ pipe ->setEx ($ id , $ lifetime , $ value );
255
- }
256
- });
257
- }
258
- } elseif (!$ this ->redis ->mSet ($ serialized )) {
259
- return false ;
235
+ if (0 >= $ lifetime ) {
236
+ $ this ->redis ->mSet ($ serialized );
237
+
238
+ return $ failed ;
260
239
}
261
240
241
+ $ this ->pipeline (function ($ pipe ) use (&$ serialized , $ lifetime ) {
242
+ foreach ($ serialized as $ id => $ value ) {
243
+ $ pipe ('setEx ' , $ id , array ($ lifetime , $ value ));
244
+ }
245
+ });
246
+
262
247
return $ failed ;
263
248
}
264
249
250
+ private function execute ($ command , $ id , array $ args , $ redis = null )
251
+ {
252
+ array_unshift ($ args , $ id );
253
+ call_user_func_array (array ($ redis ?: $ this ->redis , $ command ), $ args );
254
+ }
255
+
265
256
private function pipeline (\Closure $ callback )
266
257
{
267
- if ($ this ->redis instanceof \Predis \Client) {
268
- return $ this ->redis ->pipeline ($ callback );
269
- }
270
- $ pipe = $ this ->redis instanceof \Redis && $ this ->redis ->multi (\Redis::PIPELINE );
258
+ $ e = null ;
259
+ $ redis = $ this ->redis ;
260
+
271
261
try {
272
- $ e = null ;
273
- $ callback ($ this ->redis );
262
+ if ($ redis instanceof \Predis \Client) {
263
+ $ redis ->pipeline (function ($ pipe ) use ($ callback ) {
264
+ $ this ->redis = $ pipe ;
265
+ $ callback (array ($ this , 'execute ' ));
266
+ });
267
+ } elseif ($ redis instanceof \RedisArray) {
268
+ $ connections = array ();
269
+ $ callback (function ($ command , $ id , $ args ) use (&$ connections ) {
270
+ if (!isset ($ connections [$ h = $ this ->redis ->_target ($ id )])) {
271
+ $ connections [$ h ] = $ this ->redis ->_instance ($ h );
272
+ $ connections [$ h ]->multi (\Redis::PIPELINE );
273
+ }
274
+ $ this ->execute ($ command , $ id , $ args , $ connections [$ h ]);
275
+ });
276
+ foreach ($ connections as $ c ) {
277
+ $ c ->exec ();
278
+ }
279
+ } else {
280
+ $ pipe = $ redis ->multi (\Redis::PIPELINE );
281
+ try {
282
+ $ callback (array ($ this , 'execute ' ));
283
+ } catch (\Exception $ e ) {
284
+ }
285
+ if ($ pipe ) {
286
+ $ redis ->exec ();
287
+ }
288
+ if (null !== $ e ) {
289
+ throw $ e ;
290
+ }
291
+ }
274
292
} catch (\Exception $ e ) {
275
293
}
276
- if ($ pipe ) {
277
- $ this ->redis ->exec ();
278
- }
294
+
295
+ $ this ->redis = $ redis ;
279
296
if (null !== $ e ) {
280
297
throw $ e ;
281
298
}
0 commit comments