目录

Redis 命令

目录

强烈建议屏蔽keys命令

1
redis-cli --bigkeys

这个bigkeys得到的最大,不一定是最大。

  • 如果是string结构,通过strlen判断;
  • 如果是list结构,通过llen判断;
  • 如果是hash结构,通过hlen判断;
  • 如果是set结构,通过scard判断;
  • 如果是sorted set结构,通过zcard判断。
1
2
3
# 监视执行命令
redis-cli monitor
redis-cli monitor | grep keys

SCAN

允许增量迭代,返回少量数据,KEYS或者SMEMBERS在针对大型集合调用时可能会阻塞服务器很长时间(甚至几秒钟)键或元素。

SCAN 是一个基于游标的迭代器。这意味着在每次调用命令时,服务器都会返回一个更新的游标,用户需要在下一次调用中将其用作游标参数。 当游标设置为 0 时开始迭代,并在服务器返回的游标为 0 时终止。称为完全迭代。

SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]
SSCAN key cursor [MATCH pattern] [COUNT count]
  • SCAN 迭代当前选定的 Redis 数据库中的一组键。
  • SSCAN 迭代 Sets 类型的元素。
  • HSCAN 迭代 Hash 类型的字段及其关联值。
  • ZSCAN 迭代 Sorted Set 类型的元素及其相关分数。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
scan 0 COUNT 3

# 匹配选项
sadd myset 1 2 3 foo foobar feelsgood
sscan myset 0 match f*

# 类型选项
GEOADD geokey 0 0 value
ZADD zkey 1000 value
TYPE geokey
TYPE zkey
SCAN 0 TYPE zset
1
2
3
4
# INFO命令以计算机易于解析且人类易于阅读的格式返回有关服务器的信息和统计信息。
info
info memory
info keyspace
1
UNLINK key [key ...]

此命令非常类似于DEL:它删除指定的键。就像DEL一个键如果不存在就会被忽略。但是,该命令在不同的线程中执行实际的内存回收,因此它不是阻塞的,而 DEL 是阻塞的。这就是命令名称的来源:该命令只是将键与键空间取消链接。实际的删除将在稍后异步发生。

可用于删除 bigkey

SET

1
SET key value [ EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL] [ NX | XX] [GET]
  • EX seconds – Set the specified expire time, in seconds.
  • PX milliseconds – Set the specified expire time, in milliseconds.
  • EXAT timestamp-seconds – Set the specified Unix time at which the key will expire, in seconds.
  • PXAT timestamp-milliseconds – Set the specified Unix time at which the key will expire, in milliseconds.
  • NX – Only set the key if it does not already exist.
  • XX – Only set the key if it already exist.
  • KEEPTTL – Retain the time to live associated with the key.
  • !GET – Return the old string stored at key, or nil if key did not exist. An error is returned and SET aborted if the value stored at key is not a string.