Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 67 lines (53 sloc) 1.615 kb
092b822 @nrk Add an example for server-side scripting with Lua using Predis abstracti...
authored
1 <?php
2
9608a97 @nrk Add a license header to PHP files.
authored
3 /*
4 * This file is part of the Predis package.
5 *
6 * (c) Daniele Alessandri <suppakilla@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
092b822 @nrk Add an example for server-side scripting with Lua using Predis abstracti...
authored
12 require 'SharedConfigurations.php';
13
dc9bd4e @nrk Add notice in the example of servers-side scripting with Lua.
authored
14 // This example will not work with versions of Redis < 2.6.
15 //
092b822 @nrk Add an example for server-side scripting with Lua using Predis abstracti...
authored
16 // Additionally to the EVAL command defined in the current development profile, the new
17 // Predis\Commands\ScriptedCommand base class can be used to build an higher abstraction
18 // for our "scripted" commands so that they will appear just like any other command on
19 // the client-side. This is a quick example used to implement INCREX.
20
21 use Predis\Commands\ScriptedCommand;
22
5af2b62 @nrk Scripting abstraction can use negatives to calculate number of keys.
authored
23 class IncrementExistingKeysBy extends ScriptedCommand
20d872a @nrk Apply some coding style changes.
authored
24 {
371ac91 @nrk [tests] Rewrite the whole test suite to allow more granular testing.
authored
25 public function getKeysCount()
20d872a @nrk Apply some coding style changes.
authored
26 {
5af2b62 @nrk Scripting abstraction can use negatives to calculate number of keys.
authored
27 // Tell Predis to use all the arguments but the last one as arguments
28 // for KEYS. The last one will be used to populate ARGV.
29 return -1;
092b822 @nrk Add an example for server-side scripting with Lua using Predis abstracti...
authored
30 }
31
20d872a @nrk Apply some coding style changes.
authored
32 public function getScript()
33 {
092b822 @nrk Add an example for server-side scripting with Lua using Predis abstracti...
authored
34 return
35 <<<LUA
5af2b62 @nrk Scripting abstraction can use negatives to calculate number of keys.
authored
36 local cmd, insert = redis.call, table.insert
37 local increment, results = ARGV[1], { }
38
39 for idx, key in ipairs(KEYS) do
40 if cmd('exists', key) == 1 then
41 insert(results, idx, cmd('incrby', key, increment))
42 else
43 insert(results, idx, false)
44 end
45 end
46
47 return results
092b822 @nrk Add an example for server-side scripting with Lua using Predis abstracti...
authored
48 LUA;
49 }
50 }
51
626520b @nrk Add specific server profile alias for Redis 2.6.
authored
52 $client = new Predis\Client($single_server, '2.6');
20d872a @nrk Apply some coding style changes.
authored
53
5af2b62 @nrk Scripting abstraction can use negatives to calculate number of keys.
authored
54 $client->getProfile()->defineCommand('increxby', 'IncrementExistingKeysBy');
092b822 @nrk Add an example for server-side scripting with Lua using Predis abstracti...
authored
55
5af2b62 @nrk Scripting abstraction can use negatives to calculate number of keys.
authored
56 $client->mset('foo', 10, 'foobar', 100);
20d872a @nrk Apply some coding style changes.
authored
57
5af2b62 @nrk Scripting abstraction can use negatives to calculate number of keys.
authored
58 var_export($client->increxby('foo', 'foofoo', 'foobar', 50));
59
60 /*
61 array (
62 0 => 60,
63 1 => NULL,
64 2 => 150,
65 )
66 */
Something went wrong with that request. Please try again.