Php-redis Chinese Document

Source: Internet
Author: User
Tags redis version

Phpredis is an extension of PHP, and the efficiency is quite high. It provides the linked list sorting function to create memory-level module business relationships.

 

Useful. The following are the command tips officially provided by redis:

 

 

 

As follows:

 

Https://github.com/owlient/phpredis (support for redis 2.0.4)


Redis :__ constructConstructor
$ Redis = new redis ();

Connect, openLink to the redis Service
Parameters
Host
: String, service address
Port
: Int, Port Number
Timeout
: Float, link duration (optional, default value: 0, unlimited link time)
Note: There is time in redis. conf. The default value is 300.

Pconnect, popenLinks that are not automatically closed
Refer to the above

SetoptionSet redis Mode

GetoptionView the mode set by redis

PingView connection status

GetObtains the value of a key (string value)
If the key does not exist, returnFalse

SetWrite key and value (string value)
If the write is successful, returnTure

SetexWrite value with TTL
$ Redis-> setex ('key', 3600, 'value'); // sets key → value, with 1 h TTL.

SetnxJudge whether there are duplicates, write Value
$ Redis-> setnx ('key', 'value ');
$ Redis-> setnx ('key', 'value ');

DeleteDeletes the value of a specified key.
Returns the number of deleted keys (long integer)
$ Redis-> Delete ('key1', 'key2 ');
$ Redis-> Delete (Array ('key3', 'key4', 'key5 '));

TTL
Get the survival time of a key

Persist
Remove the expired key
If the key expires, true if the key does not expire, false

Mset(Redis version 1.1 or later is available)
Assign values to multiple keys at the same time
$ Redis-> mset (Array ('key0' => 'value0', 'key1' => 'value1 '));

Multi, exec, discard
Enter or exit the transaction mode
Redis: MultiOrRedis: pipeline. The default value is redis: Multi.
Redis: Multi: execute multiple operations as one transaction
Redis: Pipeline: Allows (multiple) simple commands to be executed and sent to the server more quickly, without any atomic guarantee.
Discard: deletes a transaction.
Return Value
Multi (), returns a redis object, and enters the multi-mode. Once the multi-mode is entered, all methods called in the future will return the same object, only to Exec () method is called.

Watch, unwatch(CodeAfter the test, the effect cannot be achieved)
Monitor whether the value of a key is replaced by otherProgramChange. If the key is modified between watch and exec (method), the execution of the multi/exec transaction will fail (return false)
Unwatch cancels all keys monitored by this program
Parameter, a list of keys
$ 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 channel. Note that this method may change in the future

Exists
Determine whether the key exists. True or not false

Incr, incrby
The value in the key is auto-incremented by 1. If the second parameter is entered, the value entered by the second parameter is auto-incrementing.
$ Redis-> incr ('key1 ');
$ Redis-> incrby ('key1', 10 );

Decr, decrby
Subtraction, using the same method as incr

Getmultiple
Passing Parameters
Array composed of keys
Response parameters
If the key exists, return value. If the key does not exist, 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 );

ListRelated Operations
Lpush
$ Redis-> lpush (Key, value );
Add an element with the value to the left (header) of the list named key

Rpush
$ Redis-> rpush (Key, value );
Add an element with the value to the right (end) of the list named key

Lpushx/Rpushx
$ Redis-> lpushx (Key, value );
Add an element with the value to the left (header)/right (tail) of the list with the key name. If the value already exists, do not add

Lpop/Rpop
$ Redis-> lpop ('key ');
Delete the first element from the left (header) and right (tail) of the list with the name key.

Blpop/brpop
$ Redis-> blpop ('key1', 'key2', 10 );
The block version of The lpop command. If timeout is 0IThe command ends if the list does not exist or the list is empty. If the timeout value is greater than 0, the system waits for timeout seconds in the case of the preceding situation. If the problem persistsI + 1Start the pop operation on the list.

Lsize
$ Redis-> lsize ('key ');
Returns the number of elements in a list named key.

Lindex, lget
$ Redis-> lget ('key', 0 );
Returns the index position element in the list named key.

Lset
$ Redis-> lset ('key', 0, 'x ');
Assign a value to the index position element in the list named key

Lrange, lgetrange
$ Redis-> lrange ('key1', 0,-1 );
Returns the elements between start and end in the list named key (end is-1, and all elements are returned)

Ltrim, listtrim
$ Redis-> ltrim ('key', start, end );
Extract the list named key and retain the elements between start and end.

Lrem, lremove
$ Redis-> lrem ('key', 'A', 2 );
Delete the elements in the list with the Count value as the key. If the value of count is 0, all elements with the value are deleted. If the value of count is greater than 0, all elements with the value are deleted from the beginning to the end, count <0 Delete from end to end | count | elements with a value

Linsert
In the list named key, the value isBytesAccording to the parameter redis: Before | redis: After, newvalue is placed before or after the queue. If the key does not exist, it is not inserted. If the key 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
Return and delete the end element of the list named srckey, and add the element to the header of the list named dstkey.
$ Redis-> Delete ('x', 'y ');
$ Redis-> lpush ('x', 'abc'); $ redis-> lpush ('x', 'def '); $ redis-> lpush ('y ', '000000'); $ redis-> lpush ('y', '000000'); // 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 Operations
Sadd
Add the element value to the set named key. If the value exists and is not written, return false
$ Redis-> sadd (Key, value );

Srem, sremove
Delete the element value in the Set named key
$ Redis-> sadd ('key1', 'set1 ');
$ Redis-> sadd ('key1', 'set2 ');
$ Redis-> sadd ('key1', 'set3 ');
$ Redis-> Srem ('key1', 'set2 ');

Smove
Move the value element from the set named srckey to the set named dstkey.
$ Redis-> smove (seckey, dstkey, value );

Sismember, scontains
Check whether the value element exists in the Set named key. If the value element exists, true or false.
$ Redis-> sismember (Key, value );

Scard, ssize
Returns the number of set elements named key.

Spop
Returns and deletes an element of a set named key at random.

Srandmember
Random return of an element in the Set named key, not deleted

Sinter
Intersection

Sinterstore
Calculates and saves the intersection to the output set.
$ Redis-> sinterstore ('output', 'key1', 'key2', 'key3 ')

Sunion
Union
$ Redis-> sunion ('s0', 's1', 's2 ');
Returns the Union of S0, S1, and S2.

Sunionstore
Returns the Union set and saves the Union set to the output set.
$ Redis-> sunionstore ('output', 'key1', 'key2', 'key3 ');

Sdiff
Difference set

Sdiffstore
Evaluate the difference set and save the difference set to the output set

Smembers, sgetmembers
Returns all the elements of a set named 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 '); //, 5
Var_dump ($ redis-> sort ('s ', array ('sort' => 'desc'); // 5, 4, 3, 2, 1
Var_dump ($ redis-> sort ('s ', array ('Port' => 'desc', 'store' => 'out'); // (INT) 5
 
String command
GetSet
Returns the value in the original key and writes the 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 the string with the name of key is appended with value
$ Redis-> set ('key', 'value1 ');
$ Redis-> append ('key', 'value2 ');
$ Redis-> get ('key ');

Getrange(Method does not exist)
Returns the start to end characters in the string with the key name.
$ Redis-> set ('key', 'string value ');
$ Redis-> getrange ('key', 0, 5 );
$ Redis-> getrange ('key',-5,-1 );

Setrange(Method does not exist)
Change the character between start and end in the string of the key to value.
$ Redis-> set ('key', 'Hello World ');
$ Redis-> setrange ('key', 6, "redis ");
$ Redis-> get ('key ');

Strlen
Obtain the string length of the key.
$ Redis-> strlen ('key ');

Getbit / Setbit
Returns binary information.

Zset(Sorted set) Operation-related
Zadd (Key, score, Member) : Add the member element to the zset named key. Score is used for sorting. If the element already exists, the sequence of the element is updated based on the 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 the elements of index from start to end in the zset with the key name (the elements are sorted in ascending order of score ).
$ Redis-> zadd ('key1', 0, 'val0 ');
$ Redis-> zadd ('key1', 2, 'val2 ');
$ Redis-> zadd ('key1', 10, 'val10 ');
$ Redis-> zrange ('key1', 0,-1); // with scores $ redis-> zrange ('key1', 0,-1, true );

Zdelete, zrem
Zrem (Key, Member) : Delete the element member in the zset named key.
$ Redis-> zadd ('key', 0, 'val0 ');
$ Redis-> zadd ('key', 2, 'val2 ');
$ Redis-> zadd ('key', 10, 'val10 ');
$ Redis-> zdelete ('key', 'val2 ');
$ Redis-> zrange ('key', 0,-1 );

Zrevrange(Key, start, end,Withscores): Returns all the elements of index from start to end in the zset with the key name (the elements are sorted by score in ascending order.Withscores: Whether to output the socret value. The default value is false. No value is output.
$ Redis-> zadd ('key', 0, 'val0 ');
$ Redis-> zadd ('key', 2, 'val2 ');
$ Redis-> zadd ('key', 10, '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 the elements of the zset whose names are key, score> = Star and score <= end.

Zcount
$ Redis-> zcount (Key, Star, end );
Returns the number of all elements in the zset with the name of key, score> = Star and score <= end.

Zremrangebyscore, zdeleterangebyscore
$ Redis-> zremrangebyscore ('key', Star, end );
Delete all the elements with score> = Star and score <= end in the zset with the key, and return the number of deletions.

Zsize, zcard
Returns the number of all elements in the zset named key.

Zscore
$ Redis-> zscore (Key, val2 );
Returns the score of element val2 in a zset named key.

Zrank, zrevrank
$ Redis-> zrevrank (Key, Val );
Return the rank (index, starting from 0) of the Val element in the zset with the key name (the element has been sorted in ascending order of score). If there is no Val element, return "null ". Zrevrank is sorted in ascending order.

Zincrby
$ Redis-> zincrby ('key', increment, 'member ');
If the element member already exists in the zset named key, the score of this element is added with increment; otherwise, this element is added to the set, 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 during the zunion.
Perform union and intersection on N zsets and save the last set in dstkeyn. For the score of each element in the Set, the score must be multiplied by the weight parameter before the aggregate operation. If weight is not provided, the default value is 1. The default aggregate value is sum, that is, the score of the element in the result set is the sum calculation value for all the corresponding elements in the Set, while min and Max refer, the score of an element in the result set is the minimum and maximum values of all elements in the set.

HashOperation
Hset
$ Redis-> hset ('h', 'key1', 'Hello ');
Add the element key1-> hello to the hash named H.

Hget
$ Redis-> hget ('h', 'key1 ');
Returns the value (Hello) corresponding to key1 in hash with the name H)

Hlen
$ Redis-> hlen ('H ');
Returns the number of elements in hash with the name H.

Hdel
$ Redis-> hdel ('h', 'key1 ');
Delete the key 1 field in the hash with the name H.

Hkeys
$ Redis-> hkeys ('H ');
Returns all the keys in the hash with the key name.

Hvals
$ Redis-> hvals ('H ')
Returns the value corresponding to all keys in hash with the name H.

Hgetall
$ Redis-> hgetall ('H ');
Returns all the keys (fields) in the hash with the name H and their corresponding values.

Hexists
$ Redis-> hexists ('h', 'A ');
Indicates whether the hash with the name H contains a domain with the key name.

Hincrby
$ Redis-> hincrby ('h', 'x', 2 );
Increase the value of X in hash named h by 2.

Hmset
$ Redis-> hmset ('user: 1', array ('name' => 'job', 'salary '=> 2000 ));
Add multiple elements to the hash with the key name

Hmet
$ Redis-> hmet ('h', array ('field1', 'field2 '));
Returns the value corresponding to field1 and field2 in hash with the name H.

Redis operations
Flushdb
Clear Current Database

Flushall
Clear all databases

Randomkey
Returns a random key of the key space.
$ 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 42 to X
$ Redis-> move ('x', 1); // move to DB 1
$ Redis-> select (1); // switch to DB 1
$ Redis-> get ('x'); // will 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 be replaced

SetTimeout, expire
Set the activity time of a key (s)
$ Redis-> setTimeout ('x', 3 );

Expireat
Key survived to a Unix Timestamp
$ Redis-> expireat ('x', time () + 3 );

Keys, getkeys
Returns all keys that meet the specified pattern.
$ Keywithuserprefix = $ redis-> keys ('user *');

Dbsize
Check the number of keys in the current database
$ COUNT = $ redis-> dbsize ();

Auth
Password Authentication
$ Redis-> auth ('foobared ');

Bgrewriteaof
Use aof for database persistence
$ Redis-> bgrewriteaof ();

Slaveof
Select slave server
$ Redis-> slaveof ('10. 0.1.7 ', 6379 );

Save
Save data to disk synchronously

Bgsave
Asynchronously save data to a disk

Lastsave
Returns the Unix timestamp when data is successfully saved to the disk.

Info
Returns the redis version information and other details.

type
return the key type value
string: redis: redis_string
set: redis :: redis_set
List: redis: redis_list
zset: redis: redis_zset
hash: redis: redis_hash
Other: redis :: redis_not_found

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.