Make it possible to specify a custom strategy for redis-cluster.

This commit is contained in:
Daniele Alessandri
2014-07-21 10:56:32 +02:00
parent 7b41029fd7
commit 4188dc51bf
2 changed files with 22 additions and 6 deletions
+9 -5
View File
@@ -16,6 +16,7 @@ use Countable;
use IteratorAggregate;
use OutOfBoundsException;
use Predis\NotSupportedException;
use Predis\Cluster\StrategyInterface;
use Predis\Cluster\RedisStrategy as RedisClusterStrategy;
use Predis\Command\CommandInterface;
use Predis\Command\RawCommand;
@@ -57,12 +58,15 @@ class RedisCluster implements ClusterInterface, IteratorAggregate, Countable
private $connections;
/**
* @param FactoryInterface $connections Connection factory object.
* @param FactoryInterface $connections Connection factory instance.
* @param StrategyInterface $strategy Cluster strategy instance.
*/
public function __construct(FactoryInterface $connections = null)
{
$this->strategy = new RedisClusterStrategy();
public function __construct(
FactoryInterface $connections = null,
StrategyInterface $strategy = null
) {
$this->connections = $connections ?: new Factory();
$this->strategy = $strategy ?: new RedisClusterStrategy();
}
/**
@@ -486,7 +490,7 @@ class RedisCluster implements ClusterInterface, IteratorAggregate, Countable
* Returns the underlying command hash strategy used to hash commands by
* using keys found in their arguments.
*
* @return \Predis\Cluster\StrategyInterface
* @return StrategyInterface
*/
public function getClusterStrategy()
{
@@ -25,12 +25,24 @@ class RedisClusterTest extends PredisTestCase
/**
* @group disconnected
*/
public function testExposesCommandHashStrategy()
public function testUsesRedisClusterStrategyByDefault()
{
$cluster = new RedisCluster();
$this->assertInstanceOf('Predis\Cluster\RedisStrategy', $cluster->getClusterStrategy());
}
/**
* @group disconnected
*/
public function testAcceptsCustomClusterStrategy()
{
$strategy = $this->getMock('Predis\Cluster\StrategyInterface');
$cluster = new RedisCluster(null, $strategy);
$this->assertSame($strategy, $cluster->getClusterStrategy());
}
/**
* @group disconnected
*/