Skip to content

Commit 6a53379

Browse files
Added PIPELINE/MULTI unit test for EVAL, updated docs with new commands
1 parent 568aad3 commit 6a53379

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed

README.markdown

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,3 +2411,127 @@ Get or Set the redis config keys.
24112411
$redis->config("GET", "*max-*-entries*");
24122412
$redis->config("SET", "dir", "/var/run/redis/dumps/");
24132413
</pre>
2414+
2415+
## eval
2416+
##### Description
2417+
Evaluate a LUA script serverside
2418+
##### Parameters
2419+
*script* string.
2420+
*args* array, optional.
2421+
*num_keys* int, optional.
2422+
##### Return value
2423+
Mixed. What is returned depends on what the LUA script itself returns, which could be a scalar value (int/string), or an array.
2424+
Arrays that are returned can also contain other arrays, if that's how it was set up in your LUA script. If there is an error
2425+
executing the LUA script, the getLastError() function can tell you the message that came back from Redis (e.g. compile error).
2426+
##### Examples
2427+
<pre>
2428+
$redis->eval("return 1"); // Returns an integer: 1
2429+
$redis->eval("return {1,2,3}"); // Returns Array(1,2,3)
2430+
$redis->del('mylist');
2431+
$redis->rpush('mylist','a');
2432+
$redis->rpush('mylist','b');
2433+
$redis->rpush('mylist','c');
2434+
// Nested response: Array(1,2,3,Array('a','b','c'));
2435+
$redis->eval("return {1,2,3,redis.call('lrange','mylist',0,-1)}}");
2436+
</pre>
2437+
2438+
## evalSha
2439+
##### Description
2440+
Evaluate a LUA script serverside, from the SHA1 hash of the script instead of the script itself. In order to run this command Redis
2441+
will have to have already loaded the script, either by running it or via the SCRIPT LOAD command.
2442+
##### Parameters
2443+
*script_sha* string. The sha1 encoded hash of the script you want to run.<br>
2444+
*args* array, optional. Arguments to pass to the LUA script.<br>
2445+
*num_keys* int, optional. The number of arguments that should go into the KEYS array, vs. the ARGV array when Redis spins the script
2446+
##### Return value
2447+
Mixed. See EVAL
2448+
##### Examples
2449+
<pre>
2450+
$script = 'return 1';
2451+
$sha = $redis->script('load', $script);
2452+
$redis->evalSha($sha); // Returns 1
2453+
</pre>
2454+
2455+
## script
2456+
##### Description
2457+
Execute the Redis SCRIPT command to perform various operations on the scripting subsystem.
2458+
##### Usage
2459+
<pre>
2460+
$redis->script('load', $script);
2461+
$redis->script('flush');
2462+
$redis->script('kill');
2463+
$redis->script('exists', $script1, [$script2, $script3, ...]);
2464+
</pre>
2465+
##### Return value
2466+
* SCRIPT LOAD will return the SHA1 hash of the passed script on success, and FALSE on failure.
2467+
* SCRIPT FLUSH should always return TRUE
2468+
* SCRIPT KILL will return true if a script was able to be killed and false if not
2469+
* SCRIPT EXISTS will return an array with TRUE or FALSE for each passed script
2470+
2471+
## getLastError
2472+
##### Description
2473+
The last error message (if any) returned from a SCRIPT call
2474+
##### Parameters
2475+
*none*
2476+
##### Return Value
2477+
A string with the last returned script based error message, or NULL if there is no error
2478+
##### Examples
2479+
<pre>
2480+
$redis->eval('this-is-not-lua');
2481+
$err = $redis->getLastError();
2482+
// "ERR Error compiling script (new function): user_script:1: '=' expected near '-'"
2483+
</pre>
2484+
2485+
## _prefix
2486+
##### Description
2487+
A utility method to prefix the value with the prefix setting for phpredis.
2488+
##### Parameters
2489+
*value* string. The value you wish to prefix
2490+
##### Return value
2491+
If a prefix is set up, the value now prefixed. If there is no prefix, the value will be returned unchanged.
2492+
##### Examples
2493+
<pre>
2494+
$redis->setOpt(Redis::OPT_PREFIX, 'my-prefix:');
2495+
$redis->_prefix('my-value'); // Will return 'my-prefix:my-value'
2496+
</pre>
2497+
2498+
## _unserialize
2499+
##### Description
2500+
A utility method to unserialize data with whatever serializer is set up. If there is no serializer set, the value will be
2501+
returned unchanged. If there is a serializer set up, and the data passed in is malformed, an exception will be thrown.
2502+
This can be useful if phpredis is serializing values, and you return something from redis in a LUA script that is serialized.
2503+
##### Parameters
2504+
*value* string. The value to be unserialized
2505+
##### Examples
2506+
<pre>
2507+
$redis->setOpt(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
2508+
$redis->_unserialize('a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}'); // Will return Array(1,2,3)
2509+
</pre>
2510+
2511+
## dump
2512+
##### Description
2513+
Dump a key out of a redis database, the value of which can later be passed into redis using the RESTORE command. The data
2514+
that comes out of DUMP is a binary representation of the key as Redis stores it.
2515+
##### Parameters
2516+
*key* string
2517+
##### Return value
2518+
The Redis encoded value of the key, or FALSE if the key doesn't exist
2519+
##### Examples
2520+
<pre>
2521+
$redis->set('foo', 'bar');
2522+
$val = $redis->dump('foo'); // $val will be the Redis encoded key value
2523+
</pre>
2524+
2525+
## restore
2526+
##### Description
2527+
Restore a key from the result of a DUMP operation.
2528+
##### Parameters
2529+
*key* string. The key name
2530+
*ttl* integer. How long the key should live (if zero, no expire will be set on the key)
2531+
*value* string (binary). The Redis encoded key value (from DUMP)
2532+
##### Examples
2533+
<pre>
2534+
$redis->set('foo', 'bar');
2535+
$val = $redis->dump('foo');
2536+
$redis->restore('bar', 0, $val); // The key 'bar', will now be equal to the key 'foo'
2537+
</pre>

tests/TestRedis.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3007,7 +3007,8 @@ public function testEval() {
30073007
$this->assertTrue($set == Array('d','e','f'));
30083008

30093009
// Test an empty MULTI BULK response
3010-
$empty_resp = $this->redis->eval("return redis.call('lrange', 'not-any-kind-of-set', 0, -1)");
3010+
$this->redis->del('not-any-kind-of-list');
3011+
$empty_resp = $this->redis->eval("return redis.call('lrange', 'not-any-kind-of-list', 0, -1)");
30113012
$this->assertTrue(is_array($empty_resp) && empty($empty_resp));
30123013

30133014
// Now test a nested reply
@@ -3041,6 +3042,24 @@ public function testEval() {
30413042
$eval_result = $this->redis->eval($nested_script);
30423043
$this->assertTrue(count($this->array_diff_recursive($eval_result, $expected)) == 0);
30433044

3045+
/*
3046+
* Nested reply wihin a multi/pipeline block
3047+
*/
3048+
3049+
$num_scripts = 10;
3050+
3051+
foreach(Array(Redis::PIPELINE, Redis::MULTI) as $mode) {
3052+
$this->redis->multi($mode);
3053+
for($i=0;$i<$num_scripts;$i++) {
3054+
$this->redis->eval($nested_script);
3055+
}
3056+
$replies = $this->redis->exec();
3057+
3058+
foreach($replies as $reply) {
3059+
$this->assertTrue(count($this->array_diff_recursive($reply, $expected)) == 0);
3060+
}
3061+
}
3062+
30443063
/*
30453064
* KEYS/ARGV
30463065
*/

0 commit comments

Comments
 (0)