Last active
December 28, 2022 16:22
-
-
Save weedge/2b473c5baf0ac59d8d0c8b1ddc5692f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- author: weedge | |
-- params: KEYS[1] user asset key | |
-- params: KEYS[2] event msg key | |
-- params: ARGV[1] incr asset num eg:1,-1 | |
-- params: ARGV[2] user asset key ttl | |
-- params: ARGV[3] event msg key ttl | |
-- return 1:操作成功, 0:无操作,-1:缓存资产不存在,-2:资产不足, | |
-- debug: | |
-- redis-cli --ldb --eval user_asset_change.redis.lua I.asset.{100} M.asset.{100}.`ksuid` , 100 86400 86400 | |
-- redis-cli -c -p 26383 --ldb --eval user_asset_change.redis.lua I.asset.{100} M.asset.{100}.`ksuid` , 100 86400 86400 | |
-- restart to slot debug | |
if redis.call("exists", KEYS[1]) ~= 1 then | |
return -1 | |
end | |
if redis.call("type", KEYS[1]).ok == "string" then | |
local assetStr = redis.call('get', KEYS[1]); | |
local assetInfo = cjson.decode(assetStr); | |
if assetInfo.assetCn == nil then | |
assetInfo.assetCn = 0 | |
end | |
local incr = 0 | |
if ARGV[1] ~= nil then | |
incr = tonumber(ARGV[1]); | |
end | |
if assetInfo.assetCn + incr < 0 then | |
return -2; | |
end | |
assetInfo.assetCn = assetInfo.assetCn + incr; | |
assetStr = cjson.encode(assetInfo); | |
if redis.call('set', KEYS[1], assetStr, 'ex', tonumber(ARGV[2])) | |
and redis.call('set', KEYS[2], 1, 'ex', tonumber(ARGV[3])) then | |
return 1; | |
end | |
return 0; | |
end | |
if redis.call("type", KEYS[1]).ok == "hash" then | |
local assetCn = redis.call('hincrby', KEYS[1], 'assetCn', 0); | |
local incr = 0 | |
if ARGV[1] ~= nil then | |
incr = tonumber(ARGV[1]); | |
end | |
if assetCn + incr < 0 then | |
return -2; | |
end | |
assetCn = assetCn + incr; | |
if redis.call('hincrby', KEYS[1], 'assetCn', incr) | |
and redis.call('expire', KEYS[1], tonumber(ARGV[2])) | |
and redis.call('set', KEYS[2], 1, 'ex', tonumber(ARGV[3])) then | |
return 1; | |
end | |
return 0; | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment