Test lua脚本 直接测试
EVAL " local comment_user_key=KEYS[1] local gold=ARGV[1] local redis_gold=redis.call('GET',comment_user_key) if redis_gold then redis_gold=tonumber(redis_gold)+tonumber(gold) else redis_gold=tonumber(gold) end redis.call('SET',comment_user_key,redis_gold) redis_gold=redis.call('GET',comment_user_key) return redis_gold " 1 key123 1000
Test lua脚本 代码测试
public static final StringBuilder USER_AIMS_GOLD_LUA=new StringBuilder();
static {
USER_AIMS_GOLD_LUA.append(" local comment_user_key=KEYS[1]");
USER_AIMS_GOLD_LUA.append(" local gold=ARGV[1]");
USER_AIMS_GOLD_LUA.append(" local redis_gold=redis.call('GET',comment_user_key)");
USER_AIMS_GOLD_LUA.append(" if redis_gold ");
USER_AIMS_GOLD_LUA.append(" then ");
USER_AIMS_GOLD_LUA.append(" redis_gold=tonumber(redis_gold)+tonumber(gold) ");
USER_AIMS_GOLD_LUA.append(" else ");
USER_AIMS_GOLD_LUA.append(" redis_gold=tonumber(gold) ");
USER_AIMS_GOLD_LUA.append(" end ");
USER_AIMS_GOLD_LUA.append(" redis.call('SET',comment_user_key,redis_gold)");
USER_AIMS_GOLD_LUA.append(" redis_gold=redis.call('GET',comment_user_key)");
USER_AIMS_GOLD_LUA.append(" return redis_gold ");
LogManager.info(USER_AIMS_GOLD_LUA.toString());
}
public Object setLuaAimsGold(String keys,Object gold) {
byte[] lni=redisTemplate.execute(
(RedisConnection connection)-> connection.eval(
RedisLuaText.USER_AIMS_GOLD_LUA.toString().getBytes(),//lua脚本字符串
ReturnType.VALUE,//设置返回值 byte[]
1, //设置key数量
keys.getBytes(), //key1
gold.toString().getBytes()//Args 值
)
);
return new String(lni);
}