时间:2021-07-01 10:21:17 帮助过:3人阅读
在 http://www.redis.net.cn/ 能找到所有关于redis的信息,包括安装、命令、在编程语言中的使用等等。这里就不讲如何安装redis了,因为在上面的网站中都能找到。下面直接讲redis是如何在php中使用的,这里我选择的是phpredis扩展。
执行phpinfo()函数,根据下面截图中的“NTS”和“VCn”选择对应的压缩包,https://github.com/phpredis/phpredis/downloads。另外注意,PHP版本也要对应好。
首先把压缩包中的 php_igbinary.dll和php_redis.dll 文件放到PHP安装目录的 ext 目录中
然后在 php.ini 添加如下配置
extension=php_igbinary.dll extension=php_redis.dll
$redis = new Redis(); //连接redis服务器 $redis->connect('127.0.0.1', '6379'); echo "Connection to server sucessfully <br/>"; //查看服务是否运行 echo "Server is running: " . $redis->ping();
结果如下,连接redis服务器成功
Connection to server sucessfully Server is running: +PONG
至此,我们可以在php中痛痛快快的使用redis了。
1 $redis = new Redis(); 2 //连接redis服务器 3 $redis->connect('127.0.0.1', '6379'); 4 5 6 $key = "key"; 7 $val = "val"; 8 9 //redis key操作 10 $redis->exists($key); //判断key值是否存在 11 $redis->expire($key, 10); //设置key在10秒后过期 12 13 //redis string 字符串 14 $redis->set($key, $val); 15 $redis->incr($key); //key值+1,除非val是整数,否则函数执行失败 16 $redis->decr($key); //key值-1,同上 17 $redis->append($key, "ue"); //追加key值内容 18 $redis->strlen($key); //返回key值的长度 19 20 //当第一次设置key值后,key值的数据类型就不能改变了。 21 $redis->del($key); //删除key值 22 23 //redis hash 哈希 24 $redis->hset($key, 'field1', 'val1'); //设置一个key-value键值对 25 $redis->hmset($key, array('field2'=>'val2', 'field3'=>'val3')); //设置多个k-v键值对26 $redis->hget($key, 'field2'); //获取hash其中的一个键值 27 $redis->hmget($key, array('field2', 'field1')); //获取hash的多个键值 28 $redis->hgetall($key); //获取hash中所有的键值对 29 $redis->hlen($key); //获取hash中键值对的个数 30 $redis->hkeys($key); //获取hash中所有的键 31 $redis->hvals($key); //获取hash中所有的值 32 $redis->del($key); //删除key值 33 34 //redis list 列表 35 $index = $start = 0; 36 $redis->lpush($key, 'val1', 'val2'); //在list的开头添加多个值 37 $redis->lpop($key); //移除并获取list的第一个元素 38 $redis->rpop($key); //移除并获取list的最后一个元素 39 $stop = $redis->llen($key) - 1; //获取list的长度 40 $redis->lindex($key, $index); //通过索引获取list元素 41 $redis->lrange($key, $start, $stop); //获取指定范围内的元素 42 43 $redis->del($key); 44 45 //redis set 无序集合 46 $redis->sadd($key, 'val1', 'val2'); //向集合中添加多个元素 47 $redis->scard($key); //获取集合元素个数 48 $redis->spop($key); //移除并获取集合内随机一个元素 49 $redis->srem($key, 'val1', 'val2'); //移除集合的多个元素 50 $redis->sismember($key, 'val1'); //判断元素是否存在于集合内 51 52 $redis->del($key); 53 //redis sorted set 有序集合 54 //有序集合里的元素都和一个分数score关联,就靠这个分数score对元素进行排序 55 $redis->zadd($key, $score1, $val1, $score2, $val2); //向集合内添加多个元素 56 $redis->zcard($key); //获取集合内元素总数 57 $redis->zcount($key, $minScore, $maxScore); //获取集合内分类范围内的元素 58 $redis->zrem($key, $member1, $member2); //移除集合内多个元素
附:Redis类的源码
<?php /** * Helper autocomplete for php redis extension * @author Max Kamashev <max.kamashev@gmail.com> * @link https://github.com/ukko/phpredis-phpdoc * * @method echo string $string Sends a string to Redis, which replies with the same string * * @method eval( $script, $args = array(), $numKeys = 0 ) * Evaluate a LUA script serverside * @param string $script * @param array $args * @param int $numKeys * @return Mixed. What is returned depends on what the LUA script itself returns, which could be a scalar value * (int/string), or an array. 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 executing the LUA script, the getLastError() function can tell you the * message that came back from Redis (e.g. compile error). * @link http://redis.io/commands/eval * @example * <pre> * $redis->eval("return 1"); // Returns an integer: 1 * $redis->eval("return {1,2,3}"); // Returns Array(1,2,3) * $redis->del('mylist'); * $redis->rpush('mylist','a'); * $redis->rpush('mylist','b'); * $redis->rpush('mylist','c'); * // Nested response: Array(1,2,3,Array('a','b','c')); * $redis->eval("return {1,2,3,redis.call('lrange','mylist',0,-1)}}"); * </pre> * */ class Redis { const AFTER = 'after'; const BEFORE = 'before'; /** * Options */ const OPT_SERIALIZER = 1; const OPT_PREFIX = 2; const OPT_READ_TIMEOUT = 3; const OPT_SCAN = 4; /** * Serializers */ const SERIALIZER_NONE = 0; const SERIALIZER_PHP = 1; const SERIALIZER_IGBINARY = 2; /** * Multi */ const ATOMIC = 0; const MULTI = 1; const PIPELINE = 2; /** * Type */ const REDIS_NOT_FOUND = 0; const REDIS_STRING = 1; const REDIS_SET = 2; const REDIS_LIST = 3; const REDIS_ZSET = 4; const REDIS_HASH = 5; /** * Scan */ const SCAN_NORETRY = 0; const SCAN_RETRY = 1; /** * Creates a Redis client * * @example $redis = new Redis(); */ public function __construct( ) {} /** * Connects to a Redis instance. * * @param string $host can be a host, or the path to a unix domain socket * @param int $port optional * @param float $timeout value in seconds (optional, default is 0.0 meaning unlimited) * @return bool TRUE on success, FALSE on error. * <pre> * $redis->connect('127.0.0.1', 6379); * $redis->connect('127.0.0.1'); // port 6379 by default * $redis->connect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout. * $redis->connect('/tmp/redis.sock'); // unix domain socket. * </pre> */ public function connect( $host, $port = 6379, $timeout = 0.0 ) {} /** * Set the string value in argument as value of the key, with a time to live. * * @param string $key * @param int $ttl in milliseconds * @param string $value * @return bool: TRUE if the command is successful. * @link http://redis.io/commands/setex * $redis->psetex('key', 100, 'value'); // sets key → value, with 0.1 sec TTL. */ public function psetex($key, $ttl, $value) {} /** * Scan a set for members. * * @see scan() * @param string $key * @param int $iterator * @param string $pattern * @param int $count * @return array|bool */ public function sScan($key, $iterator, $pattern = '', $count = 0) {} /** * Scan the keyspace for keys. * * @param int $iterator * @param string $pattern * @param int $count How many keys to return in a go (only a sugestion to Redis) * @return array|bool an array of keys or FALSE if there are no more keys * @link http://redis.io/commands/scan * <pre> * $it = NULL; // Initialize our iterator to NULL * $redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY); // retry when we get no keys back * while($arr_keys = $redis->scan($it)) { * foreach($arr_keys as $str_key) { * echo "Here is a key: $str_key\n"; * } * echo "No more keys to scan!\n"; * } * </pre> */ public function scan($iterator, $pattern = '', $count = 0) {} /** * Scan a sorted set for members, with optional pattern and count. * * @see scan() * @param string $key * @param int $iterator * @param string $pattern * @param int $count * @return array|bool */ public function zScan($key, $iterator, $pattern = '', $count = 0) {} /** * Scan a HASH value for members, with an optional pattern and count. * * @see scan() * @param string $key * @param int $iterator * @param string $pattern * @param int $count * @return array */ public function hScan($key, $iterator, $pattern = '', $count = 0) {} /** * Issue the CLIENT command with various arguments. * @param string $command list | getname | setname | kill * @param string $arg * @return mixed * @link http://redis.io/commands/client-list * @link http://redis.io/commands/client-getname * @link http://redis.io/commands/client-setname * @link http://redis.io/commands/client-kill * <pre> * $redis->client('list'); * $redis->client('getname'); * $redis->client('setname', 'somename'); * $redis->client('kill', <ip:port>); * </pre> * * * CLIENT LIST will return an array of arrays with client information. * CLIENT GETNAME will return the client name or false if none has been set * CLIENT SETNAME will return true if it can be set and false if not * CLIENT KILL will return true if the client can be killed, and false if not */ public function client($command, $arg = '') {} /** * Access the Redis slow log. * * @param string $command get | len | reset * @return mixed * @link http://redis.io/commands/slowlog * <pre> * // Get ten slowlog entries * $redis->slowlog('get', 10); * * // Get the default number of slowlog entries * $redis->slowlog('get'); * * // Reset our slowlog * $redis->slowlog('reset'); * * // Retrieve slowlog length * $redis->slowlog('len'); * </pre> */ public function slowlog($command) {} /** * @see connect() * @param string $host * @param int $port * @param float $timeout */ public function open( $host, $port = 6379, $timeout = 0.0 ) {} /** * Connects to a Redis instance or reuse a connection already established with pconnect/popen. * * The connection will not be closed on close or end of request until the php process ends. * So be patient on to many open FD's (specially on redis server side) when using persistent connections on * many servers connecting to one redis server. * * Also more than one persistent connection can be made identified by either host + port + timeout * or unix socket + timeout. * * This feature is not available in threaded versions. pconnect and popen then working like their non persistent * equivalents. * * @param string $host can be a host, or the path to a unix domain socket * @param int $port optional * @param float $timeout value in seconds (optional, default is 0 meaning unlimited) * @return bool TRUE on success, FALSE on ertcnror. * <pre> * $redis->connect('127.0.0.1', 6379); * $redis->connect('127.0.0.1'); // port 6379 by default * $redis->connect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout. * $redis->connect('/tmp/redis.sock'); // unix domain socket. * </pre> */ public function pconnect( $host, $port = 6379, $timeout = 0.0 ) {} /** * @see pconnect() * @param string $host * @param int $port * @param float $timeout */ public function popen( $host, $port = 6379, $timeout = 0.0 ) {} /** * Disconnects from the Redis instance, except when pconnect is used. */ public function close( ) {} /** * Set client option. * * @param string $name parameter name * @param string $value parameter value * @return bool: TRUE on success, FALSE on error. * @example * <pre> * $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // don't serialize data * $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // use built-in serialize/unserialize * $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // use igBinary serialize/unserialize * $redis->setOption(Redis::OPT_PREFIX, 'myAppName:'); // use custom prefix on all keys * </pre> */ public function setOption( $name, $value ) {} /** * Get client option * * @param string $name parameter name * @return int Parameter value. * @example * // return Redis::SERIALIZER_NONE, Redis::SERIALIZER_PHP, or Redis::SERIALIZER_IGBINARY. * $redis->getOption(Redis::OPT_SERIALIZER); */ public function getOption( $name ) {} /** * Check the current connection status * * @return string STRING: +PONG on success. Throws a RedisException object on connectivity error, as described above. * @link http://redis.io/commands/ping */ public function ping( ) {} /** * Get the value related to the specified key * * @param string $key * @return string|bool: If key didn't exist, FALSE is returned. Otherwise, the value related to this key is returned. * @link http://redis.io/commands/get * @example $redis->get('key'); */ public function get( $key ) {} /** * Set the string value in argument as value of the key. * * @param string $key * @param string $value * @param int $timeout [optional] Calling setex() is preferred if you want a timeout. * @return bool: TRUE if the command is successful. * @link http://redis.io/commands/set * @example $redis->set('key', 'value'); */ public function set( $key, $value, $timeout = 0 ) {} /** * Set the string value in argument as value of the key, with a time to live. * * @param string $key * @param int $ttl * @param string $value * @return bool: TRUE if the command is successful. * @link http://redis.io/commands/setex * @example $redis->setex('key', 3600, 'value'); // sets key → value, with 1h TTL. */ public function setex( $key, $ttl, $value ) {} /** * Set the string value in argument as value of the key if the key doesn't already exist in the database. * * @param string $key * @param string $value * @return bool: TRUE in case of success, FALSE in case of failure. * @link http://redis.io/commands/setnx * @example * <pre> * $redis->setnx('key', 'value'); // return TRUE * $redis->setnx('key', 'value'); // return FALSE * </pre> */ public function setnx( $key, $value ) {} /** * Remove specified keys. * * @param int|array $key1 An array of keys, or an undefined number of parameters, each a key: key1 key2 key3 ... keyN * @param string $key2 ... * @param string $key3 ... * @return int Number of keys deleted. * @link http://redis.io/commands/del * @example * <pre> * $redis->set('key1', 'val1'); * $redis->set('key2', 'val2'); * $redis->set('key3', 'val3'); * $redis->set('key4', 'val4'); * $redis->delete('key1', 'key2'); // return 2 * $redis->delete(array('key3', 'key4')); // return 2 * </pre> */ public function del( $key1, $key2 = null, $key3 = null ) {} /** * @see del() * @param $key1 * @param null $key2 * @param null $key3 */ public function delete( $key1, $key2 = null, $key3 = null ) {} /** * Enter and exit transactional mode. * * @internal param Redis::MULTI|Redis::PIPELINE * Defaults to Redis::MULTI. * A Redis::MULTI block of commands runs as a single transaction; * a Redis::PIPELINE block is simply transmitted faster to the server, but without any guarantee of atomicity. * discard cancels a transaction. * @return Redis returns the Redis instance and enters multi-mode. * Once in multi-mode, all subsequent method calls return the same object until exec() is called. * @link http://redis.io/commands/multi * @example * <pre> * $ret = $redis->multi() * ->set('key1', 'val1') * ->get('key1') * ->set('key2', 'val2') * ->get('key2') * ->exec(); * * //$ret == array ( * // 0 => TRUE, * // 1 => 'val1', * // 2 => TRUE, * // 3 => 'val2'); * </pre> */ public function multi( ) {} /** * @see multi() * @link http://redis.io/commands/exec */ public function exec( ) {} /** * @see multi() * @link http://redis.io/commands/discard */ public function discard( ) {} /** * Watches a key for modifications by another client. If the key is modified between WATCH and EXEC, * the MULTI/EXEC transaction will fail (return FALSE). unwatch cancels all the watching of all keys by this client. * @param string | array $key: a list of keys * @return void * @link http://redis.io/commands/watch * @example * <pre> * $redis->watch('x'); * // long code here during the execution of which other clients could well modify `x` * $ret = $redis->multi() * ->incr('x') * ->exec(); * // $ret = FALSE if x has been modified between the call to WATCH and the call to EXEC. * </pre> */ public function watch( $key ) {} /** * @see watch() * @link http://redis.io/commands/unwatch */ public function unwatch( ) {} /** * Subscribe to channels. Warning: this function will probably change in the future. * * @param array $channels an array of channels to subscribe to * @param string | array $callback either a string or an array($instance, 'method_name'). * The callback function receives 3 parameters: the redis instance, the channel name, and the message. * @link http://redis.io/commands/subscribe * @example * <pre> * function f($redis, $chan, $msg) { * switch($chan) { * case 'chan-1': * ... * break; * * case 'chan-2': * ... * break; * * case 'chan-2': * ... * break; * } * } * * $redis->subscribe(array('chan-1', 'chan-2', 'chan-3'), 'f'); // subscribe to 3 chans * </pre> */ public function subscribe( $channels, $callback ) {} /** * Subscribe to channels by pattern * * @param array $patterns The number of elements removed from the set. * @param string|array $callback Either a string or an array with an object and method. * The callback will get four arguments ($redis, $pattern, $channel, $message) * @link http://redis.io/commands/psubscribe * @example * <pre> * function psubscribe($redis, $pattern, $chan, $msg) { * echo "Pattern: $pattern\n"; * echo "Channel: $chan\n"; * echo "Payload: $msg\n"; * } * </pre> */ public function psubscribe( $patterns, $callback ) {} /** * Publish messages to channels. Warning: this function will probably change in the future. * * @param string $channel a channel to publish to * @param string $message string * @link http://redis.io/commands/publish * @return int Number of clients that received the message * @example $redis->publish('chan-1', 'hello, world!'); // send message. */ public function publish( $channel, $message ) {} /** * Verify if the specified key exists. * * @param string $key * @return bool: If the key exists, return TRUE, otherwise return FALSE. * @link http://redis.io/commands/exists * @example * <pre> * $redis->set('key', 'value'); * $redis->exists('key'); // TRUE * $redis->exists('NonExistingKey'); // FALSE * </pre> */ public function exists( $key ) {} /** * Increment the number stored at key by one. * * @param string $key * @return int the new value * @link http://redis.io/commands/incr * @example * <pre> * $redis->incr('key1'); // key1 didn't exists, set to 0 before the increment and now has the value 1 * $redis->incr('key1'); // 2 * $redis->incr('key1'); // 3 * $redis->incr('key1'); // 4 * </pre> */ public function incr( $key ) {} /** * Increment the float value of a key by the given amount * * @param string $key * @param float $increment * @return float * @link http://redis.io/commands/incrbyfloat * @example * <pre> * $redis = new Redis(); * $redis->connect('127.0.0.1'); * $redis->set('x', 3); * var_dump( $redis->incrByFloat('x', 1.5) ); // float(4.5) * * // ! SIC * var_dump( $redis->get('x') ); // string(3) "4.5" * </pre> */ public function incrByFloat( $key, $increment ) {} /** * Increment the number stored at key by one. If the second argument is filled, it will be used as the integer * value of the increment. * * @param string $key key * @param int $value value that will be added to key (only for incrBy) * @return int the new value * @link http://redis.io/commands/incrby * @example * <pre> * $redis->incr('key1'); // key1 didn't exists, set to 0 before the increment and now has the value 1 * $redis->incr('key1'); // 2 * $redis->incr('key1'); // 3 * $redis->incr('key1'); // 4 * $redis->incrBy('key1', 10); // 14 * </pre> */ public function incrBy( $key, $value ) {} /** * Decrement the number stored at key by one. * * @param string $key * @return int the new value * @link http://redis.io/commands/decr * @example * <pre> * $redis->decr('key1'); // key1 didn't exists, set to 0 before the increment and now has the value -1 * $redis->decr('key1'); // -2 * $redis->decr('key1'); // -3 * </pre> */ public function decr( $key ) {} /** * Decrement the number stored at key by one. If the second argument is filled, it will be used as the integer * value of the decrement. * * @param string $key * @param int $value that will be substracted to key (only for decrBy) * @return int the new value * @link http://redis.io/commands/decrby * @example * <pre> * $redis->decr('key1'); // key1 didn't exists, set to 0 before the increment and now has the value -1 * $redis->decr('key1'); // -2 * $redis->decr('key1'); // -3 * $redis->decrBy('key1', 10); // -13 * </pre> */ public function decrBy( $key, $value ) {} /** * Get the values of all the specified keys. If one or more keys dont exist, the array will contain FALSE at the * position of the key. * * @param array $keys Array containing the list of the keys * @return array Array containing the values related to keys in argument * @example * <pre> * $redis->set('key1', 'value1'); * $redis->set('key2', 'value2'); * $redis->set('key3', 'value3'); * $redis->getMultiple(array('key1', 'key2', 'key3')); // array('value1', 'value2', 'value3'); * $redis->getMultiple(array('key0', 'key1', 'key5')); // array(`FALSE`, 'value2', `FALSE`); * </pre> */ public function getMultiple( array $keys ) {} /** * Adds the string values to the head (left) of the list. Creates the list if the key didn't exist. * If the key exists and is not a list, FALSE is returned. * * @param string $key * @param string $value1 String, value to push in key * @param string $value2 Optional * @param string $valueN Optional * @return int The new length of the list in case of success, FALSE in case of Failure. * @link http://redis.io/commands/lpush * @example * <pre> * $redis->lPush('l', 'v1', 'v2', 'v3', 'v4') // int(4) * var_dump( $redis->lRange('l', 0, -1) ); * //// Output: * // array(4) { * // [0]=> string(2) "v4" * // [1]=> string(2) "v3" * // [2]=> string(2) "v2" * // [3]=> string(2) "v1" * // } * </pre> */ public function lPush( $key, $value1, $value2 = null, $valueN = null ) {} /** * Adds the string values to the tail (right) of the list. Creates the list if the key didn't exist. * If the key exists and is not a list, FALSE is returned. * * @param string $key * @param string $value1 String, value to push in key * @param string $value2 Optional * @param string $valueN Optional * @return int The new length of the list in case of success, FALSE in case of Failure. * @link http://redis.io/commands/rpush * @example * <pre> * $redis->rPush('l', 'v1', 'v2', 'v3', 'v4'); // int(4) * var_dump( $redis->lRange('l', 0, -1) ); * //// Output: * // array(4) { * // [0]=> string(2) "v1" * // [1]=> string(2) "v2" * // [2]=> string(2) "v3" * // [3]=> string(2) "v4" * // } * </pre> */ public function rPush( $key, $value1, $value2 = null, $valueN = null ) {} /** * Adds the string value to the head (left) of the list if the list exists. * * @param string $key * @param string $value String, value to push in key * @return int The new length of the list in case of success, FALSE in case of Failure. * @link http://redis.io/commands/lpushx * @example * <pre> * $redis->delete('key1'); * $redis->lPushx('key1', 'A'); // returns 0 * $redis->lPush('key1', 'A'); // returns 1 * $redis->lPushx('key1', 'B'); // returns 2 * $redis->lPushx('key1', 'C'); // returns 3 * // key1 now points to the following list: [ 'A', 'B', 'C' ] * </pre> */ public function lPushx( $key, $value ) {} /** * Adds the string value to the tail (right) of the list if the ist exists. FALSE in case of Failure. * * @param string $key * @param string $value String, value to push in key * @return int The new length of the list in case of success, FALSE in case of Failure. * @link http://redis.io/commands/rpushx * @example * <pre> * $redis->delete('key1'); * $redis->rPushx('key1', 'A'); // returns 0 * $redis->rPush('key1', 'A'); // returns 1 * $redis->rPushx('key1', 'B'); // returns 2 * $redis->rPushx('key1', 'C'); // returns 3 * // key1 now points to the following list: [ 'A', 'B', 'C' ] * </pre> */ public function rPushx( $key, $value ) {} /** * Returns and removes the first element of the list. * * @param string $key * @return string if command executed successfully BOOL FALSE in case of failure (empty list) * @link http://redis.io/commands/lpop * @example * <pre> * $redis->rPush('key1', 'A'); * $redis->rPush('key1', 'B'); * $redis->rPush('key1', 'C'); // key1 => [ 'A', 'B', 'C' ] * $redis->lPop('key1'); // key1 => [ 'B', 'C' ] * </pre> */ public function lPop( $key ) {} /** * Returns and removes the last element of the list. * * @param string $key * @return string if command executed successfully BOOL FALSE in case of failure (empty list) * @link http://redis.io/commands/rpop * @example * <pre> * $redis->rPush('key1', 'A'); * $redis->rPush('key1', 'B'); * $redis->rPush('key1', 'C'); // key1 => [ 'A', 'B', 'C' ] * $redis->rPop('key1'); // key1 => [ 'A', 'B' ] * </pre> */ public function rPop( $key ) {} /** * Is a blocking lPop primitive. If at least one of the lists contains at least one element, * the element will be popped from the head of the list and returned to the caller. * Il all the list identified by the keys passed in arguments are empty, blPop will block * during the specified timeout until an element is pushed to one of those lists. This element will be popped. * * @param array $keys Array containing the keys of the lists INTEGER Timeout * Or STRING Key1 STRING Key2 STRING Key3 ... STRING Keyn INTEGER Timeout * @return array array('listName', 'element') * @link http://redis.io/commands/blpop * @example * <pre> * // Non blocking feature * $redis->lPush('key1', 'A'); * $redis->delete('key2'); * * $redis->blPop('key1', 'key2', 10); // array('key1', 'A') * // OR * $redis->blPop(array('key1', 'key2'), 10); // array('key1', 'A') * * $redis->brPop('key1', 'key2', 10); // array('key1', 'A') * // OR * $redis->brPop(array('key1', 'key2'), 10); // array('key1', 'A') * * // Blocking feature * * // process 1 * $redis->delete('key1'); * $redis->blPop('key1', 10); * // blocking for 10 seconds * * // process 2 * $redis->lPush('key1', 'A'); * * // process 1 * // array('key1', 'A') is returned * </pre> */ public function blPop( array $keys ) {} /** * Is a blocking rPop primitive. If at least one of the lists contains at least one element, * the element will be popped from the head of the list and returned to the caller. * Il all the list identified by the keys passed in arguments are empty, brPop will * block during the specified timeout until an element is pushed to one of those lists. T * his element will be popped. * * @param array $keys Array containing the keys of the lists INTEGER Timeout * Or STRING Key1 STRING Key2 STRING Key3 ... STRING Keyn INTEGER Timeout * @return array array('listName', 'element') * @link http://redis.io/commands/brpop * @example * <pre> * // Non blocking feature * $redis->lPush('key1', 'A'); * $redis->delete('key2'); * * $redis->blPop('key1', 'key2', 10); // array('key1', 'A') * // OR * $redis->blPop(array('key1', 'key2'), 10); // array('key1', 'A') * * $redis->brPop('key1', 'key2', 10); // array('key1', 'A') * // OR * $redis->brPop(array('key1', 'key2'), 10); // array('key1', 'A') * * // Blocking feature * * // process 1 * $redis->delete('key1'); * $redis->blPop('key1', 10); * // blocking for 10 seconds * * // process 2 * $redis->lPush('key1', 'A'); * * // process 1 * // array('key1', 'A') is returned * </pre> */ public function brPop( array $keys ) {} /** * Returns the size of a list identified by Key. If the list didn't exist or is empty, * the command returns 0. If the data type identified by Key is not a list, the command return FALSE. * * @param string $key * @return int The size of the list identified by Key exists. * bool FALSE if the data type identified by Key is not list * @link http://redis.io/commands/llen * @example * <pre> * $redis->rPush('key1', 'A'); * $redis->rPush('key1', 'B'); * $redis->rPush('key1', 'C'); // key1 => [ 'A', 'B', 'C' ] * $redis->lLen('key1'); // 3 * $redis->rPop('key1'); * $redis->lLen('key1'); // 2 * </pre> */ public function lLen( $key ) {} /** * @see lLen() * @param string $key * @param int $index * @link http://redis.io/commands/llen */ public function lSize( $key ) {} /** * Return the specified element of the list stored at the specified key. * 0 the first element, 1 the second ... -1 the last element, -2 the penultimate ... * Return FALSE in case of a bad index or a key that doesn't point to a list. * @param string $key * @param int $index * @return String the element at this index * Bool FALSE if the key identifies a non-string data type, or no value corresponds to this index in the list Key. * @link http://redis.io/commands/lindex * @example * <pre> * $redis->rPush('key1', 'A'); * $redis->rPush('key1', 'B'); * $redis->rPush('key1', 'C'); // key1 => [ 'A', 'B', 'C' ] * $redis->lGet('key1', 0); // 'A' * $redis->lGet('key1', -1); // 'C' * $redis->lGet('key1', 10); // `FALSE` * </pre> */ public function lIndex( $key, $index ) {} /** * @see lIndex() * @param string $key * @param int $index * @link http://redis.io/commands/lindex */ public function lGet( $key, $index ) {} /** * Set the list at index with the new value. * * @param string $key * @param int $index * @param string $value * @return BOOL TRUE if the new value is setted. FALSE if the index is out of range, or data type identified by key * is not a list. * @link http://redis.io/commands/lset * @example * <pre> * $redis->rPush('key1', 'A'); * $redis->rPush('key1', 'B'); * $redis->rPush('key1', 'C'); // key1 => [ 'A', 'B', 'C' ] * $redis->lGet('key1', 0); // 'A' * $redis->lSet('key1', 0, 'X'); * $redis->lGet('key1', 0); // 'X' * </pre> */ public function lSet( $key, $index, $value ) {} /** * Returns the specified elements of the list stored at the specified key in * the range [start, end]. start and stop are interpretated as indices: 0 the first element, * 1 the second ... -1 the last element, -2 the penultimate ... * @param string $key * @param int $start * @param int $end * @return array containing the values in specified range. * @link http://redis.io/commands/lrange * @example * <pre> * $redis->rPush('key1', 'A'); * $redis->rPush('key1', 'B'); * $redis->rPush('key1', 'C'); * $redis->lRange('key1', 0, -1); // array('A', 'B', 'C') * </pre> */ public function lRange( $key, $start, $end ) {} /** * @see lRange() * @link http://redis.io/commands/lrange * @param string $key * @param int $start * @param int $end */ public function lGetRange( $key, $start, $end ) {} /** * Trims an existing list so that it will contain only a specified range of elements. * * @param string $key * @param int $start * @param int $stop * @return array Bool return FALSE if the key identify a non-list value. * @link http://redis.io/commands/ltrim * @example * <pre> * $redis->rPush('key1', 'A'); * $redis->rPush('key1', 'B'); * $redis->rPush('key1', 'C'); * $redis->lRange('key1', 0, -1); // array('A', 'B', 'C') * $redis->lTrim('key1', 0, 1); * $redis->lRange('key1', 0, -1); // array('A', 'B') * </pre> */ public function lTrim( $key, $start, $stop ) {} /** * @see lTrim() * @link http://redis.io/commands/ltrim * @param string $key * @param int $start * @param int $stop */ public function listTrim( $key, $start, $stop ) {} /** * Removes the first count occurences of the value element from the list. * If count is zero, all the matching elements are removed. If count is negative, * elements are removed from tail to head. * * @param string $key * @param string $value * @param int $count * @return int the number of elements to remove * bool FALSE if the value identified by key is not a list. * @link http://redis.io/commands/lrem * @example * <pre> * $redis->lPush('key1', 'A'); * $redis->lPush('key1', 'B'); * $redis->lPush('key1', 'C'); * $redis->lPush('key1', 'A'); * $redis->lPush('key1', 'A'); * * $redis->lRange('key1', 0, -1); // array('A', 'A', 'C', 'B', 'A') * $redis->lRem('key1', 'A', 2); // 2 * $redis->lRange('key1', 0, -1); // array('C', 'B', 'A') * </pre> */ public function lRem( $key, $value, $count ) {} /** * @see lRem * @link http://redis.io/commands/lremove * @param string $key * @param string $value * @param int $count */ public function lRemove( $key, $value, $count ) {} /** * Insert value in the list before or after the pivot value. the parameter options * specify the position of the insert (before or after). If the list didn't exists, * or the pivot didn't exists, the value is not inserted. * * @param string $key * @param int $position Redis::BEFORE | Redis::AFTER * @param string $pivot * @param string $value * @return int The number of the elements in the list, -1 if the pivot didn't exists. * @link http://redis.io/commands/linsert * @example * <pre> * $redis->delete('key1'); * $redis->lInsert('key1', Redis::AFTER, 'A', 'X'); // 0 * * $redis->lPush('key1', 'A'); * $redis->lPush('key1', 'B'); * $redis->lPush('key1', 'C'); * * $redis->lInsert('key1', Redis::BEFORE, 'C', 'X'); // 4 * $redis->lRange('key1', 0, -1); // array('A', 'B', 'X', 'C') * * $redis->lInsert('key1', Redis::AFTER, 'C', 'Y'); // 5 * $redis->lRange('key1', 0, -1); // array('A', 'B', 'X', 'C', 'Y') * * $redis->lInsert('key1', Redis::AFTER, 'W', 'value'); // -1 * </pre> */ public function lInsert( $key, $position, $pivot, $value ) {} /** * Adds a values to the set value stored at key. * If this value is already in the set, FALSE is returned. * * @param string $key Required key * @param string $value1 Required value * @param string $value2 Optional value * @param string $valueN Optional value * @return int The number of elements added to the set * @link http://redis.io/commands/sadd * @example * <pre> * $redis->sAdd('k', 'v1'); // int(1) * $redis->sAdd('k', 'v1', 'v2', 'v3'); // int(2) * </pre> */ public function sAdd( $key, $value1, $value2 = null, $valueN = null ) {} /** * Removes the specified members from the set value stored at key. * * @param string $key * @param string $member1 * @param string $member2 * @param string $memberN * @return int The number of elements removed from the set. * @link http://redis.io/commands/srem * @example * <pre> * var_dump( $redis->sAdd('k', 'v1', 'v2', 'v3') ); // int(3) * var_dump( $redis->sRem('k', 'v2', 'v3') ); // int(2) * var_dump( $redis->sMembers('k') ); * //// Output: * // array(1) { * // [0]=> string(2) "v1" * // } * </pre> */ public function sRem( $key, $member1, $member2 = null, $memberN = null ) {} /** * @see sRem() * @link http://redis.io/commands/srem * @param string $key * @param string $member1 * @param string $member2 * @param string $memberN */ public function sRemove( $key, $member1, $member2 = null, $memberN = null ) {} /** * Moves the specified member from the set at srcKey to the set at dstKey. * * @param string $srcKey * @param string $dstKey * @param string $member * @return bool If the operation is successful, return TRUE. * If the srcKey and/or dstKey didn't exist, and/or the member didn't exist in srcKey, FALSE is returned. * @link http://redis.io/commands/smove * @example * <pre> * $redis->sAdd('key1' , 'set11'); * $redis->sAdd('key1' , 'set12'); * $redis->sAdd('key1' , 'set13'); // 'key1' => {'set11', 'set12', 'set13'} * $redis->sAdd('key2' , 'set21'); * $redis->sAdd('key2' , 'set22'); // 'key2' => {'set21', 'set22'} * $redis->sMove('key1', 'key2', 'set13'); // 'key1' => {'set11', 'set12'} * // 'key2' => {'set21', 'set22', 'set13'} * </pre> */ public function sMove( $srcKey, $dstKey, $member ) {} /** * Checks if value is a member of the set stored at the key key. * * @param string $key * @param string $value * @return bool TRUE if value is a member of the set at key key, FALSE otherwise. * @link http://redis.io/commands/sismember * @example * <pre> * $redis->sAdd('key1' , 'set1'); * $redis->sAdd('key1' , 'set2'); * $redis->sAdd('key1' , 'set3'); // 'key1' => {'set1', 'set2', 'set3'} * * $redis->sIsMember('key1', 'set1'); // TRUE * $redis->sIsMember('key1', 'setX'); // FALSE * </pre> */ public function sIsMember( $key, $value ) {} /** * @see sIsMember() * @link http://redis.io/commands/sismember * @param string $key * @param string $value */ public function sContains( $key, $value ) {} /** * Returns the cardinality of the set identified by key. * * @param string $key * @return int the cardinality of the set identified by key, 0 if the set doesn't exist. * @link http://redis.io/commands/scard * @example * <pre> * $redis->sAdd('key1' , 'set1'); * $redis->sAdd('key1' , 'set2'); * $redis->sAdd('key1' , 'set3'); // 'key1' => {'set1', 'set2', 'set3'} * $redis->sCard('key1'); // 3 * $redis->sCard('keyX'); // 0 * </pre> */ public function sCard( $key ) {} /** * Removes and returns a random element from the set value at Key. * * @param string $key * @return string "popped" value * bool FALSE if set identified by key is empty or doesn't exist. * @link http://redis.io/commands/spop * @example * <pre> * $redis->sAdd('key1' , 'set1'); * $redis->sAdd('key1' , 'set2'); * $redis->sAdd('key1' , 'set3'); // 'key1' => {'set3', 'set1', 'set2'} * $redis->sPop('key1'); // 'set1', 'key1' => {'set3', 'set2'} * $redis->sPop('key1'); // 'set3', 'key1' => {'set2'} * </pre> */ public function sPop( $key ) {} /** * Returns a random element from the set value at Key, without removing it. * * @param string $key * @return string value from the set * bool FALSE if set identified by key is empty or doesn't exist. * @link http://redis.io/commands/srandmember * @example * <pre> * $redis->sAdd('key1' , 'set1'); * $redis->sAdd('key1' , 'set2'); * $redis->sAdd('key1' , 'set3'); // 'key1' => {'set3', 'set1', 'set2'} * $redis->sRandMember('key1'); // 'set1', 'key1' => {'set3', 'set1', 'set2'} * $redis->sRandMember('key1'); // 'set3', 'key1' => {'set3', 'set1', 'set2'} * </pre> */ public function sRandMember( $key ) {} /** * Returns the members of a set resulting from the intersection of all the sets * held at the specified keys. If just a single key is specified, then this command * produces the members of this set. If one of the keys is missing, FALSE is returned. * * @param string $key1 keys identifying the different sets on which we will apply the intersection. * @param string $key2 ... * @param string $keyN ... * @return array, contain the result of the intersection between those keys. * If the intersection between the different sets is empty, the return value will be empty array. * @link http://redis.io/commands/sinterstore * @example * <pre> * $redis->sAdd('key1', 'val1'); * $redis->sAdd('key1', 'val2'); * $redis->sAdd('key1', 'val3'); * $redis->sAdd('key1', 'val4'); * * $redis->sAdd('key2', 'val3'); * $redis->sAdd('key2', 'val4'); * * $redis->sAdd('key3', 'val3'); * $redis->sAdd('key3', 'val4'); * * var_dump($redis->sInter('key1', 'key2', 'key3')); * * //array(2) { * // [0]=> * // string(4) "val4" * // [1]=> * // string(4) "val3" * //} * </pre> */ public function sInter( $key1, $key2, $keyN = null ) {} /** * Performs a sInter command and stores the result in a new set. * * @param string $dstKey the key to store the diff into. * @param string $key1 are intersected as in sInter. * @param string $key2 ... * @param string $keyN ... * @return int: The cardinality of the resulting set, or FALSE in case of a missing key. * @link http://redis.io/commands/sinterstore * @example * <pre> * $redis->sAdd('key1', 'val1'); * $redis->sAdd('key1', 'val2'); * $redis->sAdd('key1', 'val3'); * $redis->sAdd('key1', 'val4'); * * $redis->sAdd('key2', 'val3'); * $redis->sAdd('key2', 'val4'); * * $redis->sAdd('key3', 'val3'); * $redis->sAdd('key3', 'val4'); * * var_dump($redis->sInterStore('output', 'key1', 'key2', 'key3')); * var_dump($redis->sMembers('output')); * * //int(2) * // * //array(2) { * // [0]=> * // string(4) "val4" * // [1]=> * // string(4) "val3" * //} * </pre> */ public function sInterStore( $dstKey, $key1, $key2, $keyN = null ) {} /** * Performs the union between N sets and returns it. * * @param string $key1 Any number of keys corresponding to sets in redis. * @param string $key2 ... * @param string $keyN ... * @return array of strings: The union of all these sets. * @link http://redis.io/commands/sunionstore * @example * <pre> * $redis->delete('s0', 's1', 's2'); * * $redis->sAdd('s0', '1'); * $redis->sAdd('s0', '2'); * $redis->sAdd('s1', '3'); * $redis->sAdd('s1', '1'); * $redis->sAdd('s2', '3'); * $redis->sAdd('s2', '4'); * * var_dump($redis->sUnion('s0', 's1', 's2')); * * array(4) { * // [0]=> * // string(1) "3" * // [1]=> * // string(1) "4" * // [2]=> * // string(1) "1" * // [3]=> * // string(1) "2" * //} * </pre> */ public function sUnion( $key1, $key2, $keyN = null ) {} /** * Performs the same action as sUnion, but stores the result in the first key * * @param string $dstKey the key to store the diff into. * @param string $key1 Any number of keys corresponding to sets in redis. * @param string $key2 ... * @param string $keyN ... * @return int Any number of keys corresponding to sets in redis. * @link http://redis.io/commands/sunionstore * @example * <pre> * $redis->delete('s0', 's1', 's2'); * * $redis->sAdd('s0', '1'); * $redis->sAdd('s0', '2'); * $redis->sAdd('s1', '3'); * $redis->sAdd('s1', '1'); * $redis->sAdd('s2', '3'); * $redis->sAdd('s2', '4'); * * var_dump($redis->sUnionStore('dst', 's0', 's1', 's2')); * var_dump($redis->sMembers('dst')); * * //int(4) * //array(4) { * // [0]=> * // string(1) "3" * // [1]=> * // string(1) "4" * // [2]=> * // string(1) "1" * // [3]=> * // string(1) "2" * //} * </pre> */ public function sUnionStore( $dstKey, $key1, $key2, $keyN = null ) {} /** * Performs the difference between N sets and returns it. * * @param string $key1 Any number of keys corresponding to sets in redis. * @param string $key2 ... * @param string $keyN ... * @return array of strings: The difference of the first set will all the others. * @link http://redis.io/commands/sdiff * @example * <pre> * $redis->delete('s0', 's1', 's2'); * * $redis->sAdd('s0', '1'); * $redis->sAdd('s0', '2'); * $redis->sAdd('s0', '3'); * $redis->sAdd('s0', '4'); * * $redis->sAdd('s1', '1'); * $redis->sAdd('s2', '3'); * * var_dump($redis->sDiff('s0', 's1', 's2')); * * //array(2) { * // [0]=> * // string(1) "4" * // [1]=> * // string(1) "2" * //} * </pre> */ public function sDiff( $key1, $key2, $keyN = null ) {} /** * Performs the same action as sDiff, but stores the result in the first key * * @param string $dstKey the key to store the diff into. * @param string $key1 Any number of keys corresponding to sets in redis * @param string $key2 ... * @param string $keyN ... * @return int: The cardinality of the resulting set, or FALSE in case of a missing key. * @link http://redis.io/commands/sdiffstore * @example * <pre> * $redis->delete('s0', 's1', 's2'); * * $redis->sAdd('s0', '1'); * $redis->sAdd('s0', '2'); * $redis->sAdd('s0', '3'); * $redis->sAdd('s0', '4'); * * $redis->sAdd('s1', '1'); * $redis->sAdd('s2', '3'); * * var_dump($redis->sDiffStore('dst', 's0', 's1', 's2')); * var_dump($redis->sMembers('dst')); * * //int(2) * //array(2) { * // [0]=> * // string(1) "4" * // [1]=> * // string(1) "2" * //} * </pre> */ public function sDiffStore( $dstKey, $key1, $key2, $keyN = null ) {} /** * Returns the contents of a set. * * @param string $key * @return array An array of elements, the contents of the set. * @link http://redis.io/commands/smembers * @example * <pre> * $redis->delete('s'); * $redis->sAdd('s', 'a'); * $redis->sAdd('s', 'b'); * $redis->sAdd('s', 'a'); * $redis->sAdd('s', 'c'); * var_dump($redis->sMembers('s')); * * //array(3) { * // [0]=> * // string(1) "c" * // [1]=> * // string(1) "a" * // [2]=> * // string(1) "b" * //} * // The order is random and corresponds to redis' own internal representation of the set structure. * </pre> */ public function sMembers( $key ) {} /** * @see sMembers() * @param string $key * @link http://redis.io/commands/smembers */ public function sGetMembers( $key ) {} /** * Sets a value and returns the previous entry at that key. * * @param string $key * @param string $value * @return string A string, the previous value located at this key. * @link http://redis.io/commands/getset * @example * <pre> * $redis->set('x', '42'); * $exValue = $redis->getSet('x', 'lol'); // return '42', replaces x by 'lol' * $newValue = $redis->get('x')' // return 'lol' * </pre> */ public function getSet( $key, $value ) {} /** * Returns a random key. * * @return string: an existing key in redis. * @link http://redis.io/commands/randomkey * @example * <pre> * $key = $redis->randomKey(); * $surprise = $redis->get($key); // who knows what's in there. * </pre> */ public function randomKey( ) {} /** * Switches to a given database. * * @param int $dbindex * @return bool TRUE in case of success, FALSE in case of failure. * @link http://redis.io/commands/select * @example * <pre> * $redis->select(0); // switch to DB 0 * $redis->set('x', '42'); // write 42 to x * $redis->move('x', 1); // move to DB 1 * $redis->select(1); // switch to DB 1 * $redis->get('x'); // will return 42 * </pre> */ public function select( $dbindex ) {} /** * Moves a key to a different database. * * @param string $key * @param int $dbindex * @return bool: TRUE in case of success, FALSE in case of failure. * @link http://redis.io/commands/move * @example * <pre> * $redis->select(0); // switch to DB 0 * $redis->set('x', '42'); // write 42 to x * $redis->move('x', 1); // move to DB 1 * $redis->select(1); // switch to DB 1 * $redis->get('x'); // will return 42 * </pre> */ public function move( $key, $dbindex ) {} /** * Renames a key. * * @param string $srcKey * @param string $dstKey * @return bool: TRUE in case of success, FALSE in case of failure. * @link http://redis.io/commands/rename * @example * <pre> * $redis->set('x', '42'); * $redis->rename('x', 'y'); * $redis->get('y'); // → 42 * $redis->get('x'); // → `FALSE` * </pre> */ public function rename( $srcKey, $dstKey ) {} /** * @see rename() * @link http://redis.io/commands/rename * @param string $srcKey * @param string $dstKey */ public function renameKey( $srcKey, $dstKey ) {} /** * Renames a key. * * Same as rename, but will not replace a key if the destination already exists. * This is the same behaviour as setNx. * * @param