有没有什么好方法可以支持 Redis Sorted Set 中的 pop 成员?

编程入门 行业动态 更新时间:2024-10-15 14:19:32
本文介绍了有没有什么好方法可以支持 Redis Sorted Set 中的 pop 成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有好办法像 List 的 api LPOP 一样支持 Redis Sorted Set 中的 pop 成员?

Is there good way to support pop members from the Redis Sorted Set just like the api LPOP of the List ?

我发现从 Redis Sorted Set 弹出消息是使用 ZRANGE +ZREM ,但是它不是线程安全性的,并且当多线程从不同的主机同时访问它们时需要分布式锁.

What I figured out for poping message from the Redis Sorted Set is using ZRANGE +ZREM , however it is not thread security and need the distributed lock when multi threads accessing them at the same time from the different host.

请建议是否有更好的方法从排序集中弹出成员?

Please kind suggesting if there is better way to pop the members from the Sorted Set?

推荐答案

你可以写一个 Lua 脚本 完成这项工作:将这两个命令包装在一个 Lua 脚本中.Redis 确保 Lua 脚本以原子方式运行.

You can write a Lua script to do the job: wrap these two commands in a single Lua script. Redis ensures that the Lua script runs in an atomic way.

local key = KEYS[1] local result = redis.call('ZRANGE', key, 0, 0) local member = result[1] if member then redis.call('ZREM', key, member) return member else return nil end

更多推荐

有没有什么好方法可以支持 Redis Sorted Set 中的 pop 成员?

本文发布于:2023-11-22 01:01:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615356.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有没有什么   成员   方法   Sorted   Redis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!