Procházet zdrojové kódy

Add an example for server-side scripting with Lua using Predis abstractions.

Daniele Alessandri před 14 roky
rodič
revize
092b822033
1 změnil soubory, kde provedl 32 přidání a 0 odebrání
  1. 32 0
      examples/ServerSideScripting.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