redis命令大全

1. 全局

命令命令说明备注
keys * 或 keys “*”(慎用)- 查看所有key
keys “employee*”- 查看匹配前缀的keys
flushdb- 清空redis
randomkey- 随机取出一个key
type key- 查看key的类型
dbsize- 查看数据库中key的数量
info- 查看服务器信息
monitor- 查看redis正在做什么(只能在测试环境下使用)
slowlog get slowlog get 10- 查看日志
COPY source destination [DB destination-db] [REPLACE]
summary: Copies the value of a key to a new key.
  since: 6.2.0

DEL key [key ...]
summary: Deletes one or more keys.
  since: 1.0.0

DUMP key
summary: Returns a serialized representation of the value stored at a key.
  since: 2.6.0

EXISTS key [key ...]
summary: Determines whether one or more keys exist.
  since: 1.0.0

  EXPIRE key seconds [NX|XX|GT|LT]
summary: Sets the expiration time of a key in seconds.
  since: 1.0.0

EXPIREAT key unix-time-seconds [NX|XX|GT|LT]
summary: Sets the expiration time of a key to a Unix timestamp.
  since: 1.2.0

EXPIRETIME key
summary: Returns the expiration time of a key as a Unix timestamp.
  since: 7.0.0

KEYS pattern
summary: Returns all key names that match a pattern.
  since: 1.0.0

MIGRATE host port key|"" destination-db timeout [COPY] [REPLACE] [AUTH password|AUTH2 username password] [KEYS key [key ...]]
summary: Atomically transfers a key from one Redis instance to another.
  since: 2.6.0

MOVE key db
summary: Moves a key to another database.
  since: 1.0.0

OBJECT (null)
summary: A container for object introspection commands.
  since: 2.2.3

OBJECT ENCODING key
summary: Returns the internal encoding of a Redis object.
  since: 2.2.3

OBJECT FREQ key
summary: Returns the logarithmic access frequency counter of a Redis object.
  since: 4.0.0

OBJECT HELP (null)
summary: Returns helpful text about the different subcommands.
  since: 6.2.0

  OBJECT IDLETIME key
summary: Returns the time since the last access to a Redis object.
  since: 2.2.3

OBJECT REFCOUNT key
summary: Returns the reference count of a value of a key.
  since: 2.2.3

PERSIST key
summary: Removes the expiration time of a key.
  since: 2.2.0

PEXPIRE key milliseconds [NX|XX|GT|LT]
summary: Sets the expiration time of a key in milliseconds.
  since: 2.6.0

PEXPIREAT key unix-time-milliseconds [NX|XX|GT|LT]
summary: Sets the expiration time of a key to a Unix milliseconds timestamp.
  since: 2.6.0

  PEXPIRETIME key
summary: Returns the expiration time of a key as a Unix milliseconds timestamp.
  since: 7.0.0

  PTTL key
summary: Returns the expiration time in milliseconds of a key.
  since: 2.6.0

RANDOMKEY (null)
summary: Returns a random key name from the database.
  since: 1.0.0

RENAME key newkey
summary: Renames a key and overwrites the destination.
  since: 1.0.0

RENAMENX key newkey
summary: Renames a key only when the target key name doesn't exist.
  since: 1.0.0

  RESTORE key ttl serialized-value [REPLACE] [ABSTTL] [IDLETIME seconds] [FREQ frequency]
summary: Creates a key from the serialized representation of a value.
  since: 2.6.0

SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]
summary: Iterates over the key names in the database.
  since: 2.8.0

  SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]
  summary: Sorts the elements in a list, a set, or a sorted set, optionally storing the result.
  since: 1.0.0

  SORT_RO key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA]
  summary: Returns the sorted elements of a list, a set, or a sorted set.
  since: 7.0.0

  TOUCH key [key ...]
  summary: Returns the number of existing keys out of those specified after updating the time they were last accessed.
  since: 3.2.1

  TTL key
  summary: Returns the expiration time in seconds of a key.
  since: 1.0.0

  TYPE key
  summary: Determines the type of value stored at a key.
  since: 1.0.0

  UNLINK key [key ...]
  summary: Asynchronously deletes one or more keys.
  since: 4.0.0

  WAIT numreplicas timeout
  summary: Blocks until the asynchronous replication of all preceding write commands sent by the connection is completed.
  since: 3.0.0

  WAITAOF numlocal numreplicas timeout
  summary: Blocks until all of the preceding write commands sent by the connection are written to the append-only file of the master and/or replicas.
  since: 7.2.0

2. string

img

APPEND key value
  summary: Appends a string to the value of a key. Creates the key if it doesn't exist.
  since: 2.0.0

  DECR key
  summary: Decrements the integer value of a key by one. Uses 0 as initial value if the key doesn't exist.
  since: 1.0.0

  DECRBY key decrement
  summary: Decrements a number from the integer value of a key. Uses 0 as initial value if the key doesn't exist.
  since: 1.0.0

  GET key
  summary: Returns the string value of a key.
  since: 1.0.0

  GETDEL key
  summary: Returns the string value of a key after deleting the key.
  since: 6.2.0

  GETEX key [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|PERSIST]
  summary: Returns the string value of a key after setting its expiration time.
  since: 6.2.0

  GETRANGE key start end
  summary: Returns a substring of the string stored at a key.
  since: 2.4.0

  GETSET key value
  summary: Returns the previous string value of a key after setting it to a new value.
  since: 1.0.0

  INCR key
  summary: Increments the integer value of a key by one. Uses 0 as initial value if the key doesn't exist.
  since: 1.0.0

  INCRBY key increment
  summary: Increments the integer value of a key by a number. Uses 0 as initial value if the key doesn't exist.
  since: 1.0.0

  INCRBYFLOAT key increment
  summary: Increment the floating point value of a key by a number. Uses 0 as initial value if the key doesn't exist.
  since: 2.6.0

  LCS key1 key2 [LEN] [IDX] [MINMATCHLEN min-match-len] [WITHMATCHLEN]
  summary: Finds the longest common substring.
  since: 7.0.0

  MGET key [key ...]
  summary: Atomically returns the string values of one or more keys.
  since: 1.0.0

  MSET key value [key value ...]
  summary: Atomically creates or modifies the string values of one or more keys.
  since: 1.0.1

  MSETNX key value [key value ...]
  summary: Atomically modifies the string values of one or more keys only when all keys don't exist.
  since: 1.0.1

  PSETEX key milliseconds value
  summary: Sets both string value and expiration time in milliseconds of a key. The key is created if it doesn't exist.
  since: 2.6.0

  SET key value [NX|XX] [GET] [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|KEEPTTL]
  summary: Sets the string value of a key, ignoring its type. The key is created if it doesn't exist.
  since: 1.0.0

  SETEX key seconds value
  summary: Sets the string value and expiration time of a key. Creates the key if it doesn't exist.
  since: 2.0.0

  SETNX key value
  summary: Set the string value of a key only when the key doesn't exist.
  since: 1.0.0

  SETRANGE key offset value
  summary: Overwrites a part of a string value with another by an offset. Creates the key if it doesn't exist.
  since: 2.2.0

  STRLEN key
  summary: Returns the length of a string value.
  since: 2.2.0

  SUBSTR key start end
  summary: Returns a substring from a string value.
  since: 1.0.0
1. APPEND key value 
  1.1. 摘要: 将字符串追加到键的值中。如果键不存在,则创建该键。 
  1.2. 自版本: 2.0.0
2. DECR key 
  2.1. 摘要: 将键的整数值减少一。如果键不存在,则将其初始值设为 02.2. 自版本: 1.0.0
3. DECRBY key decrement 
  3.1. 摘要: 从键的整数值中减去一个数字。如果键不存在,则将其初始值设为 03.2. 自版本: 1.0.0
4. GET key 
  4.1. 摘要: 返回键的字符串值。 
  4.2. 自版本: 1.0.0
5. GETDEL key 
  5.1. 摘要: 删除键后返回键的字符串值。 
  5.2. 自版本: 6.2.0
6. GETEX key [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|PERSIST] 
  6.1. 摘要: 设置键的过期时间后,返回键的字符串值。 自版本: 6.2.0
7. GETRANGE key start end 
  7.1. 摘要: 返回存储在键中的字符串的子字符串。 自版本: 2.4.0
8. GETSET key value 
  8.1. 摘要: 设置键的新值后返回键的先前字符串值。 自版本: 1.0.0
9. INCR key 
  9.1. 摘要: 将键的整数值增加一。如果键不存在,则将其初始值设为 0。 自版本: 1.0.0
10. INCRBY key increment 
  10.1. 摘要: 将键的整数值增加一个数字。如果键不存在,则将其初始值设为 0。 自版本: 1.0.0
11. INCRBYFLOAT key increment 
  11.1. 摘要: 将键的浮点数值增加一个数字。如果键不存在,则将其初始值设为 0。 自版本: 2.6.0
12. LCS key1 key2 [LEN] [IDX] [MINMATCHLEN min-match-len] [WITHMATCHLEN] 
  12.1. 摘要: 查找最长公共子串。 自版本: 7.0.0
13. MGET key [key ...] 
  13.1. 摘要: 原子性地返回一个或多个键的字符串值。 自版本: 1.0.0
14. MSET key value [key value ...] 
  14.1. 摘要: 原子性地创建或修改一个或多个键的字符串值。 自版本: 1.0.1
15. MSETNX key value [key value ...] 
  15.1. 摘要: 仅当所有键都不存在时,原子性地修改一个或多个键的字符串值。 自版本: 1.0.1
16. PSETEX key milliseconds value 
  16.1. 摘要: 设置键的字符串值和过期时间(毫秒)。如果键不存在,则创建该键。 自版本: 2.6.0
17. SET key value [NX|XX] [GET] [EX seconds|PX milliseconds|EXAT unix-time-seconds|PXAT unix-time-milliseconds|KEEPTTL] 
  17.1. 摘要: 设置键的字符串值,忽略其类型。如果键不存在,则创建该键。 自版本: 1.0.0
18. SETEX key seconds value 
  18.1. 摘要: 设置键的字符串值和过期时间(秒)。如果键不存在,则创建该键。 自版本: 2.0.0
19. SETNX key value 
  19.1. 摘要: 仅当键不存在时,设置键的字符串值。 自版本: 1.0.0
20. SETRANGE key offset value 
  20.1. 摘要: 通过偏移量覆盖字符串值的一部分。如果键不存在,则创建该键。 自版本: 2.2.0
21. STRLEN key 
  21.1. 摘要: 返回字符串值的长度。 自版本: 2.2.0
22. SUBSTR key start end 
  22.1. 摘要: 从字符串值中返回子字符串。 自版本: 1.0.0

3. list

  BLMOVE source destination LEFT|RIGHT LEFT|RIGHT timeout
  summary: Pops an element from a list, pushes it to another list and returns it. Blocks until an element is available otherwise. Deletes the list if the last element was moved.
  since: 6.2.0

  BLMPOP timeout numkeys key [key ...] LEFT|RIGHT [COUNT count]
  summary: Pops the first element from one of multiple lists. Blocks until an element is available otherwise. Deletes the list if the last element was popped.
  since: 7.0.0

  BLPOP key [key ...] timeout
  summary: Removes and returns the first element in a list. Blocks until an element is available otherwise. Deletes the list if the last element was popped.
  since: 2.0.0

  BRPOP key [key ...] timeout
  summary: Removes and returns the last element in a list. Blocks until an element is available otherwise. Deletes the list if the last element was popped.
  since: 2.0.0

  BRPOPLPUSH source destination timeout
  summary: Pops an element from a list, pushes it to another list and returns it. Block until an element is available otherwise. Deletes the list if the last element was popped.
  since: 2.2.0

  LINDEX key index
  summary: Returns an element from a list by its index.
  since: 1.0.0

  LINSERT key BEFORE|AFTER pivot element
  summary: Inserts an element before or after another element in a list.
  since: 2.2.0

  LLEN key
  summary: Returns the length of a list.
  since: 1.0.0

  LMOVE source destination LEFT|RIGHT LEFT|RIGHT
  summary: Returns an element after popping it from one list and pushing it to another. Deletes the list if the last element was moved.
  since: 6.2.0

  LMPOP numkeys key [key ...] LEFT|RIGHT [COUNT count]
  summary: Returns multiple elements from a list after removing them. Deletes the list if the last element was popped.
  since: 7.0.0

  LPOP key [count]
  summary: Returns the first elements in a list after removing it. Deletes the list if the last element was popped.
  since: 1.0.0

  LPOS key element [RANK rank] [COUNT num-matches] [MAXLEN len]
  summary: Returns the index of matching elements in a list.
  since: 6.0.6

  LPUSH key element [element ...]
  summary: Prepends one or more elements to a list. Creates the key if it doesn't exist.
  since: 1.0.0

  LPUSHX key element [element ...]
  summary: Prepends one or more elements to a list only when the list exists.
  since: 2.2.0

  LRANGE key start stop
  summary: Returns a range of elements from a list.
  since: 1.0.0

  LREM key count element
  summary: Removes elements from a list. Deletes the list if the last element was removed.
  since: 1.0.0

  LSET key index element
  summary: Sets the value of an element in a list by its index.
  since: 1.0.0

  LTRIM key start stop
  summary: Removes elements from both ends a list. Deletes the list if all elements were trimmed.
  since: 1.0.0

  RPOP key [count]
  summary: Returns and removes the last elements of a list. Deletes the list if the last element was popped.
  since: 1.0.0

  RPOPLPUSH source destination
  summary: Returns the last element of a list after removing and pushing it to another list. Deletes the list if the last element was popped.
  since: 1.2.0

  RPUSH key element [element ...]
  summary: Appends one or more elements to a list. Creates the key if it doesn't exist.
  since: 1.0.0

  RPUSHX key element [element ...]
  summary: Appends an element to a list only when the list exists.
  since: 2.2.0

4. hash

HDEL key field [field ...]
  summary: Deletes one or more fields and their values from a hash. Deletes the hash if no fields remain.
  since: 2.0.0

  HEXISTS key field
  summary: Determines whether a field exists in a hash.
  since: 2.0.0

  HGET key field
  summary: Returns the value of a field in a hash.
  since: 2.0.0

  HGETALL key
  summary: Returns all fields and values in a hash.
  since: 2.0.0

  HINCRBY key field increment
  summary: Increments the integer value of a field in a hash by a number. Uses 0 as initial value if the field doesn't exist.
  since: 2.0.0

  HINCRBYFLOAT key field increment
  summary: Increments the floating point value of a field by a number. Uses 0 as initial value if the field doesn't exist.
  since: 2.6.0

  HKEYS key
  summary: Returns all fields in a hash.
  since: 2.0.0

  HLEN key
  summary: Returns the number of fields in a hash.
  since: 2.0.0

  HMGET key field [field ...]
  summary: Returns the values of all fields in a hash.
  since: 2.0.0

  HMSET key field value [field value ...]
  summary: Sets the values of multiple fields.
  since: 2.0.0

  HRANDFIELD key [count [WITHVALUES]]
  summary: Returns one or more random fields from a hash.
  since: 6.2.0

  HSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Iterates over fields and values of a hash.
  since: 2.8.0

  HSET key field value [field value ...]
  summary: Creates or modifies the value of a field in a hash.
  since: 2.0.0

  HSETNX key field value
  summary: Sets the value of a field in a hash only when the field doesn't exist.
  since: 2.0.0

  HSTRLEN key field
  summary: Returns the length of the value of a field.
  since: 3.2.0

  HVALS key
  summary: Returns all values in a hash.
  since: 2.0.0

5. set

SADD key member [member ...]
  summary: Adds one or more members to a set. Creates the key if it doesn't exist.
  since: 1.0.0

  SCARD key
  summary: Returns the number of members in a set.
  since: 1.0.0

  SDIFF key [key ...]
  summary: Returns the difference of multiple sets.
  since: 1.0.0

  SDIFFSTORE destination key [key ...]
  summary: Stores the difference of multiple sets in a key.
  since: 1.0.0

  SINTER key [key ...]
  summary: Returns the intersect of multiple sets.
  since: 1.0.0

  SINTERCARD numkeys key [key ...] [LIMIT limit]
  summary: Returns the number of members of the intersect of multiple sets.
  since: 7.0.0

  SINTERSTORE destination key [key ...]
  summary: Stores the intersect of multiple sets in a key.
  since: 1.0.0

  SISMEMBER key member
  summary: Determines whether a member belongs to a set.
  since: 1.0.0

  SMEMBERS key
  summary: Returns all members of a set.
  since: 1.0.0

  SMISMEMBER key member [member ...]
  summary: Determines whether multiple members belong to a set.
  since: 6.2.0

  SMOVE source destination member
  summary: Moves a member from one set to another.
  since: 1.0.0

  SPOP key [count]
  summary: Returns one or more random members from a set after removing them. Deletes the set if the last member was popped.
  since: 1.0.0

  SRANDMEMBER key [count]
  summary: Get one or multiple random members from a set
  since: 1.0.0

  SREM key member [member ...]
  summary: Removes one or more members from a set. Deletes the set if the last member was removed.
  since: 1.0.0

  SSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Iterates over members of a set.
  since: 2.8.0

  SUNION key [key ...]
  summary: Returns the union of multiple sets.
  since: 1.0.0

  SUNIONSTORE destination key [key ...]
  summary: Stores the union of multiple sets in a key.
  since: 1.0.0

6. zset(sorted-set)

127.0.0.1:6379> help @sorted-set
  BZMPOP timeout numkeys key [key ...] MIN|MAX [COUNT count]
  summary: Removes and returns a member by score from one or more sorted sets. Blocks until a member is available otherwise. Deletes the sorted set if the last element was popped.
  since: 7.0.0

  BZPOPMAX key [key ...] timeout
  summary: Removes and returns the member with the highest score from one or more sorted sets. Blocks until a member available otherwise.  Deletes the sorted set if the last element was popped.
  since: 5.0.0

  BZPOPMIN key [key ...] timeout
  summary: Removes and returns the member with the lowest score from one or more sorted sets. Blocks until a member is available otherwise. Deletes the sorted set if the last element was popped.
  since: 5.0.0

  ZADD key [NX|XX] [GT|LT] [CH] [INCR] score member [score member ...]
  summary: Adds one or more members to a sorted set, or updates their scores. Creates the key if it doesn't exist.
  since: 1.2.0

  ZCARD key
  summary: Returns the number of members in a sorted set.
  since: 1.2.0

  ZCOUNT key min max
  summary: Returns the count of members in a sorted set that have scores within a range.
  since: 2.0.0

  ZDIFF numkeys key [key ...] [WITHSCORES]
  summary: Returns the difference between multiple sorted sets.
  since: 6.2.0

  ZDIFFSTORE destination numkeys key [key ...]
  summary: Stores the difference of multiple sorted sets in a key.
  since: 6.2.0

  ZINCRBY key increment member
  summary: Increments the score of a member in a sorted set.
  since: 1.2.0

  ZINTER numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
  summary: Returns the intersect of multiple sorted sets.
  since: 6.2.0

  ZINTERCARD numkeys key [key ...] [LIMIT limit]
  summary: Returns the number of members of the intersect of multiple sorted sets.
  since: 7.0.0

  ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
  summary: Stores the intersect of multiple sorted sets in a key.
  since: 2.0.0

  ZLEXCOUNT key min max
  summary: Returns the number of members in a sorted set within a lexicographical range.
  since: 2.8.9

  ZMPOP numkeys key [key ...] MIN|MAX [COUNT count]
  summary: Returns the highest- or lowest-scoring members from one or more sorted sets after removing them. Deletes the sorted set if the last member was popped.
  since: 7.0.0

  ZMSCORE key member [member ...]
  summary: Returns the score of one or more members in a sorted set.
  since: 6.2.0

  ZPOPMAX key [count]
  summary: Returns the highest-scoring members from a sorted set after removing them. Deletes the sorted set if the last member was popped.
  since: 5.0.0

  ZPOPMIN key [count]
  summary: Returns the lowest-scoring members from a sorted set after removing them. Deletes the sorted set if the last member was popped.
  since: 5.0.0

  ZRANDMEMBER key [count [WITHSCORES]]
  summary: Returns one or more random members from a sorted set.
  since: 6.2.0

  ZRANGE key start stop [BYSCORE|BYLEX] [REV] [LIMIT offset count] [WITHSCORES]
  summary: Returns members in a sorted set within a range of indexes.
  since: 1.2.0

  ZRANGEBYLEX key min max [LIMIT offset count]
  summary: Returns members in a sorted set within a lexicographical range.
  since: 2.8.9

  ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
  summary: Returns members in a sorted set within a range of scores.
  since: 1.0.5

  ZRANGESTORE dst src min max [BYSCORE|BYLEX] [REV] [LIMIT offset count]
  summary: Stores a range of members from sorted set in a key.
  since: 6.2.0

  ZRANK key member [WITHSCORE]
  summary: Returns the index of a member in a sorted set ordered by ascending scores.
  since: 2.0.0

  ZREM key member [member ...]
  summary: Removes one or more members from a sorted set. Deletes the sorted set if all members were removed.
  since: 1.2.0

  ZREMRANGEBYLEX key min max
  summary: Removes members in a sorted set within a lexicographical range. Deletes the sorted set if all members were removed.
  since: 2.8.9

  ZREMRANGEBYRANK key start stop
  summary: Removes members in a sorted set within a range of indexes. Deletes the sorted set if all members were removed.
  since: 2.0.0

  ZREMRANGEBYSCORE key min max
  summary: Removes members in a sorted set within a range of scores. Deletes the sorted set if all members were removed.
  since: 1.2.0

  ZREVRANGE key start stop [WITHSCORES]
  summary: Returns members in a sorted set within a range of indexes in reverse order.
  since: 1.2.0

  ZREVRANGEBYLEX key max min [LIMIT offset count]
  summary: Returns members in a sorted set within a lexicographical range in reverse order.
  since: 2.8.9

  ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
  summary: Returns members in a sorted set within a range of scores in reverse order.
  since: 2.2.0

  ZREVRANK key member [WITHSCORE]
  summary: Returns the index of a member in a sorted set ordered by descending scores.
  since: 2.0.0

  ZSCAN key cursor [MATCH pattern] [COUNT count]
  summary: Iterates over members and scores of a sorted set.
  since: 2.8.0

  ZSCORE key member
  summary: Returns the score of a member in a sorted set.
  since: 1.2.0

  ZUNION numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES]
  summary: Returns the union of multiple sorted sets.
  since: 6.2.0

  ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
  summary: Stores the union of multiple sorted sets in a key.
  since: 2.0.0

关注公众号:关大仙的学习笔记

后续高质量文章都会发布到这个公众号上, 还有面试共享群可以添加

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值