8000 [Cache] Clean RedisAdapter pipelining implementation · symfony/symfony@062dc2e · GitHub
[go: up one dir, main page]

Skip to content

Commit 062dc2e

Browse files
[Cache] Clean RedisAdapter pipelining implementation
1 parent b85ab60 commit 062dc2e

File tree

1 file changed

+51
-34
lines changed

1 file changed

+51
-34
lines changed

src/Symfony/Component/Cache/Adapter/RedisAdapter.php

Lines changed: 51 additions & 34 deletions
< 8000 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -229,53 +229,70 @@ protected function doSave(array $values, $lifetime)
229229
$failed[] = $id;
230230
}
231231
}
232-
233232
if (!$serialized) {
234233
return $failed;
235234
}
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;
260239
}
261240

241+
$this->pipeline(function ($pipe) use (&$serialized, $lifetime) {
242+
foreach ($serialized as $id => $value) {
243+
$pipe('setEx', $id, array($lifetime, $value));
244+
}
245+
});
246+
262247
return $failed;
263248
}
264249

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+
265256
private function pipeline(\Closure $callback)
266257
{
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+
271261
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+
}
274292
} catch (\Exception $e) {
275293
}
276-
if ($pipe) {
277-
$this->redis->exec();
278-
}
294+
295+
$this->redis = $redis;
279296
if (null !== $e) {
280297
throw $e;
281298
}

0 commit comments

Comments
 (0)
0