2771 | 2658 |
| @@ -2799,3 +2686,151 @@ $redis->zUnion('ko1', array('k1', 'k2')); /* 4, 'ko1' => array('val0', 'val1', '
|
2799 | 2686 | $redis->zUnion('ko2', array('k1', 'k2'), array(1, 1)); /* 4, 'ko2' => array('val0', 'val1', 'val2', 'val3') */
|
2800 | 2687 | $redis->zUnion('ko3', array('k1', 'k2'), array(5, 1)); /* 4, 'ko3' => array('val0', 'val2', 'val3', 'val1') */
|
2801 | 2688 | ~~~
|
| 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