|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPDaemon\Clients\Redis; |
|
3
|
|
|
|
|
4
|
|
|
use PHPDaemon\Core\CallbackWrapper; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Easy wrapper for queue of eval's |
|
8
|
|
|
* |
|
9
|
|
|
* @package NetworkClients |
|
10
|
|
|
* @subpackage RedisClient |
|
11
|
|
|
* @author Efimenko Dmitriy <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
* Use exampe 1: |
|
14
|
|
|
* var $eval = $this->redis->meval($cb); |
|
15
|
|
|
* $eval->add('redis.call("set",KEYS[1],ARGV[1])', [$key1], [$arg1]); |
|
16
|
|
|
* $eval->add('redis.call("del",KEYS[1])', [$key2]); |
|
17
|
|
|
* $eval->add('redis.call("expireat",KEYS[1],ARGV[1])', $key1, $arg2); |
|
18
|
|
|
* $eval->exec(); |
|
19
|
|
|
* |
|
20
|
|
|
* Use exampe 2: |
|
21
|
|
|
* var $eval = $this->redis->meval($cb); |
|
22
|
|
|
* $eval('redis.call("set",KEYS[1],ARGV[1])', [$key1], [$arg1]); |
|
23
|
|
|
* $eval('redis.call("del",KEYS[1])', [$key2]); |
|
24
|
|
|
* $eval('redis.call("expireat",KEYS[1],ARGV[1])', $key1, $arg2); |
|
25
|
|
|
* $eval(); |
|
26
|
|
|
* |
|
27
|
|
|
* Result request: |
|
28
|
|
|
* eval 'redis.call("set",KEYS[1],ARGV[1]);redis.call("del",KEYS[2]);redis.call("expireat",KEYS[1],ARGV[2])' 2 $key1 $key2 $arg1 $arg2 |
|
29
|
|
|
*/ |
|
30
|
|
|
class MultiEval |
|
31
|
|
|
{ |
|
32
|
|
|
use \PHPDaemon\Traits\ClassWatchdog; |
|
33
|
|
|
use \PHPDaemon\Traits\StaticObjectWatchdog; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array Listeners |
|
37
|
|
|
*/ |
|
38
|
|
|
public $listeners = []; |
|
39
|
|
|
protected $pool; |
|
40
|
|
|
protected $stack = []; |
|
41
|
|
|
protected $cachedParams = false; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Constructor |
|
45
|
|
|
* @param callable $cb Callback |
|
46
|
|
|
* @param Pool $pool Redis pool |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($cb, $pool) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->pool = $pool; |
|
51
|
|
|
if ($cb !== null) { |
|
52
|
|
|
$this->addListener($cb); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Adds listener |
|
58
|
|
|
* @param callable $cb |
|
59
|
|
|
*/ |
|
60
|
|
|
public function addListener($cb) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->listeners[] = CallbackWrapper::wrap($cb); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Clean up |
|
67
|
|
|
*/ |
|
68
|
|
|
public function cleanup() |
|
69
|
|
|
{ |
|
70
|
|
|
$this->listeners = []; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Adds eval command or calls execute() method |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
public function __invoke(...$args) |
|
78
|
|
|
{ |
|
79
|
|
|
if (!count($args)) { |
|
80
|
|
|
$this->execute(); |
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
$this->add(...$args); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Runs the stack of commands |
|
88
|
|
|
*/ |
|
89
|
|
|
public function execute() |
|
90
|
|
|
{ |
|
91
|
|
|
if (!count($this->stack)) { |
|
92
|
|
|
foreach ($this->listeners as $cb) { |
|
93
|
|
|
$cb($this->pool); |
|
94
|
|
|
} |
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$params = $this->getParams(); |
|
99
|
|
|
$params[] = function ($redis) { |
|
100
|
|
|
foreach ($this->listeners as $cb) { |
|
101
|
|
|
$cb($redis); |
|
102
|
|
|
} |
|
103
|
|
|
}; |
|
104
|
|
|
$this->pool->eval(...$params); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Return params for eval command |
|
109
|
|
|
* @return array |
|
|
|
|
|
|
110
|
|
|
*/ |
|
111
|
|
|
public function getParams() |
|
112
|
|
|
{ |
|
113
|
|
|
if ($this->cachedParams) { |
|
114
|
|
|
return $this->cachedParams; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$CMDS = []; |
|
118
|
|
|
$KEYS = []; |
|
119
|
|
|
$ARGV = []; |
|
120
|
|
|
$KEYNUM = 0; |
|
121
|
|
|
$ARGNUM = 0; |
|
122
|
|
|
|
|
123
|
|
|
foreach ($this->stack as $part) { |
|
124
|
|
|
list($cmd, $keys, $argv) = $part; |
|
125
|
|
|
|
|
126
|
|
View Code Duplication |
if (!empty($keys)) { |
|
|
|
|
|
|
127
|
|
|
$cmd = preg_replace_callback('~KEYS\[(\d+)\]~', function ($m) use (&$KEYS, &$KEYNUM, $keys) { |
|
128
|
|
|
$key = $keys[$m[1] - 1]; |
|
129
|
|
|
if (!isset($KEYS[$key])) { |
|
130
|
|
|
$KEYS[$key] = ++$KEYNUM; |
|
131
|
|
|
} |
|
132
|
|
|
return 'KEYS[' . $KEYS[$key] . ']'; |
|
133
|
|
|
}, $cmd); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
View Code Duplication |
if (!empty($argv)) { |
|
|
|
|
|
|
137
|
|
|
$cmd = preg_replace_callback('~ARGV\[(\d+)\]~', function ($m) use (&$ARGV, &$ARGNUM, $argv) { |
|
138
|
|
|
$arg = $argv[$m[1] - 1]; |
|
139
|
|
|
if (!isset($ARGV[$arg])) { |
|
140
|
|
|
$ARGV[$arg] = ++$ARGNUM; |
|
141
|
|
|
} |
|
142
|
|
|
return 'ARGV[' . $ARGV[$arg] . ']'; |
|
143
|
|
|
}, $cmd); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$CMDS[] = $cmd; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $this->cachedParams = array_merge( |
|
|
|
|
|
|
150
|
|
|
[implode(';', $CMDS), count($KEYS)], |
|
151
|
|
|
array_keys($KEYS), |
|
152
|
|
|
array_keys($ARGV) |
|
153
|
|
|
); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Adds eval command in stack |
|
158
|
|
|
* @param string $cmd Lua script |
|
159
|
|
|
* @param mixed $keys Keys |
|
160
|
|
|
* @param mixed $argv Arguments |
|
161
|
|
|
*/ |
|
162
|
|
|
public function add($cmd, $keys = null, $argv = null) |
|
163
|
|
|
{ |
|
164
|
|
View Code Duplication |
if ($keys !== null) { |
|
|
|
|
|
|
165
|
|
|
if (is_scalar($keys)) { |
|
166
|
|
|
$keys = [(string)$keys]; |
|
167
|
|
|
} elseif (!is_array($keys)) { |
|
168
|
|
|
throw new \Exception("Keys must be an array or scalar"); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
View Code Duplication |
if ($argv !== null) { |
|
|
|
|
|
|
172
|
|
|
if (is_scalar($argv)) { |
|
173
|
|
|
$argv = [(string)$argv]; |
|
174
|
|
|
} elseif (!is_array($argv)) { |
|
175
|
|
|
throw new \Exception("Argv must be an array or scalar"); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$this->cachedParams = false; |
|
180
|
|
|
$this->stack[] = [$cmd, $keys, $argv]; |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: