我需要能够在 Redis 中进行以下事务的操作:
为了实现这一点,我编写了一个 LUA 脚本,计算减量的结果,然后用这个结果修改字段。我选择这种解决方案,因为:
TL;DR:我需要在Redis上实现一个共享余额功能,有什么好的方法吗?
如果您知道如何实现,请在堆栈交换中发布:https://softwareengineering.stackexchange.com/questions/391529/what-architecture-is-the-most-adapted-for-a-shared-balance-in-nodejs-and-maybe
- 只有当结果>0时,才将n值减少
- 否则什么也不做
- 处理任意精度的小数(我需要它们以浮点格式)
- 可被其他进程访问
为了实现这一点,我编写了一个 LUA 脚本,计算减量的结果,然后用这个结果修改字段。我选择这种解决方案,因为:
- 它是原子性的
- 更简单的 INCRBYFLOAT 不管结果都会进行减法运算,并且似乎没有正确的精度
- 使用了 LUA 库 http://oss.digirati.com.br/luabignum/
- 使用的库不符合要求:它仅适用于整数,并且每次发送时太大(即使使用evalsha也很慢)
- 在Redis中编写Lua脚本时如何包含第三方库 => 遵循此方法后,我对Redis上使用附加模块的用法感到困惑。不过,这是从过去看的。现在情况怎么样?
- 是否有更有效的方法来做到这一点?欢迎对代码本身提出建议
- Redis是否真正能够满足我的需求?
this.redisClient.eval(`
${luaBigNumbers}
local operations = cjson.decode(KEYS[1])
local isStillValid = true
local test
for k, v in pairs(operations) do
local temp = BigNum.new(redis.call('hget', v.key, v.field))
local res = BigNum.mt.add(temp, BigNum.new(v.value))
if BigNum.mt.lt(res, BigNum.new('0')) then
isStillValid = false
end
end
if isStillValid then
for k, v in pairs(operations) do
local temp = BigNum.new(redis.call('hget',v.key, v.field))
redis.call('hset', v.key, v.field, BigNum.mt.tostring(BigNum.mt.add(temp, BigNum.new(v.value))))
end
end
return tostring(isStillValid)`,
1, JSON.stringify(values), (err, reply) => {
TL;DR:我需要在Redis上实现一个共享余额功能,有什么好的方法吗?
如果您知道如何实现,请在堆栈交换中发布:https://softwareengineering.stackexchange.com/questions/391529/what-architecture-is-the-most-adapted-for-a-shared-balance-in-nodejs-and-maybe