1
|
<?php
|
2
|
|
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
|
|
12
|
require 'SharedConfigurations.php';
|
13
|
|
14
|
// This example will not work with versions of Redis < 2.6.
|
15
|
//
|
16
|
// Additionally to the EVAL command defined in the current development profile, the new
|
17
|
// Predis\Command\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\Command\ScriptedCommand;
|
22
|
|
23
|
class IncrementExistingKeysBy extends ScriptedCommand
|
24
|
{
|
25
|
public function getKeysCount()
|
26
|
{
|
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;
|
30
|
}
|
31
|
|
32
|
public function getScript()
|
33
|
{
|
34
|
return
|
35
|
<<<LUA
|
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
|
48
|
LUA;
|
49
|
}
|
50
|
}
|
51
|
|
52
|
$client = new Predis\Client($single_server);
|
53
|
|
54
|
$client->getProfile()->defineCommand('increxby', 'IncrementExistingKeysBy');
|
55
|
|
56
|
$client->mset('foo', 10, 'foobar', 100);
|
57
|
|
58
|
var_export($client->increxby('foo', 'foofoo', 'foobar', 50));
|
59
|
|
60
|
/*
|
61
|
array (
|
62
|
0 => 60,
|
63
|
1 => NULL,
|
64
|
2 => 150,
|
65
|
)
|
66
|
*/
|