[Cache] Dont use Redis connection when not required

This commit is contained in:
Nicolas Grekas 2016-04-28 09:51:36 +02:00
parent f2228d5cd4
commit 11654591e7
2 changed files with 16 additions and 10 deletions

View File

@ -227,7 +227,7 @@ abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface
$ok = true;
// When bulk-save failed, retry each item individually
// When bulk-delete failed, retry each item individually
foreach ($ids as $key => $id) {
try {
$e = null;

View File

@ -22,11 +22,10 @@ class RedisAdapter extends AbstractAdapter
public function __construct(\Redis $redisConnection, $namespace = '', $defaultLifetime = 0)
{
$this->redis = $redisConnection;
if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
throw new InvalidArgumentException(sprintf('RedisAdapter namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
}
$this->redis = $redisConnection;
parent::__construct($namespace, $defaultLifetime);
}
@ -36,13 +35,15 @@ class RedisAdapter extends AbstractAdapter
*/
protected function doFetch(array $ids)
{
$values = $this->redis->mget($ids);
$index = 0;
$result = [];
$result = array();
foreach ($ids as $id) {
if (false !== $value = $values[$index++]) {
$result[$id] = unserialize($value);
if ($ids) {
$values = $this->redis->mget($ids);
$index = 0;
foreach ($ids as $id) {
if (false !== $value = $values[$index++]) {
$result[$id] = unserialize($value);
}
}
}
@ -80,7 +81,9 @@ class RedisAdapter extends AbstractAdapter
*/
protected function doDelete(array $ids)
{
$this->redis->del($ids);
if ($ids) {
$this->redis->del($ids);
}
return true;
}
@ -101,6 +104,9 @@ class RedisAdapter extends AbstractAdapter
}
}
if (!$serialized) {
return $failed;
}
if ($lifetime > 0) {
$pipe = $this->redis->multi(\Redis::PIPELINE);
foreach ($serialized as $id => $value) {