Skip to content

Commit

Permalink
bug #18659 [Cache] Dont use Redis connection when not required (nicol…
Browse files Browse the repository at this point in the history
…as-grekas)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[Cache] Dont use Redis connection when not required

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

1165459 [Cache] Dont use Redis connection when not required
  • Loading branch information
fabpot committed Apr 28, 2016
2 parents 2d4df16 + 1165459 commit ae1baac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Expand Up @@ -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;
Expand Down
26 changes: 16 additions & 10 deletions src/Symfony/Component/Cache/Adapter/RedisAdapter.php
Expand Up @@ -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);
}
Expand All @@ -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);
}
}
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down

0 comments on commit ae1baac

Please sign in to comment.