Redis Learning Note Five frequently uses PHP functions

Source: Internet
Author: User

The installation of Phpredis is here:

http://blog.csdn.net/xundh/article/details/46288277

Key-value operation
$redis=new Redis();$redis->connect(‘127.0.0.1‘6379);//參数:connect(host,port,timeout)//timeout能够为空,在redis.conf里timeout默认300//pconnect不会主动关闭的连接
Key
//write value$redis -Set($work -Uid$workString);$redis -SETNX ($work -Uid$workString);//key does not exist is an assignment$redis -Setex ($a, -,1);//range Set the value after the TTL is set to$redis -SETRANGE (Key,offset,value);//Get value$redis -Get$work -UID);//Delete key$redis -Del$work -UID); Var_dump ($redis -Get$work -UID));//return bool (FALSE)//key is presentif(!$redis -Exists' key ')) Var_dump ($redis -Del' key '));//return int (0)
Mset multiple key operations
//设置mset$array_mset=[‘a‘=>1,‘b‘=>2];$redis->mset($array);$redis->msetnx($array);    //key不存在时才写入。但一次要么全写。要么全不写。//读取mset$array_mget=[‘a‘,‘b‘];$redis->mget($arram_mget);//删除多个key$redis->del($array_mget);
Keys
//读取keys$array_mset_keys=[‘abc‘=>1,‘bcd‘=>2,‘cde‘=>3];$redis->keys(‘*a*‘);  //返回[‘a‘=>1];$redis->keys(‘b??‘);  //返回[‘b‘=>2];$redis->keys(‘*‘);    //返回全部keys
Getset Key value

Set the value and return the old value

Strlen

return string length

GetRange (SUBSTR)

String interception

Rename

Key renaming

$redis->rename(‘a‘,‘a1‘);    //成功返回true,键不存在时返回false,newkey已经存在其值会被覆盖;
Renamex

Key renaming, newkey exist without changing

$redis->renamex(‘a‘,‘a1‘//a1假设已经存在,返回0;
Append

String append

$redis->append(‘a‘,‘12345‘) ;//假设a不存在,就是普通的set操作
incr

Stored numeric value increased by 1

Incrby

Value Increment increment

$redis->incrby(‘a‘,10);
Decr

numeric value minus 1

Decrby

Digital value minus Decrement

Setbit Key Offset value

Set or clear the pointing position

Getbit Key Offset

Gets the bit on the specified offset

Type

Returns the key value type
The return values are in the following ways:

None (key does not exist) Int (0)
String String Int (1)
List List Int (3)
Set Collection Int (2)
Zset Ordered collection Int (4)
Hash Hash table Int (5)
Randomkey

Randomly returns a key

echo$redis->randomkey();   //假设没有key 返回false
Object

View objects

Move

Moving data

$redis->MOVE(‘a‘,1));   //把键a从当前数据库移到1里。假设key不存在。返回false; 目标数据库存在key时,返回false.
Hashhset key field value

Set hash value

Hsetnx key field value

Set value when not present

Hmset key field Value[field value ...]

Set multiple hash values

Hget key Field

Get hash field value

Hmget key field [field ...]

Get a number of domain values

Hgetall Key

Returns key all fields and values

Hdel key field [field ...]

Delete one or more domains

Hlen Key

Returns the number of fields in the key

Hexists key Field

See if the field exists for a given field in key

Hincrby key field Increment

Domain value add increment

Hkeys Key

Return to all fields

Hvals Key

Return key all values

Table Lpush key value[value ...] table header insert one or more values
$redis->lpush(‘a‘,1);$redis->lpush(‘a‘,2);
Lpushx Key value

Inserted when and only if key exists and is a list

Rpush Key value

Footer Insertion

Rpushx Key Valuelpop Key

Remove and return the key header element

Rpop Key

Remove and return the tail element of key

Blpop key [Key ...]

The blocked version number of timeout lpop. Assuming there are no elements to eject, the program waits.

Brpop key [Key ...]

Blocked version number of timeout Rpop

Llen Key

Returns the length of the list key

Lrange Key Start stop

Return interval

Lrem Key Count value

remove element with list value
Count>0 searches from the table header to the end of the table, removing the element with value values. Quantity is Count
Count<0 removes an element of value from the end of the table to the table header. Quantity is-count
Count=0 Removing an element of value

LSet Key index value

Set the value of key subscript index

LTrim Key Start stop

Trim the list. Delete out-of-band elements

Lindex Key Index

Returns the element labeled Index

Linsert Key

Insert

Rpoplpush Source Destination

pops up the last element. Inserting elements into the list destination as a table header

Brpoplpush Source Destination Timeout

Jam

Collection (set) Sadd key member [member ...]

Inserting elements into the collection

Srem Key member [member ...]

remove element

Smembers Key

Returning collection members

Sismember Key Member

Infer if member is a member of key

SCard Key

Returns the cardinality of the collection key

Smove Source Destination Member

Member elements move from source to destination

Spop Key

Removes and returns a random element in the collection

Srandmember

Returns a random element in the collection

Sinter key [Key ...]

Returning collection members

Sinterstore destination Key[key ...]

Returns the member to destination, assuming the target exists to overwrite

Sunion key [Key ...]

Return all members of a collection

Sunionstore destination Key[key ...]

Return member saved to destination

Sdiff Key[key ...]

Returns all members of a collection

Sdiffstore destination Key[key ...]

Results saved to destination

Sorted set ordered set Zadd key score Member[[score member] [score member] ...]

One or more member and their score are added to the ordered set key

Zrem key member [member ...]

To remove one or more members from an ordered set key

Zcard Key

Returns the cardinality of an ordered set key

Zcount Key min Max

Returns the member of the score value between Min and Max in an ordered set key

Zscore Key Member

Returns the score value of member member in an ordered set key

Zincreby Key Increment member

Increment of score value to member member

Zrange key start stop [Withscores]

Returns the specified interval member in the ordered set key, score from small to large

Zrevrange key start stop [Withscores]

Returns the member within the specified interval in key, score from large to small

Zrangebyscore

Returns the interval member, score from small to large

Zrevrangebyscore key Max min [withscores] [LIMIT offset Count]

Return to the honored member. Score from big to small

Zrank Key Member

Back to member rankings, score from small to large

Zrevrank Key Member

Member ranking, score descending

Zremrangebyrank Key Start stop

Removes a member. Subscript between start stop

Zremrangebyscore Key min Max

Remove members, score between Min and Max

Zinterstore destination Numkeys key [key ...] [WEIGHTS weight [weight ...] [AGGREGATE sum| min| MAX]

Calculating the intersection of ordered sets

Zunionstore

Calculating ordered set and set

Sorting sort sort Numeric sort
$redis->SORT(‘a‘);   //返回排序结果
Alpha-qualified Sort by character
$redis->SORT(‘a‘,array(‘ALPHA‘=>TRUE//返回排序结果
Limit limit return quantity
$redis->SORT(‘a‘,array(‘LIMIT‘=>array(0,5));
Sort limits positive order reverse
$redis->SORT(‘a‘,array(‘SORT‘=>‘DESC‘));
by external sort
$redis->SORT(‘a‘,array(‘BY‘=>‘‘));

There are many other ways to use the sorting, which are not listed here.

Life cycle ttl, expire, expireat
$redis -Set(' A ',12345);$redis -Expire' A ', -);//units per second$redis -Expireat (' A ',' 1435152916 ');//Accept time stampEcho$redis -Get' A ');//Assuming it has expired, return falseEcho$redis -ttl' A ');//Returns the remaining time (in seconds). Assume that the expire value is not assigned. Returns-1. The assumed key does not exist. Also returns-1
Persit to remove the time to live
$redis->persist(‘a‘);
Database Operations Select a database
$redis->SELECT(0);    //默认就是数据库0 
Clear
$redis->//删除全部数据库$redis->flushdb();  //删除当前数据库全部key

Redis Learning Note Five frequently uses PHP functions

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.