8000 scripting methods, created toc · jrtkcoder/phpredis@a6a51a9 · GitHub
[go: up one dir, main page]

Skip to content

Commit a6a51a9

Browse files
author
Tit Petric
committed
scripting methods, created toc
1 parent 1c6ee3e commit a6a51a9

File tree

1 file changed

+149
-114
lines changed

1 file changed

+149
-114
lines changed

README.markdown

Lines changed: 149 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,120 +1879,6 @@ $redis->hSet('h', 'field2', 'value2');
18791879
$redis->hmGet('h', array('field1', 'field2')); /* returns array('field1' => 'value1', 'field2' => 'value2') */
18801880
~~~
18811881

1882-
### eval
1883-
-----
1884-
_**Description**_: Evaluate a LUA script serverside
1885-
##### *Parameters*
1886-
*script* string.
1887-
*args* array, optional.
1888-
*num_keys* int, optional.
1889-
##### *Return value*
1890-
Mixed. What is returned depends on what the LUA script itself returns, which could be a scalar value (int/string), or an array.
1891-
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
1892-
executing the LUA script, the getLastError() function can tell you the message that came back from Redis (e.g. compile error).
1893-
##### *Examples*
1894-
~~~
1895-
$redis->eval("return 1"); // Returns an integer: 1
1896-
$redis->eval("return {1,2,3}"); // Returns Array(1,2,3)
1897-
$redis->del('mylist');
1898-
$redis->rpush('mylist','a');
1899-
$redis->rpush('mylist','b');
1900-
$redis->rpush('mylist','c');
1901-
// Nested response: Array(1,2,3,Array('a','b','c'));
1902-
$redis->eval("return {1,2,3,redis.call('lrange','mylist',0,-1)}}");
1903-
~~~
1904-
1905-
### evalSha
1906-
-----
1907-
_**Description**_: Evaluate a LUA script serverside, from the SHA1 hash of the script instead of the script itself. In order to run this command Redis
1908-
will have to have already loaded the script, either by running it or via the SCRIPT LOAD command.
1909-
##### *Parameters*
1910-
*script_sha* string. The sha1 encoded hash of the script you want to run.
1911-
*args* array, optional. Arguments to pass to the LUA script.
1912-
*num_keys* int, optional. The number of arguments that should go into the KEYS array, vs. the ARGV array when Redis spins the script
1913-
##### *Return value*
1914-
Mixed. See EVAL
1915-
##### *Examples*
1916-
~~~
1917-
$script = 'return 1';
1918-
$sha = $redis->script('load', $script);
1919-
$redis->evalSha($sha); // Returns 1
1920-
~~~
1921-
1922-
### script
1923-
-----
1924-
_**Description**_: Execute the Redis SCRIPT command to perform various operations on the scripting subsystem.
1925-
##### *Usage*
1926-
~~~
1927-
$redis->script('load', $script);
1928-
$redis->script('flush');
1929-
$redis->script('kill');
1930-
$redis->script('exists', $script1, [$script2, $script3, ...]);
1931-
~~~
1932-
##### *Return value*
1933-
* SCRIPT LOAD will return the SHA1 hash of the passed script on success, and FALSE on failure.
1934-
* SCRIPT FLUSH should always return TRUE
1935-
* SCRIPT KILL will return true if a script was able to be killed and false if not
1936-
* SCRIPT EXISTS will return an array with TRUE or FALSE for each passed script
1937-
1938-
### getLastError
1939-
-----
1940-
_**Description**_: The last error message (if any)
1941-
##### *Parameters*
1942-
*none*
1943-
##### *Return value*
1944-
A string with the last returned script based error message, or NULL if there is no error
1945-
##### *Examples*
1946-
~~~
1947-
$redis->eval('this-is-not-lua');
1948-
$err = $redis->getLastError();
1949-
// "ERR Error compiling script (new function): user_script:1: '=' expected near '-'"
1950-
~~~
1951-
1952-
### clearLastError
1953-
-----
1954-
_**Description**_: Clear the last error message
1955-
##### *Parameters*
1956-
*none*
1957-
##### *Return value*
1958-
*BOOL* TRUE
1959-
##### *Examples*
1960-
~~~
1961-
$redis->set('x', 'a');
1962-
$redis->incr('x');
1963-
$err = $redis->getLastError();
1964-
// "ERR value is not an integer or out of range"
1965-
$redis->clearLastError();
1966-
$err = $redis->getLastError();
1967-
// NULL
1968-
~~~
1969-
1970-
### _prefix
1971-
-----
1972-
_**Description**_: A utility method to prefix the value with the prefix setting for phpredis.
1973-
##### *Parameters*
1974-
*value* string. The value you wish to prefix
1975-
##### *Return value*
1976-
If a prefix is set up, the value now prefixed. If there is no prefix, the value will be returned unchanged.
1977-
##### *Examples*
1978-
~~~
1979-
$redis->setOption(Redis::OPT_PREFIX, 'my-prefix:');
1980-
$redis->_prefix('my-value'); // Will return 'my-prefix:my-value'
1981-
~~~
1982-
1983-
### _unserialize
1984-
-----
1985-
_**Description**_: A utility method to unserialize data with whatever serializer is set up. If there is no serializer set, the value will be
1986-
returned unchanged. If there is a serializer set up, and the data passed in is malformed, an exception will be thrown.
1987-
This can be useful if phpredis is serializing values, and you return something from redis in a LUA script that is serialized.
1988-
##### *Parameters*
1989-
*value* string. The value to be unserialized
1990-
##### *Examples*
1991-
~~~
1992-
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
1993-
$redis->_unserialize('a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}'); // Will return Array(1,2,3)
1994-
~~~
1995-
19961882
### dump
19971883
-----
19981884
_**Description**_: Dump a key out of a redis database, the value of which can later be passed into redis using the RESTORE command. The data
@@ -2766,6 +2652,7 @@ $redis->zScore('key', 'val2'); /* 2.5 */
27662652
### zUnion
27672653
-----
27682654
_**Description**_: Creates an union of sorted sets given in second argument. The result of the union will be stored in the sorted set defined by the first argument.
2655+
27692656
The third optionnel argument defines `weights` to apply to the sorted sets in input. In this case, the `weights` will be multiplied by the score of each element in the sorted set before applying the aggregation.
27702657
The forth argument defines the `AGGREGATE` option which specify how the results of the union are aggregated.
27712658

@@ -2799,3 +2686,151 @@ $redis->zUnion('ko1', array('k1', 'k2')); /* 4, 'ko1' => array('val0', 'val1', '
27992686
$redis->zUnion('ko2', array('k1', 'k2'), array(1, 1)); /* 4, 'ko2' => array('val0', 'val1', 'val2', 'val3') */
28002687
$redis->zUnion('ko3', array('k1', 'k2'), array(5, 1)); /* 4, 'ko3' => array('val0', 'val2', 'val3', 'val1') */
28012688
~~~
2689+
2690+
2691+
## Scripting
2692+
2693+
* [eval](#) - Evaluate a LUA script serverside
2694+
* [evalSha](#) - Evaluate a LUA script serverside, from the SHA1 hash of the script instead of the script itself
2695+
* [script](#) - Execute the Redis SCRIPT command to perform various operations on the scripting subsystem
2696+
* [getLastError](#) - The last error message (if any)
2697+
* [clearLastError](#) - Clear the last error message
2698+
* [_prefix](#) - A utility method to prefix the value with the prefix setting for phpredis
2699+
* [_unserialize](#) - A utility method to unserialize data with whatever serializer is set up
2700+
2701+
### eval
2702+
-----
2703+
_**Description**_: Evaluate a LUA script serverside
2704+
2705+
##### *Parameters*
2706+
*script* string.
2707+
*args* array, optional.
2708+
*num_keys* int, optional.
2709+
2710+
##### *Return value*
2711+
Mixed. What is returned depends on what the LUA script itself returns, which could be a scalar value (int/string), or an array.
2712+
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
2713+
executing the LUA script, the getLastError() function can tell you the message that came back from Redis (e.g. compile error).
2714+
2715+
##### *Examples*
2716+
~~~
2717+
$redis->eval("return 1"); // Returns an integer: 1
2718+
$redis->eval("return {1,2,3}"); // Returns Array(1,2,3)
2719+
$redis->del('mylist');
2720+
$redis->rpush('mylist','a');
2721+
$redis->rpush('mylist','b');
2722+
$redis->rpush('mylist','c');
2723+
// Nested response: Array(1,2,3,Array('a','b','c'));
2724+
$redis->eval("return {1,2,3,redis.call('lrange','mylist',0,-1)}}");
2725+
~~~
2726+
2727+
### evalSha
2728+
-----
2729+
_**Description**_: Evaluate a LUA script serverside, from the SHA1 hash of the script instead of the script itself.
2730+
2731+
In order to run this command Redis will have to have already loaded the script,
2732+
either by running it or via the SCRIPT LOAD command.
2733+
2734+
##### *Parameters*
2735+
*script_sha* string. The sha1 encoded hash of the script you want to run.
2736+
*args* array, optional. Arguments to pass to the LUA script.
2737+
*num_keys* int, optional. The number of arguments that should go into the KEYS array, vs. the ARGV array when Redis spins the script
2738+
2739+
##### *Return value*
2740+
Mixed. See EVAL
2741+
2742+
##### *Examples*
2743+
~~~
2744+
$script = 'return 1';
2745+
$sha = $redis->script('load', $script);
2746+
$redis->evalSha($sha); // Returns 1
2747+
~~~
2748+
2749+
### script
2750+
-----
2751+
_**Description**_: Execute the Redis SCRIPT command to perform various operations on the scripting subsystem.
2752+
2753+
##### *Usage*
2754+
~~~
2755+
$redis->script('load', $script);
2756+
$redis->script('flush');
2757+
$redis->script('kill');
2758+
$redis->script('exists', $script1, [$script2, $script3, ...]);
2759+
~~~
2760+
2761+
##### *Return value*
2762+
* SCRIPT LOAD will return the SHA1 hash of the passed script on success, and FALSE on failure.
2763+
* SCRIPT FLUSH should always return TRUE
2764+
* SCRIPT KILL will return true if a script was able to be killed and false if not
2765+
* SCRIPT EXISTS will return an array with TRUE or FALSE for each passed script
2766+
2767+
### getLastError
2768+
-----
2769+
_**Description**_: The last error message (if any)
2770+
2771+
##### *Parameters*
2772+
*none*
2773+
2774+
##### *Return value*
2775+
A string with the last returned script based error message, or NULL if there is no error
2776+
2777+
##### *Examples*
2778+
~~~
2779+
$redis->eval('this-is-not-lua');
2780+
$err = $redis->getLastError();
2781+
// "ERR Error compiling script (new function): user_script:1: '=' expected near '-'"
2782+
~~~
2783+
2784+
### clearLastError
2785+
-----
2786+
_**Description**_: Clear the last error message
2787+
2788+
##### *Parameters*
2789+
*none*
2790+
2791+
##### *Return value*
2792+
*BOOL* TRUE
2793+
2794+
##### *Examples*
2795+
~~~
2796+
$redis->set('x', 'a');
2797+
$redis->incr('x');
2798+
$err = $redis->getLastError();
2799+
// "ERR value is not an integer or out of range"
2800+
$redis->clearLastError();
2801+
$err = $redis->getLastError();
2802+
// NULL
2803+
~~~
2804+
2805+
### _prefix
2806+
-----
2807+
_**Description**_: A utility method to prefix the value with the prefix setting for phpredis.
2808+
2809+
##### *Parameters*
2810+
*value* string. The value you wish to prefix
2811+
2812+
##### *Return value*
2813+
If a prefix is set up, the value now prefixed. If there is no prefix, the value will be returned unchanged.
2814+
2815+
##### *Examples*
2816+
~~~
2817+
$redis->setOption(Redis::OPT_PREFIX, 'my-prefix:');
2818+
$redis->_prefix('my-value'); // Will return 'my-prefix:my-value'
2819+
~~~
2820+
2821+
### _unserialize
2822+
-----
2823+
_**Description**_: A utility method to unserialize data with whatever serializer is set up.
2824+
2825+
If there is no serializer set, the value will be returned unchanged. If there is a serializer set up,
2826+
and the data passed in is malformed, an exception will be thrown. This can be useful if phpredis is
2827+
serializing values, and you return something from redis in a LUA script that is serialized.
2828+
2829+
##### *Parameters*
2830+
*value* string. The value to be unserialized
2831+
2832+
##### *Examples*
2833+
~~~
2834+
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);
2835+
$redis->_unserialize('a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}'); // Will return Array(1,2,3)
2836+
~~~

0 commit comments

Comments
 (0)
0