项目使用中的
脚本片段1:
"local keys,values=KEYS,ARGV \n" + " for i,v in ipairs(keys) do\n" + " redis.call('SETEX',keys[i],ARGV[#values]+0,values[i])\n" + " end " + "return true;"
脚本片段2:
"redis.call('DEL',KEYS[1]) \n" + "if (string.len(ARGV[1])>0) then redis.call('SETEX',KEYS[2],ARGV[2]+1,ARGV[1]) \n" + "else redis.call('DEL',KEYS[2]) end \n"+ "return true;"
脚本片段3:
"local len=#KEYS \n"+ "for i=1,len-1 do\n" + " redis.call('DEL',KEYS[i])\n" + "end \n" + "if (string.len(ARGV[1])>0) then redis.call('SETEX',KEYS[len],ARGV[2]+1,ARGV[1]) \n" + "else redis.call('DEL',KEYS[len]) end \n"+ "return true;"
脚本片段4:
"local len=#KEYS \n"+ "if (string.len(ARGV[1])>0) then redis.call('SETEX',KEYS[1],ARGV[2]+1,ARGV[1]) \n"+ "else redis.call('DEL',KEYS[1]) end \n"+ "for i=2,len do\n" + " redis.call('DEL',KEYS[i])\n" + "end \n" + "return true;"
脚本片段5:
" redis.call('INCR',KEYS[1]) redis.call('EXPIRE',KEYS[1],ARGV[1]+0) return redis.call('GET',KEYS[1]) "
脚本片段6:
" if redis.call('exists',KEYS[1]) == 0 then redis.call('setex',KEYS[1],ARGV[1]+0,ARGV[2]) return true else return false end"
脚本片段7:
"local len=#KEYS " + "if redis.call('exists',unpack(KEYS)) == 0 then \n" + "for i=1,len do\n" + " redis.call('setex',KEYS[i],ARGV[1]+0,ARGV[2])\n" + "end \n" + "return true " + "else return false end"
脚本片段8:
" if redis.call('get',KEYS[1]) == ARGV[1] then redis.call('del',KEYS[1]) return true else return false end"
脚本片段9:
"local len=#KEYS " + "for i=1,len do\n" + " if redis.call('get',KEYS[i]) == ARGV[1] then redis.call('del',KEYS[i]) end\n" + "end \n" + "return true;"
使用脚本片段1的java代码:
/**
* 通过lua脚本批量Set(带着过期时间)
*
* @param keys keys
* @param values values
* @param expireTime 单位秒
* @return
*/
public Boolean batchSetWithExByLuaScript(List keys,List values,Integer expireTime){
checkKeys(keys);
checkKeys(values);
String[] oriVaues = values.toArray(new String[values.size()]);
String[] args = new String[values.size()+1];
System.arraycopy(oriVaues, 0, args, 0,
values.size());
args[values.size()] = expireTime.toString();
//return (Boolean) redisClient.executeByLuaScript(keys,args, RedisUtils.BATCHSETWITHEX_LUASCRIPT);
return executeByLuaScript(keys,args,RedisUtils.BATCHSETWITHEX_LUASCRIPT,3);
}
public boolean executeByLuaScript(List keys,String[] args,String luaScript,int retryTimes){
boolean isOk = false;
int i = 0;
while (i < retryTimes) {
try {
isOk = redisClient.executeByLuaScript(keys,args, luaScript);
break;
} catch (Exception e) {
i++;
if (i >= retryTimes)
throw e;
}
}
return isOk;
}
使用脚本片段6的java代码:
/**
* 获取锁
*
* @param key
* @return
* @throws InterruptedException
*/
public boolean acquireLock(String key,String identifier) throws InterruptedException{
List keys = new ArrayList<>();
keys.add(key);
String[] args = new String[2];
args[0] = Constants.LOCKKEY_TIMEOUT_SECONDS.toString();
args[1] = identifier;
return executeByLuaScript(keys,args, RedisUtils.ACQUIRE_LOCK_WITH_TIMEOUT_LUASCRIPT,3);
}
使用脚本片段7的java代码:
/**
* 批量获取锁
* key被锁住3秒
* @param keys
* @param identifier
* @return
* @throws InterruptedException
*/
public boolean batchAcquireLock(List keys,String identifier) throws InterruptedException {
checkKeys(keys);
String[] args = new String[2];
args[0] = Constants.LOCKKEY_TIMEOUT_SECONDS.toString();
args[1] = identifier;
return executeByLuaScript(keys,args, RedisUtils.BATCHACQUIRE_LOCK_WITH_TIMEOUT_LUASCRIPT,3);
}
使用脚本片段8的java代码:
/**
* 释放锁
*
* @param key
* @param identifier
*/
public boolean releaseLock(String key,String identifier){
List keys = new ArrayList<>();
keys.add(key);
String[] args = new String[1];
args[0] = identifier;
return executeByLuaScript(keys,args, RedisUtils.RELEASE_LOCK_LUASCRIPT,3);
}
使用脚本片段9的java代码:
/**
* 批量释放锁
*
* @param keys
* @param identifier
*/
public boolean batchReleaseLock(List keys,String identifier){
checkKeys(keys);
String[] args = new String[1];
args[0] = identifier;
return executeByLuaScript(keys,args, RedisUtils.BATCHRELEASE_LOCK_LUASCRIPT,3);
}