官网命令文档
点击查看Commands | Redis{target=_blank}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YXgbVK59-1681143531535)(null)]
命令行查看命令
- 先进入redis容器
docker exec -it redis-node-1 bash
- 连接redis,这里是连接redis集群
redis-cli -h 192.168.2.20 -p 24003 -c
- 输入帮助命令
help @generic
查看和key相关的命令
[root@12 ~]# docker exec -it redis-node-1 bash
root@12:/data# redis-cli -h 192.168.2.20 -p 24003 -c
192.168.2.20:24003> help @generic
COPY source destination [DB destination-db] [REPLACE]
summary: Copy a key
since: 6.2.0
DEL key [key ...]
summary: Delete a key
since: 1.0.0
DUMP key
summary: Return a serialized version of the value stored
since: 2.6.0
EXISTS key [key ...]
summary: Determine if a key exists
since: 1.0.0
EXPIRE key seconds [NX|XX|GT|LT]
summary: Set a key's time to live in seconds
since: 1.0.0
EXPIREAT key unix-time-seconds [NX|XX|GT|LT]
summary: Set the expiration for a key as a UNIX timestamp
since: 1.2.0
EXPIRETIME key
summary: Get the expiration Unix timestamp for a key
since: 7.0.0
KEYS pattern
summary: Find all keys matching the given pattern
since: 1.0.0
MIGRATE host port key|"" destination-db timeout [COPY] [R
summary: Atomically transfer a key from a Redis instance
since: 2.6.0
MOVE key db
summary: Move a key to another database
since: 1.0.0
OBJECT
summary: A container for object introspection commands
since: 2.2.3
OBJECT ENCODING key
summary: Inspect the internal encoding of a Redis object
since: 2.2.3
OBJECT FREQ key
summary: Get the logarithmic access frequency counter of
since: 4.0.0
OBJECT HELP
summary: Show helpful text about the different subcommand
since: 6.2.0
OBJECT IDLETIME key
summary: Get the time since a Redis object was last acces
since: 2.2.3
OBJECT REFCOUNT key
summary: Get the number of references to the value of the
since: 2.2.3
PERSIST key
summary: Remove the expiration from a key
since: 2.2.0
PEXPIRE key milliseconds [NX|XX|GT|LT]
summary: Set a key's time to live in milliseconds
since: 2.6.0
PEXPIREAT key unix-time-milliseconds [NX|XX|GT|LT]
summary: Set the expiration for a key as a UNIX timestamp
since: 2.6.0
PEXPIRETIME key
summary: Get the expiration Unix timestamp for a key in m
since: 7.0.0
PTTL key
summary: Get the time to live for a key in milliseconds
since: 2.6.0
RANDOMKEY
summary: Return a random key from the keyspace
since: 1.0.0
RENAME key newkey
summary: Rename a key
since: 1.0.0
RENAMENX key newkey
summary: Rename a key, only if the new key does not exist
since: 1.0.0
RESTORE key ttl serialized-value [REPLACE] [ABSTTL] [IDLE
summary: Create a key using the provided serialized value
since: 2.6.0
SCAN cursor [MATCH pattern] [COUNT count] [TYPE type]
summary: Incrementally iterate the keys space
since: 2.8.0
SORT key [BY pattern] [LIMIT offset count] [GET pattern [
summary: Sort the elements in a list, set or sorted set
since: 1.0.0
SORT_RO key [BY pattern] [LIMIT offset count] [GET patter
summary: Sort the elements in a list, set or sorted set.
since: 7.0.0
TOUCH key [key ...]
summary: Alters the last access time of a key(s). Returns
since: 3.2.1
TTL key
summary: Get the time to live for a key in seconds
since: 1.0.0
TYPE key
summary: Determine the type stored at key
since: 1.0.0
UNLINK key [key ...]
summary: Delete a key asynchronously in another thread. O
since: 4.0.0
WAIT numreplicas timeout
summary: Wait for the synchronous replication of all the
since: 3.0.0
192.168.2.20:24003>
这么多命令没法记也不用去记,只要会查看帮助文档去查即可,其实常用的命令就那么几个,用的多了就记住了,不常用的也记不住
比如查看keys
的用法:
192.168.2.20:24003> help keys
KEYS pattern
summary: 查找匹配pattern的所有key
since: 1.0.0
group: generic
常用命令
KEYS
: 返回符合条件的所有keyKEYS *
: 返回所有keyKEYS 袁*
: 返回所有袁
开头的key
::: tip
生成环境尽量不要用keys查询,为什么呢?因为,数据量达到一定量,模糊查询效率低,使用模糊查询给服务器带来巨大负担,redis是单线程的,模糊查询的这段时间内,他就无法其他的操作key的命令,那么redis服务就被阻塞了,就会影响其他业务的正常运行.那么如果你有redis集群,可以在从节点做keys查询
:::
DEL k1
:删除k1,返回删除的个数,集群模式不能使用del删除多个EXISTS k1
:返回k1存在的数量,1存在0不存在EXPIRE k3 20
: 给k3设置20秒有效期TTL k3
: 查看k3的有效期,返回-2已过期删除了该key,返回-1代表这个key的有效期为永久