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

推荐订阅源

Recent Announcements
Recent Announcements
J
Java Code Geeks
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
腾讯CDC
博客园 - 司徒正美
B
Blog RSS Feed
博客园 - 三生石上(FineUI控件)
I
InfoQ
N
Netflix TechBlog - Medium
L
LangChain Blog
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Tailwind CSS Blog
MyScale Blog
MyScale Blog
美团技术团队
The Cloudflare Blog
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
H
Help Net Security
Martin Fowler
Martin Fowler
V
Visual Studio Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Delete Keys Matching A Pattern with py-redis-cluster
2021-07-20 · via jdhao's digital space

We have a Python web service where we store some key-val pairs in redis. Occasionally, I want to delete some of the keys matching a certain pattern. Current, we are using redis-py-cluster for redis-related operations.

We can use scan_iter() method to search such keys and delete them. The first parameter to scan_iter() is the matching pattern. The code looks roughly like this:

for k in redis_client.scan_iter("prefix:*"):
    redis_client.delete(k)

The above code kinda works, but it is awfully slow. We can add the count option to accelerate deletion. Option count specify how many keys per scan will return. According to here, the default count is 10, which is rather small.

batch_size = 500
keys = []

for k in redis_client.scan_iter("prefix:*", count=batch_size):
    keys.append(k)
    if len(keys) >= batch_size:
        redis_client.delete(*keys)
        keys = []
if len(keys) > 0:
    redis_client.delete(*keys)

Using a large count will speed up the deletion process significantly. I have benchmarked on about 20000 keys. Here is what I have found:

batch size  Time taken (seconds)
100         929
500         260
1000        175
2000        133
4000        107
5000        106
10000       93

Refs#