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);
Parameter: Connect (host,port,timeout)
//timeout can be empty, in redis.conf timeout
The default of//pconnect will not actively shut down the connection
Key
Write Value
$redis->set ($work->uid, $workString);
$redis->setnx ($work->uid, $workString); Key does not exist is the assigned value
$redis->setex ($a, 50,1); The TTL is set to
$redis->setrange (key,offset,value) after the range setting value;
Gets the value
$redis->get ($work->uid);
Delete key
$redis->del ($work->uid);
Var_dump ($redis->get ($work->uid)); Returns whether bool (FALSE)
//Key exists
if (! $redis->exists (' key '))
var_dump ($redis->del (' key ')); return int (0)
mset multiple key operations
Set Mset
$array _mset=[' A ' =>1, ' B ' =>2];
$redis->mset ($array);
$redis->msetnx ($array); Key is written when it does not exist, but it is either written all at once or not written at all.
//Read mset
$array _mget=[' A ', ' B '];
$redis->mget ($arram _mget);
Delete multiple key
$redis->del ($array _mget);
Keys
Reading keys
$array _mset_keys=[' abc ' =>1, ' BCD ' =>2, ' CDE ' =>3];
$redis->keys (' *a* '); return to [' A ' =>1];
$redis->keys (' B.?? '); Back [' B ' =>2];
$redis->keys (' * '); Return all 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 '); Returns true successfully, returns False,newkey already exists when the key does not exist and its value is overwritten;
Renamex
Key renamed, Newkey exist without change
$redis->renamex (' A ', ' A1 '); A1 If already exists, returns 0;
Append
String append
$redis->append (' A ', ' 12345 ');/If a does not exist, it is a normal set operation
incr
The stored digital value increases by 1 Incrby
Value Increase increment
$redis->incrby (' A ', 10);
DECR
Digital value minus 1 decrby
Digital value minus decrement setbit key offset value
Set or clear the positioning getbit key offset
Gets the bit type on the specified offset
Return key value type
The return value has the following types:
None |
(key does not exist) |
Int (0) |
String |
String |
Int (1) |
List |
List |
Int (3) |
Set |
Collection |
Int (2) |
Zset |
Ordered set |
Int (4) |
Hash |
Hash table |
Int (5) |
Randomkey
Return a key at random
echo $redis->randomkey (); If no key returns false
Object
View Object move
Moving data
$redis->move (' A ', 1)); Move the key a from the current database to 1. Returns False if key does not exist; Returns False when a key exists for the target database.
Hash
hset key field value
Set hash value hsetnx key field value
Setting value hmset key field Value[field value ...]
Set multiple hash values hget key field
Gets the hash field value hmget key field [field ...]
Get several domain values hgetall key
Returns key all fields and values hdel key field [field ...]
Delete one or more domains Hlen key
Return key field number hexists key field
View whether the field of a given field in the key exists hincrby key field increment
field value increase increment Hkeys key
Return all fields hvals key
Return key all Values table lpush key value[value ...] table header inserts one or more values
$redis->lpush (' A ', 1);
$redis->lpush (' A ', 2);
lpushx Key Value
Insert Rpush key value when and only if key exists and is a list
Footer Insert rpushx key value lpop key
Remove and return key header element Rpop key
Remove and return the tail element of key Blpop key [key ...]
Timeout Lpop blocked version, the program waits if there is no element to eject. brpop key [key ...]
Timeout Rpop blocked version llen key
Returns the length of the list key lrange key start Stop
Return interval lrem key count value
To remove an element with a value of the list
Count>0 searches from the header to the footer, removing the element with value, Count
Count<0 from the end of the table to the header, remove the element with value =-count
Count=0 removes the element with value of LSet key index value
Set key subscript index value ltrim key start Stop
Trim the list, delete the interval outside the element lindex key index
Returns the element Linsert key that is subscript index
Insert rpoplpush Source Destination
pops up the last element and inserts the element into the list destination as the header Brpoplpush source destination Timeout
Blocking Collection (set) sadd key member [member ...]
Insert element into collection Srem key member [member ...]
remove element smembers key
Returns the collection member sismember key
To determine if member is the key members SCard key
Returns the cardinality of the collection key smove source Destination member
Member element moves 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 ...]
Returns the collection member Sinterstore destination Key[key ...]
Returns the member to destination, if the target exists then overwrites sunion key [key ...]
Returns all members of the collection Sunionstore destination Key[key ...]
Return member save to destination Sdiff Key[key ...]
Returns all members of a collection Sdiffstore destination Key[key ...]
The results are saved to the destination Sorted set ordered set zadd key score Member[[score member] [score member] ...]
One or more member and its score are added to the ordered set key in Zrem key [...]
Remove one or more members in an ordered set key Zcard key
Returns the cardinality of the ordered set key zcount key min Max
Returns the member of the score value between Min and Max zscore key in the ordered set key
Returns the score value Zincreby key increment member of the ordered set key
Increment of score value for member members 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 members within a specified interval in the key, score from large to small zrangebyscore
Return interval member, score from small to large zrevrangebyscore key Max min [withscores] [LIMIT offset count]
Return to the members of respect, score from big to small Zrank key member
Return to member ranking, score from small to large Zrevrank key member
Member ranking, score descending Zremrangebyrank key start Stop
Remove member, subscript zremrangebyscore key min Max between start stop
Remove the member, score between Min and Max zinterstore destination Numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE sum| min| MAX]
Compute ordered set intersection Zunionstore
To compute ordered sets and sort sorted values
$redis->sort (' a '); Return sort Result
Alpha qualification sorted by character
$redis->sort (' A ', Array (' ALPHA ' =>true)); Return sort Result
Limit qualified return quantity
$redis->sort (' A ', Array (' LIMIT ' =>array (0,5));
sort limit positive sequence reverse
$redis->sort (' A ', Array (' SORT ' => ' DESC '));
By External sort
$redis->sort (' A ', Array (' by ' => '));
Sorting other uses more, here no longer detailed list. Life cycle ttl, expire, Expireat
$redis->set (' A ', 12345);
$redis->expire (' a ', 30); Unit seconds
$redis->expireat (' A ', ' 1435152916 ');//Receive timestamp
echo $redis->get (' a '); Returns false
echo $redis->ttl (' a ') if it has expired; Returns the time remaining (in seconds). Returns-1 if the expire value is not assigned, or 1 if the key does not exist
Persit to remove live time
$redis->persist (' a ');
Database Operations
Select Database
$redis->select (0);
Clear
$redis->flushall (); Delete all databases
$redis->flushdb (); Delete all keys in current database