From 09895f27bf4564e0a2ec990278d8b2e4b8d8c61f Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Sat, 30 Nov 2013 12:43:21 +0100 Subject: [PATCH] Rename command class for scripting to Predis\Command\ScriptCommand. We also changed our wording to indentify this kind of abstraction so instead of using "scripted commands" (kind of broken English) we now use "scriptable commands". --- CHANGELOG.md | 5 ++++- README.md | 8 ++++---- examples/MasterSlaveReplicationComplex.php | 20 +++++++++---------- examples/ServerSideScripting.php | 13 ++++++------ lib/Predis/Client.php | 4 ++-- lib/Predis/Cluster/PredisStrategy.php | 4 ++-- lib/Predis/Cluster/RedisStrategy.php | 4 ++-- ...{ScriptedCommand.php => ScriptCommand.php} | 4 ++-- tests/Predis/ClientTest.php | 4 ++-- tests/Predis/Cluster/PredisStrategyTest.php | 8 ++++---- tests/Predis/Cluster/RedisStrategyTest.php | 8 ++++---- tests/Predis/Command/ScriptedCommandTest.php | 16 +++++++-------- .../Replication/ReplicationStrategyTest.php | 8 ++++---- 13 files changed, 55 insertions(+), 51 deletions(-) rename lib/Predis/Command/{ScriptedCommand.php => ScriptCommand.php} (94%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d6b01f8..d5a7a61e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,7 +36,10 @@ v0.9.0 (201x-xx-xx) - `fire-and-forget`: returns a pipeline that does not read back responses (class: `Predis\Pipeline\FireAndForget`). -- The base abstract command class has been renamed to Predis\Command\Command. +- The two base abstract command classes have been renamed in the following way: + + - `Predis\Command\AbstractCommand` is now `Predis\Command\Command` + - `Predis\Command\ScriptedCommand` is now `Predis\Command\ScriptCommand` - Most classes and interfaces in the `Predis\Protocol` namespace have been moved or renamed while rationalizing the whole API of external protocol processors. diff --git a/README.md b/README.md index e11054c9..ca564e42 100644 --- a/README.md +++ b/README.md @@ -177,14 +177,14 @@ $redis->newcmd(); ### Abstraction for handling Lua scripts as plain Redis commands ### -A scripted command in Predis is an abstraction for [Lua scripting](http://redis.io/commands/eval) +A scriptable command in Predis is an abstraction for [Lua scripting](http://redis.io/commands/eval) in Redis >= 2.6 allowing to use a Lua script as if it was a Redis command registered in the profile -used by the client. Internally scripted commands use [EVALSHA](http://redis.io/commands/evalsha) to -refer to a Lua script by its SHA1 hash in order to save bandwidth, but they are capable of falling +used by the client. Internally scriptable commands use [EVALSHA](http://redis.io/commands/evalsha) to +identify a Lua script by its SHA1 hash in order to save bandwidth, but they are capable of falling back to [EVAL](http://redis.io/commands/eval) when needed: ```php -class ListPushRandomValue extends Predis\Command\ScriptedCommand +class ListPushRandomValue extends Predis\Command\ScriptCommand { public function getKeysCount() { diff --git a/examples/MasterSlaveReplicationComplex.php b/examples/MasterSlaveReplicationComplex.php index bc5fa83a..3fa3d1e7 100644 --- a/examples/MasterSlaveReplicationComplex.php +++ b/examples/MasterSlaveReplicationComplex.php @@ -11,23 +11,23 @@ require 'SharedConfigurations.php'; -// Predis allows to set Lua scripts as read-only operations in the context of -// replication. This works for both EVAL and EVALSHA and also for the client-side -// abstraction built upon them (Predis\Command\ScriptedCommand). This example -// shows a slightly more complex configuration that injects a new scripted command -// in the server profile used by the new client instance and marks it marks it as -// a read-only operation for replication so that it will be executed on slaves. +// Predis allows to set Lua scripts as read-only operations for replication. +// This works for both EVAL and EVALSHA and also for the client-side abstraction +// built upon them (Predis\Command\ScriptCommand). This example shows a slightly +// more complex configuration that injects a new script command in the server +// profile used by the new client instance and marks it marks it as a read-only +// operation for replication so that it will be executed on slaves. -use Predis\Command\ScriptedCommand; +use Predis\Command\ScriptCommand; use Predis\Connection\MasterSlaveReplication; use Predis\Replication\ReplicationStrategy; // ------------------------------------------------------------------------- // -// Define a new scripted command that returns all the fields -// of a variable number of hashes with a single roundtrip. +// Define a new script command that returns all the fields of a variable number +// of hashes with a single roundtrip. -class HashMultipleGetAll extends ScriptedCommand { +class HashMultipleGetAll extends ScriptCommand { const BODY = <<getErrorType() === 'NOSCRIPT') { + if ($command instanceof ScriptCommand && $response->getErrorType() === 'NOSCRIPT') { $eval = $this->createCommand('eval'); $eval->setRawArguments($command->getEvalArguments()); diff --git a/lib/Predis/Cluster/PredisStrategy.php b/lib/Predis/Cluster/PredisStrategy.php index 1a27bef8..d506e2cd 100644 --- a/lib/Predis/Cluster/PredisStrategy.php +++ b/lib/Predis/Cluster/PredisStrategy.php @@ -13,7 +13,7 @@ namespace Predis\Cluster; use Predis\Cluster\Hash\HashGeneratorInterface; use Predis\Command\CommandInterface; -use Predis\Command\ScriptedCommand; +use Predis\Command\ScriptCommand; /** * Default class used by Predis for client-side sharding to calculate @@ -300,7 +300,7 @@ class PredisStrategy implements StrategyInterface */ protected function getKeyFromScriptingCommands(CommandInterface $command) { - if ($command instanceof ScriptedCommand) { + if ($command instanceof ScriptCommand) { $keys = $command->getKeys(); } else { $keys = array_slice($args = $command->getArguments(), 2, $args[1]); diff --git a/lib/Predis/Cluster/RedisStrategy.php b/lib/Predis/Cluster/RedisStrategy.php index 39b1e42d..5cbbff95 100644 --- a/lib/Predis/Cluster/RedisStrategy.php +++ b/lib/Predis/Cluster/RedisStrategy.php @@ -12,7 +12,7 @@ namespace Predis\Cluster; use Predis\Command\CommandInterface; -use Predis\Command\ScriptedCommand; +use Predis\Command\ScriptCommand; /** * Default class used by Predis to calculate hashes out of keys of @@ -250,7 +250,7 @@ class RedisStrategy implements StrategyInterface */ protected function getKeyFromScriptingCommands(CommandInterface $command) { - if ($command instanceof ScriptedCommand) { + if ($command instanceof ScriptCommand) { $keys = $command->getKeys(); } else { $keys = array_slice($args = $command->getArguments(), 2, $args[1]); diff --git a/lib/Predis/Command/ScriptedCommand.php b/lib/Predis/Command/ScriptCommand.php similarity index 94% rename from lib/Predis/Command/ScriptedCommand.php rename to lib/Predis/Command/ScriptCommand.php index 1858998f..2d496346 100644 --- a/lib/Predis/Command/ScriptedCommand.php +++ b/lib/Predis/Command/ScriptCommand.php @@ -13,12 +13,12 @@ namespace Predis\Command; /** * Base class used to implement an higher level abstraction for "virtual" - * commands based on EVAL. + * commands for Lua scripting based on EVAL and EVALSHA. * * @link http://redis.io/commands/eval * @author Daniele Alessandri */ -abstract class ScriptedCommand extends ServerEvalSHA +abstract class ScriptCommand extends ServerEvalSHA { /** * Gets the body of a Lua script. diff --git a/tests/Predis/ClientTest.php b/tests/Predis/ClientTest.php index 6297fb28..fb238e38 100644 --- a/tests/Predis/ClientTest.php +++ b/tests/Predis/ClientTest.php @@ -726,9 +726,9 @@ class ClientTest extends StandardTestCase /** * @group disconnected */ - public function testClientResendScriptedCommandUsingEvalOnNoScriptErrors() + public function testClientResendScriptCommandUsingEvalOnNoScriptErrors() { - $command = $this->getMockForAbstractClass('Predis\Command\ScriptedCommand', array(), '', true, true, true, array('parseResponse')); + $command = $this->getMockForAbstractClass('Predis\Command\ScriptCommand', array(), '', true, true, true, array('parseResponse')); $command->expects($this->once()) ->method('getScript') ->will($this->returnValue('return redis.call(\'exists\', KEYS[1])')); diff --git a/tests/Predis/Cluster/PredisStrategyTest.php b/tests/Predis/Cluster/PredisStrategyTest.php index 025256b9..7b3a58b6 100644 --- a/tests/Predis/Cluster/PredisStrategyTest.php +++ b/tests/Predis/Cluster/PredisStrategyTest.php @@ -153,7 +153,7 @@ class PredisStrategyTest extends StandardTestCase /** * @group disconnected */ - public function testKeysForScriptCommand() + public function testKeysForEvalCommand() { $strategy = $this->getClusterStrategy(); $profile = Profile\Factory::getDevelopment(); @@ -168,12 +168,12 @@ class PredisStrategyTest extends StandardTestCase /** * @group disconnected */ - public function testKeysForScriptedCommand() + public function testKeysForScriptCommand() { $strategy = $this->getClusterStrategy(); $arguments = array('{key}:1', '{key}:2', 'value1', 'value2'); - $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount')); + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); $command->expects($this->once()) ->method('getScript') ->will($this->returnValue('return true')); @@ -182,7 +182,7 @@ class PredisStrategyTest extends StandardTestCase ->will($this->returnValue(2)); $command->setArguments($arguments); - $this->assertNotNull($strategy->getHash($command), "Scripted Command [{$command->getId()}]"); + $this->assertNotNull($strategy->getHash($command), "Script Command [{$command->getId()}]"); } /** diff --git a/tests/Predis/Cluster/RedisStrategyTest.php b/tests/Predis/Cluster/RedisStrategyTest.php index fc56587a..519f7d5a 100644 --- a/tests/Predis/Cluster/RedisStrategyTest.php +++ b/tests/Predis/Cluster/RedisStrategyTest.php @@ -162,7 +162,7 @@ class RedisStrategyTest extends StandardTestCase /** * @group disconnected */ - public function testKeysForScriptCommand() + public function testKeysForEvalCommand() { $strategy = $this->getClusterStrategy(); $profile = Profile\Factory::getDevelopment(); @@ -177,12 +177,12 @@ class RedisStrategyTest extends StandardTestCase /** * @group disconnected */ - public function testKeysForScriptedCommand() + public function testKeysForScriptCommand() { $strategy = $this->getClusterStrategy(); $arguments = array('key:1', 'value1'); - $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount')); + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); $command->expects($this->once()) ->method('getScript') ->will($this->returnValue('return true')); @@ -191,7 +191,7 @@ class RedisStrategyTest extends StandardTestCase ->will($this->returnValue(1)); $command->setArguments($arguments); - $this->assertNotNull($strategy->getHash($command), "Scripted Command [{$command->getId()}]"); + $this->assertNotNull($strategy->getHash($command), "Script Command [{$command->getId()}]"); } /** diff --git a/tests/Predis/Command/ScriptedCommandTest.php b/tests/Predis/Command/ScriptedCommandTest.php index ad4755fc..540729f8 100644 --- a/tests/Predis/Command/ScriptedCommandTest.php +++ b/tests/Predis/Command/ScriptedCommandTest.php @@ -16,7 +16,7 @@ use PHPUnit_Framework_TestCase as StandardTestCase; /** * @group realm-scripting */ -class ScriptedCommandTest extends StandardTestCase +class ScriptCommandTest extends StandardTestCase { const LUA_SCRIPT = 'return { KEYS[1], KEYS[2], ARGV[1], ARGV[2] }'; const LUA_SCRIPT_SHA1 = '6e07f61f502e36d123fe28523076af588f5c315e'; @@ -28,7 +28,7 @@ class ScriptedCommandTest extends StandardTestCase { $arguments = array('key1', 'key2', 'value1', 'value2'); - $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount')); + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); $command->expects($this->once()) ->method('getScript') ->will($this->returnValue(self::LUA_SCRIPT)); @@ -48,7 +48,7 @@ class ScriptedCommandTest extends StandardTestCase { $arguments = array('key1', 'key2', 'value1', 'value2'); - $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount')); + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); $command->expects($this->once()) ->method('getScript') ->will($this->returnValue(self::LUA_SCRIPT)); @@ -67,7 +67,7 @@ class ScriptedCommandTest extends StandardTestCase { $arguments = array('value1', 'value2', 'value3'); - $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount')); + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); $command->expects($this->once()) ->method('getScript') ->will($this->returnValue(self::LUA_SCRIPT)); @@ -86,7 +86,7 @@ class ScriptedCommandTest extends StandardTestCase { $arguments = array('key1', 'key2', 'value1', 'value2'); - $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount')); + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); $command->expects($this->once()) ->method('getScript') ->will($this->returnValue(self::LUA_SCRIPT)); @@ -105,7 +105,7 @@ class ScriptedCommandTest extends StandardTestCase { $arguments = array('value1', 'value2', 'value3'); - $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount')); + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); $command->expects($this->once()) ->method('getScript') ->will($this->returnValue(self::LUA_SCRIPT)); @@ -124,7 +124,7 @@ class ScriptedCommandTest extends StandardTestCase { $arguments = array('key1', 'key2', 'value1', 'value2'); - $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount')); + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); $command->expects($this->once()) ->method('getScript') ->will($this->returnValue(self::LUA_SCRIPT)); @@ -143,7 +143,7 @@ class ScriptedCommandTest extends StandardTestCase { $arguments = array('key1', 'key2', 'value1', 'value2'); - $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount')); + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount')); $command->expects($this->once()) ->method('getScript') ->will($this->returnValue(self::LUA_SCRIPT)); diff --git a/tests/Predis/Replication/ReplicationStrategyTest.php b/tests/Predis/Replication/ReplicationStrategyTest.php index 7c9e935a..7424e010 100644 --- a/tests/Predis/Replication/ReplicationStrategyTest.php +++ b/tests/Predis/Replication/ReplicationStrategyTest.php @@ -190,11 +190,11 @@ class ReplicationStrategyTest extends StandardTestCase /** * @group disconnected */ - public function testSetLuaScriptAsReadOperationWorksWithScriptedCommand() + public function testSetLuaScriptAsReadOperationWorksWithScriptCommand() { $strategy = new ReplicationStrategy(); - $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript')); + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript')); $command->expects($this->any()) ->method('getScript') ->will($this->returnValue($script = 'return true')); @@ -213,11 +213,11 @@ class ReplicationStrategyTest extends StandardTestCase /** * @group disconnected */ - public function testSetLuaScriptAsReadOperationWorksWithScriptedCommandAndCallableCheck() + public function testSetLuaScriptAsReadOperationWorksWithScriptCommandAndCallableCheck() { $strategy = new ReplicationStrategy(); - $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript')); + $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript')); $command->expects($this->any()) ->method('getScript') ->will($this->returnValue($script = 'return true'));