一.通用key操作命令
KEYS pattern
summary: Find all keys matching the given pattern
RANDOMKEY -
summary: Return a random key from the keyspace
EXISTS key [key ...]
summary: Determine if a key exists
DEL key [key ...]
summary: Delete a key
FLUSHDB [ASYNC]
summary: Remove all keys from the current database
RENAME key newkey
summary: Rename a key
RENAMENX key newkey
summary: Rename a key, only if the new key does not exist
SELECT index
summary: Change the selected database for the current connection
EXPIRE key seconds
summary: Set a key's time to live in seconds
PEXPIRE key milliseconds
summary: Set a key's time to live in milliseconds
TTL key
summary: Get the time to live for a key
PTTL key
summary: Get the time to live for a key in milliseconds
PERSIST key
summary: Remove the expiration from a key
二.字符串类型操作
SET key value [expiration EX seconds|PX milliseconds] [NX(key不存在时成功)|XX(key存在时成功)]
summary: Set the string value of a key
MSET key value [key value ...]
summary: Set multiple keys to multiple values
SETRANGE key offset value
summary: Overwrite part of a string at key starting at the specified offset
SETNX key value
summary: Set the value of a key, only if the key does not exist
GET key
summary: Get the value of a key
MGET key [key ...]
summary: Get the values of all the given keys
GETRANGE key start end
summary: Get a substring of the string stored at a key
APPEND key value
summary: Append a value to a key
GETSET key value
summary: Set the string value of a key and return its old value
INCR key
summary: Increment the integer value of a key by one
说明:秒杀场景
INCRBY key increment
summary: Increment the integer value of a key by the given amount
DECR key
summary: Decrement the integer value of a key by one
DECRBY key decrement
summary: Decrement the integer value of a key by the given number
SETBIT key offset value
summary: Sets or clears the bit at offset in the string value stored at key
说明:位操作,offset 为2^23-1,推出字符串最大512M
BITOP operation destkey key [key ...]
summary: Perform bitwise operations between strings
说明:bitop or destkey key
BITCOUNT key [start end]
summary: Count set bits in a string
BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
summary: Perform arbitrary bitfield integer operations on strings
BITPOS key bit [start] [end]
summary: Find first bit set or clear in a string
三.链表相关命令
LPUSH key value [value ...]
summary: Prepend one or multiple values to a list
RPUSH key value [value ...]
summary: Append one or multiple values to a list
LRANGE key start stop
summary: Get a range of elements from a list
说明:取所有lrange key 0 -1
LPOP key
summary: Remove and get the first element in a list
RPOP key
summary: Remove and get the last element in a list
LREM key count value
summary: Remove elements from a list
LTRIM key start stop
summary: Trim a list to the specified range
LLEN key
summary: Get the length of a list
LINSERT key BEFORE|AFTER pivot value
summary: Insert an element before or after another element in a list
RPOPLPUSH source destination
summary: Remove the last element in a list, prepend it to another list and return it
说明:场景 task|bak 双链表完成安全队列
BRPOP key [key ...] timeout
summary: Remove and get the last element in a list, or block until one is available
BLPOP key [key ...] timeout
summary: Remove and get the first element in a list, or block until one is available
说明:brpop和blpop使用场景,长轮询和在线聊天
四.set结构及命令详解
SADD key member [member ...]
summary: Add one or more members to a set
SMEMBERS key
summary: Get all the members in a set
SREM key member [member ...]
summary: Remove one or more members from a set
SPOP key [count]
summary: Remove and return one or multiple random members from a set
SRANDMEMBER key [count]
summary: Get one or multiple random members from a set
SISMEMBER key member
summary: Determine if a given value is a member of a set
SMOVE source destination member
summary: Move a member from one set to another
SINTER key [key ...]
summary: Intersect multiple sets
SUNION key [key ...]
summary: Add multiple sets
SDIFF key [key ...]
summary: Subtract multiple sets
五.order set结构及命令
ZADD key [NX|XX] [CH] [INCR] score member [score member ...]
summary: Add one or more members to a sorted set, or update its score if it already exists
ZRANGE key start stop [WITHSCORES]
summary: Return a range of members in a sorted set, by index
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
summary: Return a range of members in a sorted set, by score
ZRANK key member
summary: Determine the index of a member in a sorted set
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]
summary: Intersect multiple sorted sets and store the resulting sorted set in a new key
ZCARD key
summary: Get the number of members in a sorted set
ZCOUNT key min max
summary: Count the members in a sorted set with scores within the given values
ZINCRBY key increment member
summary: Increment the score of a member in a sorted set
六.hash结构及命令
HSET key field value
summary: Set the string value of a hash field
HGET key field
summary: Get the value of a hash field
HDEL key field [field ...]
summary: Delete one or more hash fields