删除基于图案的多个redis键

发布于 2025-01-25 13:28:15 字数 217 浏览 0 评论 0原文

我正在尝试根据指定的密钥模式从REDIS实例中删除多个键。但是,我所看到的许多其他答案都独有的是,我需要在Redis控制台内完全执行此操作,而不会使用任何其他脚本或bash。原因是我需要从Redis门户的Azure Cache的控制台功能内执行此操作。我能够选择要使用扫描0 Match *mypattern *的键,但是,我不知道将其“将”此操作到del命令中。

I am trying to remove multiple keys from a Redis instance based on a specified key pattern. However, unique to many of the other answers I've seen on SO, I need to do this entirely within the Redis console without the use of any other scripting or Bash. The reason being that I need to execute this from within the Console functionality of the Azure Cache for Redis portal. I am able to select the keys I want using SCAN 0 MATCH *mypattern* but, I don't know of any way to "pipe" this to the DEL command.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨凡馨 2025-02-01 13:28:15

如果这对其他任何人都有帮助,这是我发现对我有用的答案。

evar eval“ return redis.call('del',unfack(redis.call)('scan',0,'match',argv [1])[2]))” 0 *mypattern *

the内部redis.call('scan')命令找到匹配的所有键*mypattern*(其中任何位于其中的任何“ mypattern”的键)。该命令的返回值的第二个索引位置是键的列表,然后将其解开并传递到ofter redis.call('del')命令,该命令从缓存中删除对象。

请注意,如果没有与模式匹配的键,这将返回错误。对于我的用例,这是可以的,所以我没有进一步探索它,但是可能有办法解决这个问题。

In case this is helpful to anyone else, here is the answer I found that worked for me.

EVAL "return redis.call('del', unpack(redis.call('scan', 0, ‘MATCH', ARGV[1])[2]))" 0 *mypattern*

The inner redis.call('scan') command finds all keys matching *mypattern* (any key with "mypattern" anywhere in it). The second index position of the return value of this command is the list of the keys which is then unpacked and passed to the outer redis.call('del') command which deletes the objects from the cache.

Note that this will return an error if there are no keys matching the pattern. This was ok for my use case so I did not explore it further, but there may be a way around this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文