Redis data structures and commands
Data types supported by Redis
- STRING A STRING, integer, or floating point number
- LIST lists, where each node is a string
- SET unordered collector
- HASH Contains an unordered HASH of key and value pairs
- ZSET ordered set
Universal key-value operations
# get all key/value pair combined with regular expression keys pattern keys * # random access key randomkey # # the type of access to key type keys of judgment key whether there exists the key value of # delete key del keys name # modified key values Rename formerly known as the target of # will cover the nuptial renamenx formerly known as the target name # name not conflict was renamed mobile operators # key to other database open 16 # default database, 0-15 select server id # to switch to the specified database. TTL TTL TTL TTL TTL TTL TTL TTL TTL TTL TTL TTL TTL TTL TTL Pexpire key value name lifetime in seconds pexpire key value name lifetime in seconds pexpire key value name lifetime in secondsCopy the code
Redis string operation
# set the value of String #ex number of seconds ps number of milliseconds Set search WWW ex parameter #nx not available xx valid set search WWW nx # not available mset a aman b bold Set word hello setrange word 2?? #word will become he?? o setrange word 6 ! Getrange key 0 -4 # The last value is -1 # Get the old value, Set status sleep getSet status awake set num 10000 decr num = 1 incr num = 1 Decrby num 5 # number minus 5 incrby num 5 # number plus 5 32 set char A setbit char 2 1 #char = A setbit char 2 1 #char = A # char1 char2 char3Copy the code
Redis lists
Lpush character A rpush character B lpush character C # The end result is c, A, b # fetch the value lrange character from the list Lindex key index lindex character 0 # select * from lindex key index lindex character 0 # Select * from linkedtable where id = 1; select * from linkedtable where id = 1; select * from linkedtable where id = 1; Linsert key before a b # list becomes C, B, a # Data that pops up from the right side of a list is injected from the left side of another list # This operation itself is an atomic operation. Brpop (BLPOP) key timeout BRPOP key 5 # Wait for 5 seconds to popCopy the code
Redis set related commands (including linked list) set characteristics: disorder, unique
Semmbers key # Delete element srem key # randomly pop up and delete an element in the set spop key # randomly get one, Sismember key argument # move set element smove key1 key2 argument # move element from key1 to key2 Sinter key1 key2 key1 key2 result # Store the result of the intersection sunion key1 key2 sunionStore key1 key2 Result # will merge the result into resultCopy the code
The related instruction ordering of ordered sets in Redis: sorting by adding weight values to elements
Zrange key start end # start from 0, Zrangebyscore key value1 value2 zrangeByScore key value2 Zrangebyscore key value1 value2 limit 1 2 Zrevrank key value2 # delete element zremrangebyScore key min Max Zremrangebyrank key start end zremrangebyrank key start end Sum zadd Lisi 3 cat 5 dog 6 horse Zadd Wang 2 cat 6 dog 8 horse 1 donkey # lisi Wang Calculate the intersection and put the result into result # and perform sum, Max and min operations on the weight. Min is the smallest. Zinterstore result 2 lisi wang aggregate sum #weights 2 lisi zinterstore result 2 lisi wang aggregate weights 2 1 sumCopy the code
Hash in Redis is a management array in the form of key-value pairs
Hset user name lisi hset user aga q0 hmset user1 name Wang age 10 hgetall user hget user Name # for an element of a domain hmget user name age # # for an element of multiple domains to delete hdel user age # # for an element of a domain to check how many domain hlen hexists user # whether domain exists Hincrby user age 1 # age increase hincrByFloat user age 0.5 # float increase hdecrby user age # decrease # View hkeys key # Get all fields Hvals key # Get the values of all fieldsCopy the code