Преглед на файлове

Merge branch 'redis_scripting'

Daniele Alessandri преди 14 години
родител
ревизия
13ac03e134
променени са 4 файла, в които са добавени 67 реда и са изтрити 0 реда
  1. 32 0
      examples/ServerSideScripting.php
  2. 21 0
      lib/Predis/Commands/ScriptedCommand.php
  3. 13 0
      lib/Predis/Commands/ServerEval.php
  4. 1 0
      lib/Predis/Profiles/ServerVersionNext.php

+ 32 - 0
examples/ServerSideScripting.php

@@ -0,0 +1,32 @@
+<?php
+
+require 'SharedConfigurations.php';
+
+// Additionally to the EVAL command defined in the current development profile, the new
+// Predis\Commands\ScriptedCommand base class can be used to build an higher abstraction
+// for our "scripted" commands so that they will appear just like any other command on
+// the client-side. This is a quick example used to implement INCREX.
+
+use Predis\Commands\ScriptedCommand;
+
+class IncrementExistingKey extends ScriptedCommand {
+    protected function keysCount() {
+        return 1;
+    }
+
+    public function getScript() {
+        return
+<<<LUA
+    if redis('exists', KEYS[1]) == 1 then
+        return redis('incr', KEYS[1])
+    end
+LUA;
+    }
+}
+
+$client = new Predis\Client($single_server, 'dev');
+$client->getProfile()->defineCommand('increx', 'IncrementExistingKey');
+
+$client->set('foo', 10);
+var_dump($client->increx('foo'));       // int(11)
+var_dump($client->increx('bar'));       // NULL

+ 21 - 0
lib/Predis/Commands/ScriptedCommand.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace Predis\Commands;
+
+abstract class ScriptedCommand extends ServerEval {
+    public abstract function getScript();
+
+    protected function keysCount() {
+        // 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) {
+        return array_merge(array($this->getScript(), $this->keysCount()), $arguments);
+    }
+
+    protected function getKeys() {
+        return array_slice($this->getArguments(), 2, $this->keysCount());
+    }
+}

+ 13 - 0
lib/Predis/Commands/ServerEval.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace Predis\Commands;
+
+class ServerEval extends Command {
+    public function getId() {
+        return 'EVAL';
+    }
+
+    protected function canBeHashed() {
+        return false;
+    }
+}

+ 1 - 0
lib/Predis/Profiles/ServerVersionNext.php

@@ -20,6 +20,7 @@ class ServerVersionNext extends ServerVersion22 {
             /* remote server control commands */
             'info'                      => '\Predis\Commands\ServerInfoV24x',
             'client'                    => '\Predis\Commands\ServerClient',
+            'eval'                      => '\Predis\Commands\ServerEval',
         ));
     }
 }