redisSettring.getLUaPath是自己lua文件的路径
实现一个CAS
文件内容:
local current = redis.call('GET', KEYS[1])
if current == ARGV[1]
then redis.call('SET', KEYS[1], ARGV[2])
return true
end
return false
JAVA中的实现
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
DefaultRedisScript<Boolean> redisScript;
/**
* 测试redis lua
* @return
*/
@GetMapping("/lua")
public Object testredislua() {
String key = "lua";
redisTemplate.opsForValue().set(key, "source");
String s = (String) redisTemplate.opsForValue().get(key);
System.out.println(s);
redisTemplate.execute(redisScript, Collections.singletonList(key), "source", "target");
s = (String) redisTemplate.opsForValue().get(key);
System.out.println(s);
return null;
}