a common command
keys *
: Traversing and printing all keys, O(n), followed by regular expressions, generally not used in production environments, can be used to hot standby slave nodesdbsize
: Calculate the total number of keys, O(1)exists key
: Check if the key exists, O(1), return 1 if it exists, and return 0 if it does not existdel key
: Delete the specified key-value, O(1), you can delete multiple at a time, return 1 if the deletion is successful, and return 0 if the deletion failsexpire key seconds
: key expires in seconds seconds, O(1)ttl key
: Check the remaining expiration time of the key, O(1)persist key
: Remove the expiration time of the key, O(1)type key
: return the type of key, O(1)
Two string type (string)
Structure : the key is a string, and the value can be a string, a numeric string, or a json string. Essentially, the value is all binary, and the maximum cannot exceed 512MB.
Scenario : cache counter distributed lock
API
get key
: Get the value corresponding to the key, O(1)set key
: set key-value, O(1)del key
: Delete key-value, O(1)incr key
: The key is incremented by 1, if the key does not exist, get(key)=1 after the increment, O(1)decr key
: The key is decremented by 1. If the key does not exist, get(key)=-1 after decrement, O(1)incrby key k
: key auto-increment k, if key does not exist, get(key)=k after auto-increment, O(1)decrby key k
: The key is decremented by k, if the key does not exist, get(key)=-k after decrement, O(1)set key value
: Regardless of whether the key exists or not, it is set, O(1)setnx key value
: key does not exist, only set, O(1)set key value xx
: The key is set only if it exists, O(1)mget key1 key2 key3...
: Get keys in batches, atomic operation, O(n)mset key1 value1 key2 value2 key3 value3...
: Set key-value in batches, O(n)getset key newvalue
: Set a new newvalue and return the old value, O(1)append key value
: Append value to old value, O(1)strlen key
: Returns the length of the string, O(1)incrbyfloat key 3.5
: Increase the value corresponding to the key to 3.5, O(1)getrange key start end
: Get all the values of the specified subscript of the string, O(1)setrange key index value
: Set all corresponding values of the specified subscript, O(1)
Three hash types (hash)
Structure : key-value, value type is a bunch of key-value (field-value), similar to "Mapmap" type.
Usage scenarios : For example, record the number of visits to each user's personal homepage
API : all start with "h"
hget key field
: Get the value of the field corresponding to the hash key, O(1)hset key field value
: Set the value of the field corresponding to the hash key, O(1)hdel key field
: Delete the value of the field corresponding to the hash key, O(1)hexists key field
: Determine whether the hash key has a field, O(1)hlen key
: Get the number of hash key fields, O(1)hgetall key
: Return hash key corresponding to all fields and values, O(n)hvals key
: Return the value of all fields corresponding to the hash key, O(n)hkeys key
: Return hash key corresponding to all fields, O(n)hsetnx key field value
: Set the value of the field corresponding to the hash key. If the field already exists, the setting fails, O(1)hincrby key field intCounter
: Set the value of the field corresponding to the hash key to auto-increment intCounter, O(1)hincrbyfloat key field floatCounter
: Set the value of the field corresponding to the hash key to auto-increment intCounter (floating point version), O(1)
Four list type (list)
Structure : key-elements, elements is a queue, 有序,可重复
, insert and pop up on the left and right sides
Usage scenario : message queue article list
API : all start with "l" and "r"
rpush key value1 value2 value3 ...
: Insert values from the right end of the list, O(1~n), the order after insertion is: ..., value1, value2, value3lpush key value1 value2 value3 ...
: Insert values from the left end of the list, O(1~n), the order after insertion is: value3, value2, value1, ...linsert key before|after value newValue
: insert newValue before|after the value specified by list, O(n)pop|rpop key
: Pop an item from the left | right side of the list, O(1)lrem key count value
: According to the count value, delete all items with equal value from the list, O(n)- count > 0, delete up to count items with equal value from left to right
- count < 0, delete up to -count items with equal value from right to left
- count = 0, delete all items with equal value
ltrim key start end
: Trim the list according to the index range, keep the list of start~end length, O(n)lrange key start end
: Get the list to specify all items, including end, O(n)lindex key index
: Get the item at the specified index of the list, O(n)llen key
: Get the length of the list, O(1)lset key index newValue
: Set the list to specify the index value as newValue, O(n)blpop key timeout
: lpop blocking version, timeout is blocking timeout, timeout=0 means never blocking, O(1)brpop key timeout
: rpop blocking version, timeout is the blocking timeout, timeout=0 means never blocking, O(1)
Development skills:
LRUSH + LPOP = Stack (stack)
LPUSH + RPOP = Queue (queue)
LPUSH + LTRIM = Capped Collection (limited collection)
LPUSH + BRPOP = Message Queue (message queue)
Five collection types (set)
Structure : key-elements, elements is a collection, 无序,不重复
, supports operations between collections
Scenarios : such as lottery system, microblogging likes, likes, dislikes, etc.
API : all start with "s"
sadd key element
: Add an element to the collection, if the element already exists, the addition fails, O(1)srem key element
: Remove the element in the collection key, O(1)scard key
: Calculate the collection size, O(1)sismember key element
: Determine whether there is an element element in the collection, O(1)srandmember key count
: Randomly select count elements from the collection, the elements will not be deleted, O(1)spop key
: Randomly pop an element from the collection, the element will be deleted, O(1)smembers key
: Get all the elements in the collection, O(1), use with caution, if the amount of data is large, it is easy to block
API between collections :
sdiff key1 key2
: Differencesinter key1 key2
: intersectionsunion key1 key2
: unionsdiff|sinter|sunion store key3
: Save the difference|intersection|union results to the key3 collection
Use scenarios between collections: such as common attention, common friends, etc.
Six ordered set type (zset)
Structure : key-values, values are a bunch of key-value (core-element), core 用于排序
, no repeating elements, orderly
usage scenario : leaderboard
API : all start with "z"
zadd key core1 element1 core2 element2
: Add elements, core can be repeated, element cannot be repeated O(logN)zrem key element
: delete element, O(1)zscore key element
: Returns the core of the element, O(1)zincrby key increScore clement
: Increase or decrease fraction of elements, O(1)zcard key
: Returns the total number of elements, O(1)zrange key start end [WITHSCORES]
: Returns the ascending elements [score] within the specified index range, O(log(n) + m), n is the number of elements, m is the number of elements to be obtainedzrangebyscore key minScore maxScore [WITHSCORES]
: Returns the ascending elements [score] of the specified range score, O(log(n) + m), n is the number of elements, m is the number of elements to be obtainedzcount key minScore maxScore
: Returns the number of elements in ascending order of the specified range score, O(log(n) + m), n is the number of elements, m is the number of elements to be obtainedzremrangebyrank|zremrangebyscore key start end
: Delete the ascending elements within the specified rank | range score, O(log(n) + m), n is the number of elements, m is the number of elements to be obtainedzrevrank
: reverse order from high to lowzrevrange
: Take range data from high to lowzrevrangebyscore
: Get results from high to low for a given scorezinterstore
: intersection