Home  >  Article  >  Backend Development  >  PHP-redis command documentation_PHP tutorial

PHP-redis command documentation_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:08:11809browse

PHP-redis command documentation

Redis is essentially a Key/Value database, a NoSQL database similar to Memcached, but its data can be persisted on the disk. It solves the problem of data not being lost after the service is restarted. Its value can be string, list, sets or ordered sets. All data types have push/ Pop, add/remove, perform server-side operations such as union, intersection, difference between two sets, etc. These operations are all atomic. Redis also supports various sorting capabilities

Redis 2.0 adds many new features, such as: improved performance, new data types, and less memory utilization (AOF and VM)

phpredis is an extension of PHP. It is very efficient and has a linked list sorting function. It is useful for creating memory-level module business relationships

The following are the command usage tips officially provided by redis:

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 is 0, no limit to link time)

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

Ping to check the connection status

 get gets the value of a certain key (string value)

If the key does not exist, return the special value nil; if the key is not a string type, return an error

Set writes key and value (string value)

Before Redis version 2.6.12, the set command always returned OK .

Starting from Redis version 2.6.12, set will only return OK when the setting operation is successfully completed.

If NX or XX is set, but the setting operation is not executed because the conditions are not met, the command returns a NULL Bulk Reply.

Setex written value with survival time

Associate the value value to key and set the key's lifetime to seconds (in seconds).

If key already exists, the SETEX command will overwrite the old value.

This command is similar to the following two commands:

 SET key value

 EXPIRE key seconds # Set survival time

The difference is that SETEX is an atomic operation, and the two actions of associating the value and setting the survival time will be completed at the same time,

This command is very useful when Redis is used as a cache.

 #particles

 $redis->setex(‘key’, 3600, ‘value’); // sets key → value, with 1h TTL.

Setnx determines whether it is repeated and writes the value

 #Set the value of key to value if and only if key does not exist. If the given key already exists, SETNX does not take any action

$redis->setnx(‘key’, ‘value’);

$redis->setnx(‘key’, ‘value’);

Delete deletes the value of the 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 keys whose lifetime has expired

true if the key expires false if it does not expire

mset (can only be used with redis version 1.1 or above)

Assign values ​​to multiple keys at the same time

 $redis->mset(array(‘key0′ => ‘value0′, ‘key1′ => ‘value1′));

exists

Determine whether the key exists. exists true and is not false

$redis->exists(‘test’);

 incr, incrBy

The value in key will be incremented by 1. If the second parameter is filled in, it will be incremented by the value filled in the second parameter

 $redis->incr(‘key1′);

 $redis->incrBy(‘key1′, 10);

 decr, decrBy

To do subtraction, use the same method as incr

getMultiple

Passing parameters

An array composed of keys

Return parameters

If the key exists, return value, if it 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);#Get all lists

List related operations

lPush

 $redis->lPush(key, value);

Add an element with value as value to the left (head) of the list named key

rPush

 $redis->rPush(key, value);

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

lPushx/rPushx

 $redis->lPushx(key, value);

Add an element with value to the left (head)/right (tail) of the list named key. If value already exists,

will not be added.

lPop/rPop

 $redis->lPop(‘key’);

Output the first element from the left (head)/right (tail) of the list named key, delete the element

blPop/brPop

$redis->blPop(‘key1′, ‘key2′, 10);

Block version of lpop command. That is, when timeout is 0, if the list named key i does not exist or the list is empty, the command ends. If timeout>0, when encountering the above situation, wait for timeout seconds. If the problem is not solved, perform pop operation on the list starting from keyi+1

lSize

 $redis->lSize(‘key’);

Returns how many elements the list named key has

lIndex, lGet

 $redis->lGet(‘key’, 0);

Returns the element at index position in the list named key

lSet

 $redis->lSet(‘key’, 0, ‘X’);

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

lRange, lGetRange

 $redis->lRange(‘key1′, 0, -1);

Return the elements between start and end in the list named key (end is -1, return all)

lTrim, listTrim

$redis->lTrim(‘key’, start, end);

Intercept the list named key and keep the elements between start and end

lRem, lRemove

 $redis->lRem(‘key’, ‘A’, 2);

Delete count elements whose value is value in the list named key. count is 0, delete all elements with value, count>0 deletes count elements with value from beginning to end, count<0 deletes |count| elements with value

from end to end

lInsert

In the list named key, find the value of pivot, and determine whether newvalue is placed before or after pivot according to the parameters Redis::BEFORE | Redis::AFTER. If the key does not exist, it will not be inserted. If the 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

Return and delete the tail element of the list named srckey, and add 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 the set named key. If value exists, do not write it and 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 collection named srckey to the collection named dstkey

 $redis->sMove(seckey, dstkey, value);

 sIsMember, sContains

Find whether there is a value element in the collection named key, if there is true, there will be no false

 $redis->sIsMember(key, value);

sCard, sSize

Returns the number of elements of the set named key

 sPop

Randomly return and delete an element in the set named key

sRandMember

Randomly returns an element in the set named key without deleting

sInter

Find intersection

 sInterStore

Find the intersection and save the intersection to the output collection

$redis->sInterStore(‘output’, ‘key1′, ‘key2′, ‘key3′)

 sUnion

Find the union

$redis->sUnion(‘s0′, ‘s1′, ‘s2′);

Find the union of s0, s1, s2 at the same time

sUnionStore

Find the union and save the union to the output collection

 $redis->sUnionStore(‘output’, ‘key1′, ‘key2′, ‘key3′);

sDiff

Find the difference set

 sDiffStore

Find the difference set and save the difference set to the output set

sMembers, sGetMembers

Returns all elements of the 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’)); // 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

Return the value in the original key and write the value to 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 named key is followed by value

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

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

$redis->get(‘key’);

 getRange (method does not exist)

Returns the characters between start and end in the string named key

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

 $redis->getRange(‘key’, 0, 5);

 $redis->getRange(‘key’, -5, -1);

setRange (method does not exist)

Change the characters between start and end in the key string to value

$redis->set(‘key’, ‘Hello world’);

 $redis->setRange(‘key’, 6, “redis”);

$redis->get(‘key’);

strlen

Get the length of the string of key

 $redis->strlen(‘key’);

getBit/setBit

Return binary information

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/952701.htmlTechArticlePHP-redis command document Redis is essentially a Key/Value database, a NoSQL database similar to Memcached, but it The data can be persistently saved on the disk, which solves the problem of service restart...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn