From bd027341be3650dd390911a0feb6184fb409d542 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Tue, 3 May 2011 15:07:56 +0200 Subject: [PATCH 1/6] New command: EVAL (Redis v2.4-dev). --- lib/Predis/Commands/ServerEval.php | 13 +++++++++++++ lib/Predis/Profiles/ServerVersionNext.php | 1 + 2 files changed, 14 insertions(+) create mode 100644 lib/Predis/Commands/ServerEval.php diff --git a/lib/Predis/Commands/ServerEval.php b/lib/Predis/Commands/ServerEval.php new file mode 100644 index 00000000..b214bf67 --- /dev/null +++ b/lib/Predis/Commands/ServerEval.php @@ -0,0 +1,13 @@ + '\Predis\Commands\ServerInfoV24x', 'client' => '\Predis\Commands\ServerClient', + 'eval' => '\Predis\Commands\ServerEval', )); } } From 2bdfed5071910e9dd317c9910ef7c6fdf9bb8db3 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Tue, 3 May 2011 15:14:51 +0200 Subject: [PATCH 2/6] Add an abstraction for a virtual command that leverages Redis scripting with the new EVAL command. --- lib/Predis/Commands/ScriptedCommand.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 lib/Predis/Commands/ScriptedCommand.php diff --git a/lib/Predis/Commands/ScriptedCommand.php b/lib/Predis/Commands/ScriptedCommand.php new file mode 100644 index 00000000..bb366e18 --- /dev/null +++ b/lib/Predis/Commands/ScriptedCommand.php @@ -0,0 +1,16 @@ +keysCount()) === -1) { + $keys = count($arguments); + } + return array_merge(array($this->getScript(), $keys), $arguments); + } +} From a386263d84982e0e3ef135e62b3a7e89dc0ad4e5 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Tue, 3 May 2011 17:37:39 +0200 Subject: [PATCH 3/6] Add an helper method to get the actual keys from the whole argument list. --- lib/Predis/Commands/ScriptedCommand.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/Predis/Commands/ScriptedCommand.php b/lib/Predis/Commands/ScriptedCommand.php index bb366e18..69c9ca0c 100644 --- a/lib/Predis/Commands/ScriptedCommand.php +++ b/lib/Predis/Commands/ScriptedCommand.php @@ -13,4 +13,12 @@ abstract class ScriptedCommand extends ServerEval { } return array_merge(array($this->getScript(), $keys), $arguments); } + + protected function getKeys() { + $arguments = $this->getArguments(); + if (($keys = $this->keysCount()) === -1) { + return array_slice($arguments, 2); + } + return array_slice($arguments, 2, $keys); + } } From ca22208ce87f8bb2e00a40905815a0e48b85540b Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Tue, 3 May 2011 18:00:30 +0200 Subject: [PATCH 4/6] Define a default value for the number of values used to populate KEYS. --- lib/Predis/Commands/ScriptedCommand.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/Predis/Commands/ScriptedCommand.php b/lib/Predis/Commands/ScriptedCommand.php index 69c9ca0c..564b25de 100644 --- a/lib/Predis/Commands/ScriptedCommand.php +++ b/lib/Predis/Commands/ScriptedCommand.php @@ -5,7 +5,12 @@ namespace Predis\Commands; abstract class ScriptedCommand extends ServerEval { public abstract function getScript(); - public abstract function keysCount(); + protected function keysCount() { + // The default behaviour is to use the first argument as the only value + // for KEYS and the rest of the arguments (if any) for ARGV. When -1 is + // returned, all the arguments are considered as values for KEYS. + return 1; + } protected function filterArguments(Array $arguments) { if (($keys = $this->keysCount()) === -1) { From 0e0cd865e7291d2355de60b642e78c9a2d6886fb Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Fri, 13 May 2011 19:53:03 +0200 Subject: [PATCH 5/6] Change the default value for the number of arguments used to populate KEYS. --- lib/Predis/Commands/ScriptedCommand.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/lib/Predis/Commands/ScriptedCommand.php b/lib/Predis/Commands/ScriptedCommand.php index 564b25de..8224a1cc 100644 --- a/lib/Predis/Commands/ScriptedCommand.php +++ b/lib/Predis/Commands/ScriptedCommand.php @@ -6,24 +6,16 @@ abstract class ScriptedCommand extends ServerEval { public abstract function getScript(); protected function keysCount() { - // The default behaviour is to use the first argument as the only value - // for KEYS and the rest of the arguments (if any) for ARGV. When -1 is - // returned, all the arguments are considered as values for KEYS. - return 1; + // The default behaviour for the base class is to use all the arguments + // passed to a scripted command to populate the KEYS table in Lua. + return count($this->getArguments()); } protected function filterArguments(Array $arguments) { - if (($keys = $this->keysCount()) === -1) { - $keys = count($arguments); - } - return array_merge(array($this->getScript(), $keys), $arguments); + return array_merge(array($this->getScript(), $this->keysCount()), $arguments); } protected function getKeys() { - $arguments = $this->getArguments(); - if (($keys = $this->keysCount()) === -1) { - return array_slice($arguments, 2); - } - return array_slice($arguments, 2, $keys); + return array_slice($this->getArguments(), 2, $this->keysCount()); } } From 092b82203368e13a31dadd1353cacde0a051e585 Mon Sep 17 00:00:00 2001 From: Daniele Alessandri Date: Fri, 13 May 2011 20:06:06 +0200 Subject: [PATCH 6/6] Add an example for server-side scripting with Lua using Predis abstractions. --- examples/ServerSideScripting.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 examples/ServerSideScripting.php diff --git a/examples/ServerSideScripting.php b/examples/ServerSideScripting.php new file mode 100644 index 00000000..df1599c5 --- /dev/null +++ b/examples/ServerSideScripting.php @@ -0,0 +1,32 @@ +getProfile()->defineCommand('increx', 'IncrementExistingKey'); + +$client->set('foo', 10); +var_dump($client->increx('foo')); // int(11) +var_dump($client->increx('bar')); // NULL