[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>遇到的问题:
在开发过程中,会遇到要批量删除某种规则的key,例如login_logID(ID为变量),现在需要删除"login_log*"这一类的数据,但是redis本身只有批量查询一类key值的命令keys,但是没有批量删除某一个类的命令。
<>解决办法:
先查询,在删除,使用xargs传参(xargs可以将管道或标准输入(stdin)数据转换成命令行参数),先执行查询语句,在将查询出来的key值,当初del的参数去删除。
redis-cli KEYS key* (查找条件) | xargs redis-cli del
=>[执行后返回的结果影响数量]:(integer) 10[数量10个]
做个实验,先创三个同类型的key值
127.0.0.1:6379> set test1 1 OK 127.0.0.1:6379> set test2 2 OK 127.0.0.1:6379>
set test3 3 OK
查询keys
127.0.0.1:6379> keys test* 1) "test3" 2) "test2" 3) "test1"
退出redis,在本地执行删除命令
[root@localhost redis]# redis-cli -a 密码 -n 0(数据库) keys "test*" |xargs
redis-cli -a 密码 -n 0(数据库) del Warning: Using a password with '-a' or '-u'
option on the command line interface may not be safe. Warning: Using a password
with '-a' or '-u' option on the command line interface may not be safe.
(integer) 3 (返回行数)
原理解析:
这个命令是先通过redis客户端执行了keys命令,模糊搜索出所有的key,通过xargs命令,将前面查询出来的key作为后面redis的del命令的输入
相当于执行了 redis-cli del test1 test2 test3
注意:这里执行时需要带上redis条件,-a是输入密码,-n是指定数据库,如果redis不在本地或者其他有变动还需要加上-h
redis所在服务器ip,-p端口
例如:redis-cli -h 127.0.0.1(IP地址) -p 6379 (端口号) -a 密码 -n 1(数据在第几个库就写几) KEYS
key* (查找条件) | xargs redis-cli (-h (IP地址) -p 6379 (端口号) -a 密码 -n 1 ) del
<>补充知识:
redis中的删除
1.Redis DEL 命令用于删除已存在的键,不存在的 key 会被忽略。
例如:
127.0.0.1:6379> set hello world OK 127.0.0.1:6379> del hello (integer) 1
但是del只能删除一个或多个,不能批量删除,当需要删除数据量过大时就不适用了
2.清空整个 Redis 服务器的数据:flushall
3.清空当前库中的所有 key:flushdb