Skip to content

[Cache] Add generic TagAwareAdapter wrapper (replaces TagAwareRedisAdapter) #19524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 0 additions & 217 deletions src/Symfony/Component/Cache/Adapter/AbstractTagAwareAdapter.php

This file was deleted.

122 changes: 119 additions & 3 deletions src/Symfony/Component/Cache/Adapter/RedisAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Cache\Adapter;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes on this file are just resetting it to its exact 3.1 state.


use Predis\Connection\Factory;
use Predis\Connection\Aggregate\PredisCluster;
use Predis\Connection\Aggregate\RedisCluster;
use Symfony\Component\Cache\Exception\InvalidArgumentException;

/**
Expand All @@ -20,23 +22,29 @@
*/
class RedisAdapter extends AbstractAdapter
{
use RedisAdapterTrait;

private static $defaultConnectionOptions = array(
'class' => null,
'persistent' => 0,
'timeout' => 0,
'read_timeout' => 0,
'retry_interval' => 0,
);
private $redis;

/**
* @param \Redis|\RedisArray|\RedisCluster|\Predis\Client $redisClient
*/
public function __construct($redisClient, $namespace = '', $defaultLifetime = 0)
{
parent::__construct($namespace, $defaultLifetime);
$this->setRedis($redisClient, $namespace);

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]));
}
if (!$redisClient instanceof \Redis && !$redisClient instanceof \RedisArray && !$redisClient instanceof \RedisCluster && !$redisClient instanceof \Predis\Client) {
throw new InvalidArgumentException(sprintf('%s() expects parameter 1 to be Redis, RedisArray, RedisCluster or Predis\Client, %s given', __METHOD__, is_object($redisClient) ? get_class($redisClient) : gettype($redisClient)));
}
$this->redis = $redisClient;
}

/**
Expand Down Expand Up @@ -149,6 +157,71 @@ protected function doHave($id)
return (bool) $this->redis->exists($id);
}

/**
* {@inheritdoc}
*/
protected function doClear($namespace)
{
// When using a native Redis cluster, clearing the cache cannot work and always returns false.
// Clearing the cache should then be done by any other means (e.g. by restarting the cluster).

$cleared = true;
$hosts = array($this->redis);
$evalArgs = array(array($namespace), 0);

if ($this->redis instanceof \Predis\Client) {
$evalArgs = array(0, $namespace);

$connection = $this->redis->getConnection();
if ($connection instanceof PredisCluster) {
$hosts = array();
foreach ($connection as $c) {
$hosts[] = new \Predis\Client($c);
}
} elseif ($connection instanceof RedisCluster) {
return false;
}
} elseif ($this->redis instanceof \RedisArray) {
$hosts = array();
foreach ($this->redis->_hosts() as $host) {
$hosts[] = $this->redis->_instance($host);
}
} elseif ($this->redis instanceof \RedisCluster) {
return false;
}
foreach ($hosts as $host) {
if (!isset($namespace[0])) {
$cleared = $host->flushDb() && $cleared;
continue;
}

$info = $host->info('Server');
$info = isset($info['Server']) ? $info['Server'] : $info;

if (!version_compare($info['redis_version'], '2.8', '>=')) {
// As documented in Redis documentation (http://redis.io/commands/keys) using KEYS
// can hang your server when it is executed against large databases (millions of items).
// Whenever you hit this scale, you should really consider upgrading to Redis 2.8 or above.
$cleared = $host->eval("local keys=redis.call('KEYS',ARGV[1]..'*') for i=1,#keys,5000 do redis.call('DEL',unpack(keys,i,math.min(i+4999,#keys))) end return 1", $evalArgs[0], $evalArgs[1]) && $cleared;
continue;
}

$cursor = null;
do {
$keys = $host instanceof \Predis\Client ? $host->scan($cursor, 'MATCH', $namespace.'*', 'COUNT', 1000) : $host->scan($cursor, $namespace.'*', 1000);
if (isset($keys[1]) && is_array($keys[1])) {
$cursor = $keys[0];
$keys = $keys[1];
}
if ($keys) {
$host->del($keys);
}
} while ($cursor = (int) $cursor);
}

return $cleared;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -195,4 +268,47 @@ protected function doSave(array $values, $lifetime)

return $failed;
}

private function execute($command, $id, array $args, $redis = null)
{
array_unshift($args, $id);
call_user_func_array(array($redis ?: $this->redis, $command), $args);
}

private function pipeline(\Closure $callback)
{
$redis = $this->redis;

try {
if ($redis instanceof \Predis\Client) {
$redis->pipeline(function ($pipe) use ($callback) {
$this->redis = $pipe;
$callback(array($this, 'execute'));
});
} elseif ($redis instanceof \RedisArray) {
$connections = array();
$callback(function ($command, $id, $args) use (&$connections) {
if (!isset($connections[$h = $this->redis->_target($id)])) {
$connections[$h] = $this->redis->_instance($h);
$connections[$h]->multi(\Redis::PIPELINE);
}
$this->execute($command, $id, $args, $connections[$h]);
});
foreach ($connections as $c) {
$c->exec();
}
} else {
$pipe = $redis->multi(\Redis::PIPELINE);
try {
$callback(array($this, 'execute'));
} finally {
if ($pipe) {
$redis->exec();
}
}
}
} finally {
$this->redis = $redis;
}
}
}
Loading