diff --git a/lib/Predis/Cluster/RedisClusterHashStrategy.php b/lib/Predis/Cluster/RedisClusterHashStrategy.php index 1b1fd84e..73fe10bb 100644 --- a/lib/Predis/Cluster/RedisClusterHashStrategy.php +++ b/lib/Predis/Cluster/RedisClusterHashStrategy.php @@ -134,6 +134,10 @@ class RedisClusterHashStrategy implements CommandHashStrategyInterface 'HSET' => $keyIsFirstArgument, 'HSETNX' => $keyIsFirstArgument, 'HVALS' => $keyIsFirstArgument, + + /* scripting */ + 'EVAL' => array($this, 'getKeyFromScriptingCommands'), + 'EVALSHA' => array($this, 'getKeyFromScriptingCommands'), ); } @@ -234,6 +238,23 @@ class RedisClusterHashStrategy implements CommandHashStrategyInterface } } + /** + * Extracts the key from EVAL and EVALSHA commands. + * + * @param CommandInterface $command Command instance. + * @return string + */ + protected function getKeyFromScriptingCommands(CommandInterface $command) + { + $keys = $command instanceof ScriptedCommand + ? $command->getKeys() + : array_slice($args = $command->getArguments(), 2, $args[1]); + + if (count($keys) === 1) { + return $keys[0]; + } + } + /** * {@inheritdoc} */ diff --git a/lib/Predis/Connection/RedisCluster.php b/lib/Predis/Connection/RedisCluster.php index 4abe33d3..c490480c 100644 --- a/lib/Predis/Connection/RedisCluster.php +++ b/lib/Predis/Connection/RedisCluster.php @@ -280,7 +280,7 @@ class RedisCluster implements ClusterConnectionInterface, \IteratorAggregate, \C */ public function getCommandHashStrategy() { - return $this->cmdHasher; + return $this->strategy; } /** diff --git a/tests/Predis/Cluster/RedisClusterHashStrategyTest.php b/tests/Predis/Cluster/RedisClusterHashStrategyTest.php index 6eb26e25..d0827042 100644 --- a/tests/Predis/Cluster/RedisClusterHashStrategyTest.php +++ b/tests/Predis/Cluster/RedisClusterHashStrategyTest.php @@ -159,6 +159,41 @@ class RedisClusterHashStrategyTest extends StandardTestCase } } + /** + * @group disconnected + */ + public function testKeysForScriptCommand() + { + $strategy = $this->getHashStrategy(); + $profile = ServerProfile::getDevelopment(); + $arguments = array('%SCRIPT%', 1, 'key:1', 'value1'); + + foreach ($this->getExpectedCommands('keys-script') as $commandID) { + $command = $profile->createCommand($commandID, $arguments); + $this->assertNotNull($strategy->getHash($command), $commandID); + } + } + + /** + * @group disconnected + */ + public function testKeysForScriptedCommand() + { + $strategy = $this->getHashStrategy(); + $arguments = array('key:1', 'value1'); + + $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount')); + $command->expects($this->once()) + ->method('getScript') + ->will($this->returnValue('return true')); + $command->expects($this->exactly(1)) + ->method('getKeysCount') + ->will($this->returnValue(1)); + $command->setArguments($arguments); + + $this->assertNotNull($strategy->getHash($command), "Scripted Command [{$command->getId()}]"); + } + /** * @group disconnected */ @@ -312,6 +347,10 @@ class RedisClusterHashStrategyTest extends StandardTestCase 'HSET' => 'keys-first', 'HSETNX' => 'keys-first', 'HVALS' => 'keys-first', + + /* scripting */ + 'EVAL' => 'keys-script', + 'EVALSHA' => 'keys-script', ); if (isset($type)) {