1、话不多说,直接代码
private synchronized boolean syncLock(String page) {
boolean flag = true;
try {
long nowTime = System.currentTimeMillis();
long expireTime = System.currentTimeMillis() + EXPIRE;
baseRedis.setEx("lock", "lock_Key", nowTime);
baseRedis.expire("lock", expireTime);
String redisResult = baseRedis.get("lock");
if (StringUtil.isNotBlank(redisResult) && "lock_Key".equals(baseRedis.get("lock"))) {
flag = XXXService.eDocCheckModel(page);
}
} finally {
unLock();
}
return flag;
}
private void unLock() {
baseRedis.del("lock");
}
2、第二种写法(设置过期时间)
private synchronized boolean syncLock(String page) {
boolean flag = true;
try {
if (redisLockClient.tryLock(LOCK_ID, LockType.FAIR, 15000L, 14800L, TimeUnit.MILLISECONDS)) {
代码块实现
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
unLock();
}
return flag;
}
private void unLock() {
redisLockClient.unLock(LOCK_ID, LockType.FAIR);
}