[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; $ok = true;
// When bulk-save failed, retry each item individually // When bulk-delete failed, retry each item individually
foreach ($ids as $key => $id) { foreach ($ids as $key => $id) {
try { try {
$e = null; $e = null;

View File

@ -22,11 +22,10 @@ class RedisAdapter extends AbstractAdapter
public function __construct(\Redis $redisConnection, $namespace = '', $defaultLifetime = 0) public function __construct(\Redis $redisConnection, $namespace = '', $defaultLifetime = 0)
{ {
$this->redis = $redisConnection;
if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) { 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])); 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); parent::__construct($namespace, $defaultLifetime);
} }
@ -36,13 +35,15 @@ class RedisAdapter extends AbstractAdapter
*/ */
protected function doFetch(array $ids) protected function doFetch(array $ids)
{ {
$values = $this->redis->mget($ids); $result = array();
$index = 0;
$result = [];
foreach ($ids as $id) { if ($ids) {
if (false !== $value = $values[$index++]) { $values = $this->redis->mget($ids);
$result[$id] = unserialize($value); $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) protected function doDelete(array $ids)
{ {
$this->redis->del($ids); if ($ids) {
$this->redis->del($ids);
}
return true; return true;
} }
@ -101,6 +104,9 @@ class RedisAdapter extends AbstractAdapter
} }
} }
if (!$serialized) {
return $failed;
}
if ($lifetime > 0) { if ($lifetime > 0) {
$pipe = $this->redis->multi(\Redis::PIPELINE); $pipe = $this->redis->multi(\Redis::PIPELINE);
foreach ($serialized as $id => $value) { foreach ($serialized as $id => $value) {