Redis 批量删除 key
Redis Spring Boot Lua 大约 879 字需求背景
Spring Boot
使用RedisTemplate
时会把key
存入\xac\xed\x00\x05w\x03
这种格式,原因是因为RedisTemplate
默认使用JdkSerializationRedisSerializer
。可使用StringRedisTemplate
代替之。
需批量删除\xac\xed\x00\x05w\x03
开头的key
。
解决方法
方法一
redis-cli -h 127.0.0.1 -p 6379 keys "*keys*" | xargs redis-cli -h 127.0.0.1 -p 6379 del
因为key
是JDK
序列化后的字符串,xargs
读取出来是乱码,无法删除。故使用了方法二(适用于key
不多的情况下)
xargs: Warning: a NUL character occurred in the input. It cannot be passed through in the argument list. Did you mean to use the --null option?
方法二
编写Lua
文件,命名为del.lua
:
local key=KEYS[1]
local list=redis.call("keys", key);
for i,v in ipairs(list) do
redis.call("del", v);
end
Redis
调用Lua
脚本,注意:这里的*keys*
可替换为需要删除的关键字,如:*mykey*
。
redis-cli -h 127.0.0.1 -p 6379 --eval ./del.lua "*keys*"
备注
如果批量删除的不是JDK
序列化后的key
,而key
又很多的情况下,可使用scan
扫描删除:
redis-cli -h 127.0.0.1 -p 6379 --scan --pattern '*keys*' | xargs -L 100 redis-cli -h 127.0.0.1 -p 6379 del
阅读 2953 · 发布于 2019-10-21
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
Kubernetes Service 对外提供访问阅读 385
-
Java 使用 SnakeYAML 解析 YAML 文件阅读 120
-
Linux 不排序去除重复行和不排序统计重复行阅读 3916
-
filebeat read: connection reset by peer阅读 2422
-
IDEA 破解后打不开解决方法阅读 13224
-
Docker 部署 Kibana阅读 381
-
Kubernetes 搭建之配置 CentOS7 环境阅读 278
-
Java 使用 wait 等待会使 synchronized 升级为重量级锁阅读 858
-
数据结构:哈希表阅读 840
-
JMeter 使用 Critical Section Controller 按顺序执行接口阅读 902