惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
P
Privacy International News Feed
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 叶小钗
F
Fortinet All Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
L
LINUX DO - 最新话题
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
Recent Announcements
Recent Announcements
Recent Commits to openclaw:main
Recent Commits to openclaw:main
PCI Perspectives
PCI Perspectives
Cloudbric
Cloudbric
AI
AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
IT之家
IT之家
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
J
Java Code Geeks
M
MIT News - Artificial intelligence
Cisco Talos Blog
Cisco Talos Blog
V2EX - 技术
V2EX - 技术
Webroot Blog
Webroot Blog
Microsoft Security Blog
Microsoft Security Blog
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
G
Google Developers Blog
W
WeLiveSecurity
罗磊的独立博客
P
Privacy & Cybersecurity Law Blog
阮一峰的网络日志
阮一峰的网络日志
A
About on SuperTechFans
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
T
Tailwind CSS Blog
V
Visual Studio Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Secure Thoughts
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
Google DeepMind News
Google DeepMind News
Google DeepMind News
Google DeepMind News
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Full Disclosure
Blog — PlanetScale
Blog — PlanetScale
The Last Watchdog
The Last Watchdog
P
Proofpoint News Feed

NickChenyx's Blog

Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish Create_Rubbish
Create_Rubbish
nickChen · 2018-04-18 · via NickChenyx's Blog

线上 KEY 批量删除

Redis 删除特定前缀的 key,需要注意性能影响,比较直观的模糊删除的方式是:

# 错误示范,生产环境不可用!
redis-cli --raw keys "service:order:*" | xargs redis-cli del

使用 redis-cli 执行 KEYS 指令模糊匹配对应的 key,再利用管道传递给 redis-cli 执行 DEL 指令。这里的 KEYS 指令是个性能隐患。

实际上阿里云 4G 的 redis 实例上,只有 500 MB 左右的内存占用时,KEYS 指令的 rt 已经达到了 500ms+ 的程度,严重影响了 Redis 的性能。

此处 redis-cli 命令最简化了,实际上线上机器应当还有 host/password 等相关信息需要配置在 redis-cli 的参数上

线上不能使用 KEYS 指令应该成为编码过程中必须注意的点,实际也可以禁用 Redis 的 KEYS 指令,避免开发人员误操作。

修改 redis.conf 文件,添加 rename-command KEYS "" 即可,可以理解为重命名某个指令,重命名为空字符串即为禁用

使用 SCAN 指令进行模糊匹配的操作(Redis 2.8 开始支持此指令)

SCAN 指令的具体用法不在此处描述,此处实际要利用的是 SCAN 指令的特性进行模糊匹配要删除的 key。

redis-cli --scan --pattern "service:order:*" | xargs redis-cli del

--scan 使用的就是 SCAN 指令的特性进行了 key 的模糊匹配,对比 KEYS 指令这个操作不会阻塞 Redis 操作,对线上业务没有影响。

需要注意上述操作模糊删除 string 类型的 Redis 键时时间复杂度为 O(1),但是对于其他数据结构时间可能不一样了。比如 set 数据结构,如果直接使用 DEL 指令删除这个 set 数据结构的 key,他的时间复杂度并不是 O(1),而是 O(M),M 为数据结构中元素的数量。也就是意味着如果这个数据集合过大,这个 DEL 指令的执行实际上也有性能风险(此处可看官方文档中的 DEL 时间复杂度描述)。

面对除了 string 类型的其他数据结构,Redis 有对应的 HSCANSSCANZSCAN 指令可以遍历其元素,利用这一特性可以整理出以下的批量删除脚本。

批量删除 set 数据结构中的数据

import redis

def del_big_set_key(key_name):
    r = redis.StrictRedis(host='localhost', port=6379)

    # count表示每次删除的元素数量,这里每次删除300元素
    for key in r.sscan_iter(name=key_name, count=300):
        r.srem(key_name, key)

del_big_set_key('ops-coffee')

批量删除 hash 数据结构中的数据

import redis

def del_big_hash_key(key_name):
    r = redis.StrictRedis(host='localhost', port=6379)

    # hscan_iter获取出来的结果是个元祖,下边hdel删除用key[0]取到key
    for key in r.hscan_iter(name=key_name, count=300):
        r.hdel(key_name, key[0])

del_big_hash_key('ops-coffee')

批量删除 zset 数据结构中的数据

import redis

def del_big_sort_key(key_name):
    r = redis.StrictRedis(host='localhost', port=6379)

    while r.zcard(key_name) > 0:
        # 判断集合中是否有元素,如有有则删除排行0-99的元素
        r.zremrangebyrank(key_name, 0, 99)

del_big_sort_key('ops-coffee')

参考资料


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 nickchenyx@gmail.com