Php-redis Command Documentation _php Tutorial

Source: Internet
Author: User

Php-redis Command Documentation


Redis is essentially a key/value database, similar to the memcached NoSQL database, but his data can be persisted on disk, resolving the problem of data not lost after service restart, its value can be string (string), list, Sets (collection) or ordered sets (the sorted collection), all data types have Push/pop, Add/remove, perform service-side aggregation, intersection, two sets sets of differences, and so on, these operations are atomic, Redis also supports a variety of sorting capabilities

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

Phpredis is an extension of PHP, the efficiency is quite high with the list sorting function, to create a memory-level module business relationship

The following are the official Redis command tips:

Redis::__construct constructor function

$redis = new Redis ();

Connect, open link Redis service

Parameters

host:string, service address

Port:int, port number

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

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

Ping View connection Status

Get gets the value of a key (string value)

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

Set writes key and value (string value)

The SET command always returns OK until the Redis 2.6.12 version.

Starting with the Redis 2.6.12 release, SET returns OK when the setup operation completes successfully.

If NX or XX is set, but the set operation is not executed because the condition is not met, then the command returns an empty bulk reply (null Bulk Reply).

Setex with time-to-live write value

Associates a value of values to a key and sets the lifetime of the key to seconds (in seconds).

If the 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 the time to live

The difference is that Setex is an atomic (atomic) operation, associated values and setting the lifetime of two actions will be completed at the same time,

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

#粒子

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

Setnx determine if duplicate, write value

The value of #将 key is set to value when and only if key does not exist. If the given key already exists, then SETNX does not do any action

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

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

Delete Deletes the value of the specified key

Returns the number of keys that have been deleted (long integers)

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

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

Ttl

Get a key to the time of survival

Persist

Remove key for lifetime expiration

If the key expires true if it does not expire false

Mset (Redis version 1.1 or above only available)

Assign values to multiple keys at the same time

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

Exists

Determine if key exists. Presence true not false

$redis->exists (' Test ');

INCR, Incrby

The value in key is increased by 1, and if the second argument is filled in, the value that is filled in the second parameter is added

$redis->incr (' key1′);

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

DECR, Decrby

Do subtraction, use the same method as incr

GetMultiple

Pass the reference

An array of key elements

return parameters

If key exists return value, there is 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 with the name key

Rpush

$redis->rpush (key, value);

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

Lpushx/rpushx

$redis->lpushx (key, value);

Add a value to the left (head)/Right (tail) of the list with the name key, and if value already exists, do not add

Lpop/rpop

$redis->lpop (' key ');

The output name is the first element of the list from the left (head)/Right (tail) of key, deleting the element

Blpop/brpop

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

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

Lsize

$redis->lsize (' key ');

Returns the number of elements for a list with the name key

LIndex, LGet

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

Returns the element of the index position in the list with the name key

LSet

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

Assigns the element of the index position in the list named key to value

Lrange, Lgetrange

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

Returns the element between start and end in the list named key (end is-1, returns all)

LTrim, Listtrim

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

Intercepts the list named key, preserving the element between start and end

Lrem, Lremove

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

Remove the count of the elements in the list with the name key as value. Count is 0, removing all elements that value is count>0, removing the element with count values of value from start to finish, count<0 the element that |count| value is removed from the end of a header

Linsert

In the list named key, find value for pivot and Redis::before by parameter | Redis::after, to make sure that the newvalue is placed in front of the 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 removes the tail element of the list named Srckey and adds the element to the head of 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

Adds an element value to a set named key, if value exists, does not write, return false

$redis->sadd (key, value);

Srem, Sremove

Delete the element in the set named key value

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

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

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

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

Smove

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

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

Sismember, Scontains

Find if there is a value element in the collection named key, there is 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 an element in a set with the name key

Srandmember

Randomly returns an element in the set named key, without deleting

SInter

Ask for Intersection

Sinterstore

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

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

Sunion

Seek and set

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

S0,S1,S2 simultaneous request and set

Sunionstore

To set and save the Assembly to the set of output

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

Sdiff

Finding the difference set

Sdiffstore

Finding the difference set and saving the difference set to the set of output

Smembers, Sgetmembers

Returns all elements of a set with a name of 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 from the original key and writes 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 string with the name key appended with value

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

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

$redis->get (' key ');

GetRange (method does not exist)

Returns the character between start and end in a string named key

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

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

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

SetRange (method does not exist)

The character between start and end in a string that changes key is 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 binary information

http://www.bkjia.com/PHPjc/952701.html www.bkjia.com true http://www.bkjia.com/PHPjc/952701.html techarticle Php-redis Command Document Redis is essentially a key/value database, similar to memcached NoSQL database, but his data can be persisted on disk, resolving the service restart ...

  • 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.