由于业务需要使用分布式锁,使用的是redisTemplete客户端,里面有一个setIfAbsent没有带超时时间的属性,为了原子性以及操作效率,因此封装一个带超时时间的setIfAbsent
lua脚本
/** redisUtil.setIfAbsent 新加的带有超时的setIfAbsent 脚本*/
String newSetIfAbsentScriptStr = " if 1 == redis.call('setnx',KEYS[1],ARGV[1]) then" +
" redis.call('expire',KEYS[1],ARGV[2])" +
" return 1;" +
" else" +
" return 0;" +
" end;";
public RedisScript<Boolean> newSetIfAbsentScript = new DefaultRedisScript<Boolean>(newSetIfAbsentScriptStr,Boolean.class );
最好把RedisScript保存一下,这样可以每次脚本执行的时候,使用同一个sha1就可以访问到该script,而不必每次都重复上传script。
封装为方法
/**
* @Description: setIfAbsent升级版,加了超时时间
* @Author: Gong Yongwei
* @Date: 2018/12/12 9:21
* @param key
* @param value
* @param seconds 超时时间,秒为单位
* @return: boolean
*/
public boolean setIfAbsent(String key,String value,Long seconds){
List<Object> keys = new ArrayList<Object>();
keys.add(key);
Object[] args = {value,seconds.toString()};
return redisTemplate.<Boolean>execute(luaScript.newSetIfAbsentScript,keys, args);
}