有没有什么好方法可以支持 Redis Sorted Set 中的 pop 成员? [英] Is there good way to support pop members from the Redis Sorted Set?

查看:58
本文介绍了有没有什么好方法可以支持 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 成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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