Php-redis Chinese Help document

Source: Internet
Author: User
Tags auth redis redis version strlen port number

The download address is as follows:


Https://github.com/owlient/phpredis


Redis::__construct Constructors


$redis = new Redis ();


Connect, open link Redis service


Parameters


host:string, service address


Port:int, port number


Timeout:float, link length (optional, default is 0, unlimited link time)


Note: There is also time in redis.conf, the default is 300


Pconnect, Popen will not actively close the link


Reference above


SetOption Set Redis mode


GetOption View the mode of Redis settings


Ping View connection Status


Get gets the value of a key (string value)


If the key does not exist, return false


Set Write key and value (string value)


If the write succeeds, return ture


Setex write value with live time


$redis->setex (' key ', 3600, ' value '); Sets Key→value, with 1h TTL.


Setnx to determine whether to repeat, write value


$redis->setnx (' key ', ' value ');


$redis->setnx (' key ', ' value ');


Delete Deletes the value of the specified key


Returns the number of key deleted (Long integer)


$redis->delete (' key1′, ' key2′);


$redis->delete (Array (' key3′, ' key4′, ' key5′);


Ttl


To get a key's survival time


Persist


Remove key that expires in lifetime


If the key expires true if it does not expire false


Mset (more than 1.1 Redis version can be used)


Assigning multiple keys at the same time


$redis->mset (Array (' key0′=> ' value0′, ' key1′=> ' value1′));


Multi, exec, discard


Enter or exit Transaction mode


Parameter optional Redis::multi or Redis::P ipeline. Default is Redis::multi


Redis::multi: Perform multiple operations as a single transaction


Redis::P ipeline: Make (multiple) commands simple, faster to send to the server, but without any assurance of atomicity


Discard: Delete a transaction


return value


multi (), returns a Redis object, and enters the multi-mode mode, once the multi-mode mode is entered, all later calls will return the same object, and only the Exec () method is invoked.


Watch, Unwatch (after the code test, can not achieve the said effect)


Monitors whether the value of a key is changed by another program. If this key is modified between watch and Exec (method), the execution of this multi/exec transaction will fail (return false)


Unwatch cancels all key that is monitored by this program


parameters, a list of key pairs


$redis->watch (' x ');


$ret = $redis->multi ()->incr (' x ')->exec ();


Subscribe *


Method callback. Note that this method may change in the future


Publish *


Publish content to a certain channel. Note that this method may change in the future


Exists


Determines whether a key exists. exists true not false


INCR, Incrby


The value in the key is 1, and if the second argument is filled in, the value of the second argument is added


$redis->incr (' key1′);


$redis->incrby (' key1′, 10);


DECR, Decrby


Do subtraction, use method with INCR


GetMultiple


Pass Reference


An array of key components


return parameters


If key exists return value, no return False


$redis->set (' key1′, ' value1′); $redis->set (' key2′, ' value2′); $redis->set (' key3′, ' value3′); $redis->getmultiple (Array (' key1′, ' key2′, ' key3′);


$redis->lrem (' key1′, ' A ', 2);


$redis->lrange (' key1′, 0,-1);


List related actions


Lpush


$redis->lpush (key, value);


Add an element of value to the left (header) of the list named key


Rpush


$redis->rpush (key, value);


Add an element of value to the right (end) of the list named key


Lpushx/rpushx


$redis->lpushx (key, value);


Adds an element of value to the left (head)/Right (tail) of the list named key, without adding


Lpop/rpop


$redis->lpop (' key ');


The output name is the first element of the list left (head) starting/Right (tail) of the key, and the element is deleted


Blpop/brpop


$redis->blpop (' key1′, ' key2′, 10);


The block version of the Lpop command. That is, when timeout is 0 o'clock, the command ends if a list named key I does not exist or the list is empty. If timeout>0, then wait timeout seconds if the problem is not resolved, and perform a pop operation on the list of keyi+1 starts


Lsize


$redis->lsize (' key ');


Returns how many elements of a list with the name key


Lindex, Lget


$redis->lget (' key ', 0);


Returns an element of the index position in the list named key


LSet


$redis->lset (' key ', 0, ' X ');


Assign value to the element at index position in list named key


Lrange, Lgetrange


$redis->lrange (' key1′, 0,-1);


Returns the element between start to end in the list with the name Key (end-1, all)


LTrim, Listtrim


$redis->ltrim (' key ', start, end);


Intercepts the list with the name key, preserving the element between start and end


Lrem, Lremove


$redis->lrem (' key ', ' A ', 2);


Deletes the element with value in the list with Count name key. Count is 0 to delete all elements of value, Count>0 deletes the element with the count value of values from the end and count<0 deletes the element with value |count|


Linsert


In the list named key, find value of pivot, and Redis::before by parameter | Redis::after, to be sure, newvalue is placed in front of pivot, or behind. If key does not exist, it will not be inserted, if pivot does not exist, return-1


$redis->delete (' key1′); $redis->linsert (' key1′, Redis::after, ' A ', ' X '); $redis->lpush (' key1′, ' A '); $redis->lpush (' key1′, ' B '); $redis->lpush (' key1′, ' C '); $redis->linsert (' key1′, Redis::before, ' C ', ' X ');


$redis->lrange (' key1′, 0,-1);


$redis->linsert (' key1′, Redis::after, ' C ', ' Y ');


$redis->lrange (' key1′, 0,-1);


$redis->linsert (' key1′, Redis::after, ' W ', ' value ');


Rpoplpush


Returns and deletes the tail element of the list named Srckey and adds the element to the head of the list named Dstkey


$redis->delete (' x ', ' Y ');


$redis->lpush (' x ', ' abc '); $redis->lpush (' x ', ' Def '); $redis->lpush (' y ', ' 123′ '); $redis->lpush (' y ', ' 456′ '); Move the last of X to the front of Y. Var_dump ($redis->rpoplpush (' x ', ' Y '));


Var_dump ($redis->lrange (' x ', 0,-1));


Var_dump ($redis->lrange (' Y ', 0,-1));


String (3) "ABC"


Array (1) {[0]=> string (3) "Def"}


Array (3) {[0]=> string (3) "ABC" [1]=> string (3) "456″[2]=> string (3)" 123″}


Set operation related


Sadd


add element value to set with Name key, if value exists, does not write, return false


$redis->sadd (key, value);


Srem, Sremove


Delete element value in set with Name key


$redis->sadd (' key1′, ' set1′);


$redis->sadd (' key1′, ' set2′);


$redis->sadd (' key1′, ' set3′);


$redis->srem (' key1′, ' set2′);


Smove


Move the value element from the collection named Srckey to the collection named Dstkey


$redis->smove (Seckey, Dstkey, value);


Sismember, Scontains


Find out if there is a value element in the collection named Key, with Ture no false


$redis->sismember (key, value);


SCard, Ssize


Returns the number of elements of a set with the name key


Spop


Randomly returns and deletes one element of a set with the name key


Srandmember


Randomly returns an element of the set with the name key and does not delete


Sinter


Find intersection


Sinterstore


To find the intersection and save the intersection to the output collection


$redis->sinterstore (' Output ', ' key1′, ' key2′, ' key3′)


Sunion


Seek and set


$redis->sunion (' s0′, ' s1′, ' s2′);


S0,S1,S2 Simultaneous Collection


Sunionstore


To set and save the collection to an output set


$redis->sunionstore (' Output ', ' key1′, ' key2′, ' key3′);


Sdiff


Finding the difference set


Sdiffstore


To find the difference set and save the difference set to the output collection


Smembers, Sgetmembers


Returns all elements of a set with the name key


Sort


Sorting, paging, etc.


Parameters


' By ' => ' some_pattern_* ',


' Limit ' => array (0, 1),


' Get ' => ' some_other_pattern_* ' or an array of patterns,


' Sort ' => ' asc ' or ' desc ',


' Alpha ' => TRUE,


' Store ' => ' External-key '


Example


$redis->delete (' s '); $redis->sadd (' s ', 5); $redis->sadd (' s ', 4); $redis->sadd (' s ', 2); $redis->sadd (' s ', 1); $redis->sadd (' s ', 3);


Var_dump ($redis->sort (' s ')); 1,2,3,4,5


Var_dump ($redis->sort (' s ', array (' sort ' => ' desc ')); 5,4,3,2,1


Var_dump ($redis->sort (' s ', array (' sort ' => ' desc ', ' Store ' => ' out ')); (int) 5


String command


Getset


Returns the value in the original key and writes value to the key


$redis->set (' x ', ' 42′ ');


$exValue = $redis->getset (' x ', ' lol '); Return ' 42′, replaces x by ' LOL '


$newValue = $redis->get (' x ') '//return ' LOL '


Append


String, the value of string with the name key appended to


$redis->set (' key ', ' value1′ ');


$redis->append (' key ', ' value2′ ');


$redis->get (' key ');


GetRange (method does not exist)


Returns the character between start to end in a string with the name key


$redis->set (' key ', ' string value ');


$redis->getrange (' key ', 0, 5);


$redis->getrange (' key ',-5,-1);


SetRange (method does not exist)


Change the character from start to end in string of key to value


$redis->set (' key ', ' Hello World ');


$redis->setrange (' key ', 6, "Redis");


$redis->get (' key ');


Strlen


The length of the string that gets the key


$redis->strlen (' key ');


Getbit/setbit


Returns 2 Feed information


Zset (sorted set) operations related


Zadd (key, Score, member): Adds element Member,score to the zset named key for sorting. If the element already exists, the order of the elements is updated according to score.


$redis->zadd (' key ', 1, ' val1′);


$redis->zadd (' key ', 0, ' val0′);


$redis->zadd (' key ', 5, ' val5′);


$redis->zrange (' key ', 0,-1); Array (val0, Val1, VAL5)


Zrange (Key, Start, End,withscores): Returns all elements of index from start to end in Zset with the name key (the element has been sorted score from small to large)


$redis->zadd (' key1′, 0, ' val0′);


$redis->zadd (' key1′, 2, ' val2′);


$redis->zadd (' key1′, the ' val10′ ');


$redis->zrange (' key1′, 0,-1); With scores $redis->zrange (' key1′, 0,-1, true);


ZDelete, Zrem


Zrem (Key, member): Delete element member in Zset with name key


$redis->zadd (' key ', 0, ' val0′);


$redis->zadd (' key ', 2, ' val2′);


$redis->zadd (' key ', ten, ' val10′ ');


$redis->zdelete (' key ', ' val2′ ');


$redis->zrange (' key ', 0,-1);


Zrevrange (Key, Start, End,withscores): Returns all elements of the index from start to end in Zset with the name key (the element has been sorted score from large to small). Withscores: Whether to output Socre value, default false, no output


$redis->zadd (' key ', 0, ' val0′);


$redis->zadd (' key ', 2, ' val2′);


$redis->zadd (' key ', ten, ' val10′ ');


$redis->zrevrange (' key ', 0,-1); With scores $redis->zrevrange (' key ', 0,-1, true);


Zrangebyscore, Zrevrangebyscore


$redis->zrangebyscore (key, star, end, Array (withscores, limit));


Returns all elements of score >= star and score <= end in Zset with the name key


Zcount


$redis->zcount (key, star, end);


Returns the number of all elements score >= star and score <= end in Zset with the name key


Zremrangebyscore, Zdeleterangebyscore


$redis->zremrangebyscore (' key ', star, end);


Delete all elements of score >= star and score <= end in Zset named Key, returning the number of deletes


Zsize, Zcard


Returns the number of all elements of a zset named key


Zscore


$redis->zscore (key, Val2);


Returns the score of element Val2 in Zset with the name key


Zrank, Zrevrank


$redis->zrevrank (Key, Val);


Returns the rank (index, starting from 0) of the Val element in the Zset with the name key (the element has been sorted by score from small to large), and returns "NULL" if there is no Val element. Zrevrank is sorted from big to small


Zincrby


$redis->zincrby (' key ', increment, ' member ');


If the element member already exists in Zset with the name key, the score of the element is incremented increment, otherwise the element is added to the collection and its score value is increment


Zunion/zinter


Parameters


KeyOutput


Arrayzsetkeys


Arrayweights


AggregateFunction either "SUM", "MIN", or "MAX": Defines the behaviour to use on duplicate entries the during.


The n Zset are set and intersected, and the last set is saved in the Dstkeyn. For the score of each element in the collection, multiply the weight argument to the aggregate before making the operation. If weight is not provided, the default is 1. The default aggregate is sum, that is, the score of the elements in the result set is the sum of all the corresponding elements of the collection, while Min and Max refer to the score of the elements in the result set as the minimum and maximum values for all the corresponding elements of the collection.


Hash operation


Hset


$redis->hset (' h ', ' key1′, ' hello ');


Add an element to a hash named H Key1->hello


Hget


$redis->hget (' h ', ' key1′ ');


Returns the value of the Key1 in the hash named H (hello)


Hlen


$redis->hlen (' h ');


Returns the number of elements in a hash named H


Hdel


$redis->hdel (' h ', ' key1′ ');


Delete a field with the key key1 in the hash named H


Hkeys


$redis->hkeys (' h ');


Returns all keys in a hash named key


Hvals


$redis->hvals (' h ')


Returns the value of all keys in a hash named H


Hgetall


$redis->hgetall (' h ');


Returns all the keys (field) and their corresponding value in a hash named H


Hexists


$redis->hexists (' h ', ' a ');


Is there a field with the key name A in the hash named H


Hincrby


$redis->hincrby (' h ', ' X ', 2);


Increase the value of x in the hash named H by 2


Hmset


$redis->hmset (' user:1′, Array (' name ' => ' Joe ', ' Salary ' => 2000));


Add elements in bulk to hash with name key


Hmget


$redis->hmget (' h ', Array (' field1′, ' field2′));


Returns the value of the field1,field2 in a hash named H


Redis Operations Related


Flushdb


Empty the current database


Flushall


Empty all databases


Randomkey


A key that returns the key space at random


$key = $redis->randomkey ();


Select


Select a database


Move


Transfer a key to another database


$redis->select (0); Switch to DB 0


$redis->set (' x ', ' 42′ '); Write to X


$redis->move (' x ', 1); Move to DB 1


$redis->select (1); Switch to DB 1


$redis->get (' x '); would return 42


Rename, Renamekey


Rename Key


$redis->set (' x ', ' 42′ ');


$redis->rename (' x ', ' Y ');


$redis->get (' y '); →42


$redis->get (' x '); → ' FALSE '


Renamenx


Similar to Remane, however, if the renamed name already exists, it will not replace the success


SetTimeout, expire


Set the active time for a key (s)


$redis->settimeout (' x ', 3);


Expireat


Key survives to a UNIX timestamp time


$redis->expireat (' x ', Time () + 3);


Keys, Getkeys


Returns all keys that satisfy a given pattern


$keyWithUserPrefix = $redis->keys (' user* ');


Dbsize


See how many keys are in the database now


$count = $redis->dbsize ();


Auth


Password Authentication


$redis->auth (' foobared ');


Bgrewriteaof


Using AOF for database persistence


$redis->bgrewriteaof ();


Slaveof


Select from the server


$redis->slaveof (' 10.0.1.7′, 6379);


Save


To synchronize data to disk


Bgsave


To save data asynchronously to disk


Lastsave


Returns the UNIX timestamp that last successfully saved data to disk


Info


Returns the Redis version information, etc.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.