@Override public String updateJson(String key, String field, long num, long limit) { String script = "local t = redis.call('get', KEYS[1]);\n" + "local j;\n" + "if t then j = cjson.decode(t); else j = cjson.decode(\"{}\"); end;\n" + "local n;\n" + "if j[ARGV[3]] then n = tonumber(j[ARGV[3]]); else n = 0 end;\n" + "local r = n + tonumber(ARGV[1]);\n" + "local limit = tonumber(ARGV[2]);\n" + "if r >= 0 and (limit < 0 or r <= limit) then j[ARGV[3]] = r; " + "redis.call('set', KEYS[1], cjson.encode(j)); end;\n" + "j[ARGV[3]] = r;" + "return cjson.encode(j);"; RScript rscript = redissonClient.getScript(); return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.VALUE, Collections.singletonList(key), num, limit, field); } @Override public String hUpdateJson(String key, String uid, String field, long num, long limit) { String script = "local t = redis.call('hget', KEYS[1], ARGV[4]);\n" + "local j;\n" + "if t then j = cjson.decode(t); else j = cjson.decode(\"{}\"); end;\n" + "local n;\n" + "if j[ARGV[3]] then n = tonumber(j[ARGV[3]]); else n = 0 end;\n" + "local r = n + tonumber(ARGV[1]);\n" + "local limit = tonumber(ARGV[2]);\n" + "if r >= 0 and (limit < 0 or r <= limit) then j[ARGV[3]] = r; " + "redis.call('hset', KEYS[1], ARGV[4], cjson.encode(j)); end;\n" + "j[ARGV[3]] = r;" + "return cjson.encode(j);"; RScript rscript = redissonClient.getScript(); return rscript.eval(RScript.Mode.READ_WRITE, script, RScript.ReturnType.VALUE, Collections.singletonList(key), num, limit, field, uid); }
返回值:整个json数据,(注意:即使更新失败也会返回失败后的值,用于对当前情况的判断)
标签:redisson,String,ARGV,lua,json,limit,单个,tonumber,local From: https://www.cnblogs.com/math-and-it/p/17516729.html