PHPredisApi Chinese Document

Source: Internet
Author: User
Tags redis version
Phpredis is an extension of php, and the efficiency is quite high. It has the linked list sorting function and is useful for creating memory-level module business relationships. The following is the command usage tips officially provided by redis: github. comowlientphpredis (redis2.0.4 supported) Redis ::__ construct constructor $ redisnewRedis ()

Phpredis is an extension of php, and the efficiency is quite high. It has the linked list sorting function and is useful for creating memory-level module business relationships. The following is the command usage tips officially provided by redis: https://github.com/owlient/phpredis (Support redis 2.0.4) Redis ::__ construct constructor $ redis = new Redis ()

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 :__ construct Constructor
$ Redis = new Redis ();

Connect, open link 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 and popen do not close links.
Refer to the above

SetOption: Set redis Mode

GetOption: view the mode set by redis

Ping to view connection status

Get to get the value of a key (string value)
If the key does not exist, return false

Set writes key and value (string value)
If the write is successful, return true

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

Setnx determines whether the data is repeated and the written Value
$ Redis-> setnx ('key', 'value ');
$ Redis-> setnx ('key', 'value ');

Delete deletes 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)
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: MULTI or Redis: 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 (after code testing, it cannot achieve the said effect)
Checks whether the value of a key is changed by other programs. 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 );

List-related 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 type value of the key.
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

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.