From 11654591e70221f612a6ca312ecf6387d72f04c7 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 28 Apr 2016 09:51:36 +0200 Subject: [PATCH] [Cache] Dont use Redis connection when not required --- .../Cache/Adapter/AbstractAdapter.php | 2 +- .../Component/Cache/Adapter/RedisAdapter.php | 26 ++++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php index 0a68f82b6c78f..bb74b205fb7d5 100644 --- a/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/AbstractAdapter.php @@ -227,7 +227,7 @@ public function deleteItems(array $keys) $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; diff --git a/src/Symfony/Component/Cache/Adapter/RedisAdapter.php b/src/Symfony/Component/Cache/Adapter/RedisAdapter.php index 43aa557fa52f3..a4c18c6255c86 100644 --- a/src/Symfony/Component/Cache/Adapter/RedisAdapter.php +++ b/src/Symfony/Component/Cache/Adapter/RedisAdapter.php @@ -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 @@ public function __construct(\Redis $redisConnection, $namespace = '', $defaultLi */ protected function doFetch(array $ids) { - $values = $this->redis->mget($ids); - $index = 0; - $result = []; - - foreach ($ids as $id) { - if (false !== $value = $values[$index++]) { - $result[$id] = unserialize($value); + $result = array(); + + 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 @@ protected function doClear($namespace) */ protected function doDelete(array $ids) { - $this->redis->del($ids); + if ($ids) { + $this->redis->del($ids); + } return true; } @@ -101,6 +104,9 @@ protected function doSave(array $values, $lifetime) } } + if (!$serialized) { + return $failed; + } if ($lifetime > 0) { $pipe = $this->redis->multi(\Redis::PIPELINE); foreach ($serialized as $id => $value) {