redis通用命令

文章介绍了如何在Redis中使用命令行进行操作,包括连接容器、查看帮助文档以及一些基本的键操作如DEL、EXISTS、EXPIRE等。特别提到了在生产环境中应避免使用KEYS命令进行模糊查询,因其可能导致服务器阻塞,影响性能。建议在从节点进行此类查询以保证数据安全。
摘要由CSDN通过智能技术生成

官网命令文档

点击查看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 | Redis

常用命令

  • KEYS: 返回符合条件的所有key
    • KEYS *: 返回所有key
    • KEYS 袁*: 返回所有开头的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的有效期为永久
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值