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 increased increment
Hkeys Key
Return all domains
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 of timeout Lpop, and if there are no elements available to eject, the program waits.
Brpop key [Key ...]
Blocked version 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 Search from the table header to the footer, removing the element with value as Count
Count<0 the header from the end of the table, removes the element with value values of-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 the outside of the interval element
Lindex Key Index
Returns the element labeled Index
Linsert Key
Insert
Rpoplpush Source Destination
pops up the last element and inserts the element into the list destination as a header
Brpoplpush Source Destination Timeout
Blocking
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
Determine 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 the destination, overwriting if the target exists
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 tribute 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
Remove 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 uses of sorting, which are not listed here in detail.
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 ');//If it has expired, return falseEcho$redis -ttl' A ');//Returns the remaining time (in seconds). Returns 1 if no expire value is assigned, or 1 if the key does not exist
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 commonly used PHP functions