Redis INCRBY有限制 [英] Redis INCRBY with limits

查看:596
本文介绍了Redis INCRBY有限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以通过我的应用程序进行一次单程往返的Redis操作:

I'd like to know if there is a way to perform this in redis with a single roundtrip from my app:

对于给定键K,其可能的值V是范围[A, B]内的任何整数.基本上,它具有上下边界.

For a given key K, its possible value V is any of the integers inside the range [A, B]. Basically, it has an upper and lower boundary.

当发出INCRBYDECRBY命令(例如INCRBY key 10)时,如果结果值没有超出范围,则将仅执行.

When an INCRBY or DECRBY command is issued (eg. INCRBY key 10) it will be executed only if the resulting value is not out of bounds.

我需要此操作是原子的,并且我想知道是否有一种方法可以避免为此编写Lua脚本.

I need this operation to be atomic, and I wanted to know if there was a way to avoid Lua scripting for this.

谢谢.

推荐答案

此答案可能不是您期望的.但是我不得不说Lua脚本是非常清晰的解决方案.

This answer might not be what you expect. But I have to say that Lua scripting is the crystal clear solution.

-- range-incrby.lua key , increment
local key = KEYS[1]
local increment = ARGV[1]
local cnt = redis.call('get', key) or 0
cnt = cnt + increment
if (cnt >= 0 and cnt <= 100) then
    redis.call('set', key, cnt)
    return cnt
end

此外,如果范围是[0, 2^N - 1],则可以使用带有溢出控制功能的BITFIELD命令来解决此问题.

Also, if the range is [0, 2^N - 1], then you can use BITFIELD command with overflow control to solve the problem.

BITFIELD key OVERFLOW FAIL INCRBY uN 0 increment

但是,这似乎不是您的情况.

However, that seems not your case.

这篇关于Redis INCRBY有限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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