Redis learning Notes: The php function PHPRedis is often used here: blog. csdn. netxundharticledetails46288277 key value operation $ redisnewRedis (); $ redis-& gt; connect (127.0.0.1, 6379); parameter: connect (host, port, timeout Redis study notes five common php functions
Install PHPRedis here:
Http://blog.csdn.net/xundh/article/details/46288277
Key-value operation
$ Redis = new Redis (); $ redis-> connect ('2017. 0.0.1 ', 6379); // parameter: connect (host, port, timeout) // timeout can be empty in redis. timeout in conf is 300 by default. // pconnect does not close the connection.
Key
// Write value $ redis-> set ($ work-> uid, $ workString); $ redis-> setnx ($ work-> uid, $ workString ); // if the key does not exist, it is assigned $ redis-> setex ($ a, 50, 1); // after the range value is set, the ttl is set to 50 $ redis-> setrange (key, offset, value); // get the value $ redis-> get ($ work-> uid); // delete key $ redis-> del ($ work-> uid ); var_dump ($ redis-> get ($ work-> uid); // returns bool (false) // 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); // write data only when the key does not exist, but either completely or completely. // Read mset $ array_mget = ['A', 'B']; $ redis-> mget ($ arram_mget ); // delete multiple keys $ redis-> del ($ array_mget );
Keys
// Read keys $ array_mset_keys = ['ABC' => 1, 'bcd' => 2, 'Cde' => 3]; $ redis-> keys ('* a *'); // return ['a' => 1]; $ redis-> keys ('B ?? '); // Return [' B '=> 2]; $ redis-> keys (' * '); // return all keys
Getset key value
Set the value and return the old value
Strlen
Returns the string length.
Getrange (substr)
String truncation
Rename
Key rename
$ Redis-> rename ('A', 'A1'); // if the key does not exist, true is returned. if the key does not exist, the value of newkey is overwritten;
Renamex
Key name, not changed when newkey exists
$ Redis-> renamex ('A', 'A1'); // If a1 already exists, return 0;
Append
String appending
$ Redis-> append ('A', '20140901'); // If a does not exist, it is a normal set operation.
Incr
Increase the stored numeric value by 1
Incrby
Value increase increment
$redis->incrby('a',10);
Decr
Number minus 1
Decrby
Numeric value minus decrement
Setbit key offset value
Set or clear positioning
Getbit key offset
Obtains the bit at the specified offset.
Type
Return key value type
The return values include:
None |
(Key does not exist) |
Int (0) |
String |
String |
Int (1) |
List |
List |
Int (3) |
Set |
Set |
Int (2) |
Zset |
Ordered set |
Int (4) |
Hash |
Hash table |
Int (5) |
Randomkey
Returns a random key.
Echo $ redis-> randomkey (); // if no key is returned, false is returned.
Object
View object
Move
Mobile data
$ Redis-> MOVE ('A', 1); // MOVE key a from the current database to 1. If the key does not exist, false is returned. if the target database has a key, false is returned.
Hashhset key field value
Set the hash value
Hsetnx key field value
Set the value if it does not exist.
Hmset key field value [field value…]
Set multiple hash values
Hget key field
Obtain the hash field value
HMET key field [field…]
Obtain several domain values
Hgetall key
Returns all key fields and values.
Hdel key field [field…]
Deletes one or more domains.
Hlen key
Returns the number of domains in the key.
Hexists key field
Check whether the field of the given field in the key exists.
Hincrby key field increment
Increment added to the domain value
Hkeys key
Returns all domains.
Hvals key
Returns all key values.
Table lpush key value [value…] Insert one or more values in the header
$redis->lpush('a',1);$redis->lpush('a',2);
Lpushx key value
Insert only when the key exists and is a list
Rpush key value
Insert at the end of the table
Rpushx key valuelpop key
Remove and return the key header element
Rpop key
Remove and return the end element of the key
Blpop key [key…]
Timeout lpop blocking version. if there are no elements available for pop-up, the program will wait.
Brpop key [key…]
Timeout rpop blocking version
Llen key
Returns the length of the list key.
Lrange key start stop
Return interval
Lrem key count value
Remove an element whose list value is value
Count> 0: search from the header to the end of the table, remove the element with the value of value, the number is count
Count <0 removes the element with the value from the end of the table to the header, and the number is-count.
Count = 0 remove the element with the value
Lset key index value
Set the key subscript to the index value
Ltrim key start stop
Trim the list and delete out-of-range elements
Lindex key index
Returns the index element.
Linsert key
Insert
Rpoplpush source destination
The last element is displayed, and the element is inserted into the destination list as the header.
Brpoplpush source destination timeout
Blocking
Set sadd key member [member…]
Insert elements into the set
Srem key member [member…]
Remove element
Smembers key
Returns a set member.
Sismember key member
Judge whether member is a key member
Scard key
Returns the base number of the set key.
Smove source destination member
Move the member element from source to destination
Spop key
Removes and returns a random element from the set.
Srandmember
Returns a random element in the set.
Sinter key [key…]
Returns a set member.
Sinterstore destination key [key…]
Returns the members to destination. if the target exists, it overwrites
Sunion key [key…]
Returns all members of the set.
Sunionstore destination key [key…]
Save the returned members to destination.
Sdiff key [key…]
Returns all members of a set.
Sdiffstore destination key [key…]
Save the result to destination.
Sorted Set ordered Set zadd key score member [[score member] [score member]…]
One or more member and its scores are added to the sorted set key.
Zrem key member [member…]
Removes one or more members from the sorted set key.
Zcard key
Returns the base number of the sorted set key.
Zcount key min max
Returns the members of the sorted set key whose score value is between min and max.
Zscore key member
Returns the score value of the member in the sorted set key.
Zincreby key increment member
Score increment for member members
Zrange key start stop [WITHSCORES]
Returns the specified range member in the sorted set key, and the score ranges from small to large.
Zrevrange key start stop [WITHSCORES]
Returns the members in the specified range in the key. The score ranges from large to small.
Zrangebyscore
Returns a range Member. The score ranges from small to large.
Zrevrangebyscore key max min [WITHSCORES] [LIMIT offset count]
Back to the honorable Member, score from large to small
Zrank key member
Returns the member ranking. The score ranges from small to large.
Zrevrank key member
Member ranking, score decreasing
ZREMRANGEBYRANK key start stop
Remove members. the subscript is between start stop.
ZREMRANGEBYSCORE key min max
Remove a member. the score is between min and max.
ZINTERSTORE destination numkeys key [key…] [WEIGHTS weight [weight…] [Aggregate sum | MIN | MAX]
Calculate the intersection of ordered sets
ZUNIONSTORE
Calculate the union of sorted sets
Sort value sorting
$ Redis-> SORT ('A'); // return the sorting result
Alpha is sorted by characters.
$ Redis-> SORT ('A', array ('alpha' => TRUE); // return the sorting result
Limit the number of returned results
$redis->SORT('a',array('LIMIT'=>array(0,5));
SORT limit forward and reverse order
$redis->SORT('a',array('SORT'=>'DESC'));
By external sorting
$redis->SORT('a',array('BY'=>''));
Sorting is used in many other ways. I will not detail them here.
Ttl, expire, expireat
$ Redis-> set ('A', 12345); $ redis-> expire ('A', 30); // unit: seconds $ redis-> expireat ('A ', '20140901'); // accept the timestamp echo $ redis-> get ('A'); // if it has expired, returns falseecho $ redis-> ttl ('A'); // returns the remaining time (seconds ). If the expire value is not assigned,-1 is returned. if the key does not exist,-1 is returned.
Persit remove survival time
$redis->persist('a');
Select database for database operations
$ Redis-> SELECT (0); // The default value is database 0.
Clear
$ Redis-> FLUSHALL (); // delete all databases $ redis-> flushdb (); // delete all keys of the current database