LUA 脚本 –获取KEY local key1 = KEYS[1] --users set local key2 = KEYS[2] – lucky money count
local userNm = ARGV[1] – use name
–返回的变量 local result = {}
–redis command local count = redis.call(“get”, key2)
–用一个临时变量来存放json,json是要放入要返回的数组中的 local jsonRedisTemp={}
if tonumber(count) > 0 then local isExistFlg = redis.call(“sismember”,key1,userNm) if (isExistFlg == 1) then jsonRedisTemp[“exist”] = true jsonRedisTemp[“count”] = count jsonRedisTemp[“userNm”] = userNm jsonRedisTemp[“getResul”] = “Fail got before” else jsonRedisTemp[“exist”] = false redis.call(“sadd”,key1,userNm) redis.call(“decr”,key2) jsonRedisTemp[“count”] = redis.call(“get”, key2) jsonRedisTemp[“userNm”] = userNm jsonRedisTemp[“getResul”] = “Success” end
else jsonRedisTemp[“count”] = 0 jsonRedisTemp[“userNm”] = userNm jsonRedisTemp[“getResul”] = “Fail No count” end
result[1] = cjson.encode(jsonRedisTemp)
return result
|