StackExchange.Redis 支持 ZPOP?
StackExchange.Redis support for ZPOP?
我想使用 StackExchange.Redis 为我的应用程序实现 ZPOP
。根据WATCH部分的Redis documentation,ZPOP
可以通过以下命令实现:
WATCH zset
element = ZRANGE zset 0 0
MULTI
ZREM zset element
EXEC
在 StackExchange Redis 中,这看起来像:
var connection = GetMultiplexer();
var db = connection.GetDatabase();
var trans = db.CreateTransaction();
var elements = await trans.SortedSetRangeByScoreAsync(key, 0, 0); // THIS WILL BLOCK INDEFINITELY
var element = elemenets.FirstOrDefault();
trans.SortedSetRemoveAsync(key, element);
await trans.ExecuteAsync();
我的问题是,如何从事务中获取和使用结果?我如何实施 ZPOP
?
考虑为此使用 LUA 脚本。
Redis 保证 lua 脚本是事务性的,因为当一个 eval 脚本是 运行ning 时,没有其他东西可以同时 运行。所以你可以使用EVAL。
Here 是如何使用 LUA 脚本执行 ZPOP 的示例:
local val = redis.call('zrange', KEYS[1], 0, 0)
if val then redis.call('zremrangebyrank', KEYS[1], 0, 0) end
return val
还提供了 ZREVPOP。
使用 StackExchange.Redis 您可以使用 IServer.ScriptLoad
和 IDatabase.ScriptEvaluate
.
加载和执行 LUA 脚本
https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Transactions.md
注意这不是阻塞。在 ConnectionMultiplexer
中包含阻塞代码不是一个好主意。
来自 Stackexchange.Redis documentation:
...the only redis features that StackExchange.Redis does not offer (and
will not ever offer) are the "blocking pops" (BLPOP, BRPOP and
BRPOPLPUSH) - because this would allow a single caller to stall the
entire multiplexer
我想使用 StackExchange.Redis 为我的应用程序实现 ZPOP
。根据WATCH部分的Redis documentation,ZPOP
可以通过以下命令实现:
WATCH zset
element = ZRANGE zset 0 0
MULTI
ZREM zset element
EXEC
在 StackExchange Redis 中,这看起来像:
var connection = GetMultiplexer();
var db = connection.GetDatabase();
var trans = db.CreateTransaction();
var elements = await trans.SortedSetRangeByScoreAsync(key, 0, 0); // THIS WILL BLOCK INDEFINITELY
var element = elemenets.FirstOrDefault();
trans.SortedSetRemoveAsync(key, element);
await trans.ExecuteAsync();
我的问题是,如何从事务中获取和使用结果?我如何实施 ZPOP
?
考虑为此使用 LUA 脚本。 Redis 保证 lua 脚本是事务性的,因为当一个 eval 脚本是 运行ning 时,没有其他东西可以同时 运行。所以你可以使用EVAL。
Here 是如何使用 LUA 脚本执行 ZPOP 的示例:
local val = redis.call('zrange', KEYS[1], 0, 0)
if val then redis.call('zremrangebyrank', KEYS[1], 0, 0) end
return val
还提供了 ZREVPOP。
使用 StackExchange.Redis 您可以使用 IServer.ScriptLoad
和 IDatabase.ScriptEvaluate
.
https://github.com/StackExchange/StackExchange.Redis/blob/master/Docs/Transactions.md
注意这不是阻塞。在 ConnectionMultiplexer
中包含阻塞代码不是一个好主意。
来自 Stackexchange.Redis documentation:
...the only redis features that StackExchange.Redis does not offer (and will not ever offer) are the "blocking pops" (BLPOP, BRPOP and BRPOPLPUSH) - because this would allow a single caller to stall the entire multiplexer