如何使用Redis以原子方式删除与模式匹配的键 [英] How to atomically delete keys matching a pattern using Redis

查看:49
本文介绍了如何使用Redis以原子方式删除与模式匹配的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Redis 数据库中,我有许多 prefix: 哈希值.

有时我想以原子方式清除它们.如何在不使用某些分布式锁定机制的情况下执行此操作?

解决方案

从 redis 2.6.0 开始,您可以运行以原子方式执行的 lua 脚本.我从来没有写过,但我认为它看起来像这样

EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 前缀:[YOUR_PREFIX 例如 delete_me_*]

<块引用>

警告:正如 Redis 文档所说,由于性能问题,命令不应用于生产中的常规操作,这命令用于调试和特殊操作.阅读更多

请参阅EVAL 文档.

In my Redis DB I have a number of prefix:<numeric_id> hashes.

Sometimes I want to purge them all atomically. How do I do this without using some distributed locking mechanism?

解决方案

Starting with redis 2.6.0, you can run lua scripts, which execute atomically. I have never written one, but I think it would look something like this

EVAL "return redis.call('del', unpack(redis.call('keys', ARGV[1])))" 0 prefix:[YOUR_PREFIX e.g delete_me_*]

Warning: As the Redis document says, because of performance maters, keys command should not use for regular operations in production, this command is intended for debugging and special operations. read more

See the EVAL documentation.

这篇关于如何使用Redis以原子方式删除与模式匹配的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆