redis 锁

public class RedisLockUtil {

private static ICache client = (ICache) SpringContextHolder.getApplicationContext().getBean("mcproxy");
private static final Logger logger = LoggerFactory.getLogger(RedisLockUtil.class);
public static final String DEFAULT_VALUE="1";
public static final int DEFAULT_EXPIRE_LOCK_TIME=60;

public static final String TASK_KEY="QMS_TASK_KEY_";
public static final String TASK_AUDIT_APPROVE_KEY="TASK_AUDIT_APPROVE_KEY_";

public static boolean getLock(String key) {
return getLock(key,DEFAULT_VALUE , DEFAULT_EXPIRE_LOCK_TIME);
}

public static boolean getLock(String key,String value,int expireTime) {
boolean lock = false;
if(client != null){
try {
String result = client.getSet(key, value , expireTime);
if(result == null) {
lock = true;
}
} catch (Exception e) {
logger.error("get task lock from redis error:", e);
}
}
return lock;
}



public static boolean unLock(String key) {
if(client != null){
try {
client.remove(key);
return true;
} catch (Exception e) {
logger.error("unlock from redis error:", e);
}
}
return false;
}

private RedisLockUtil() {
throw new IllegalAccessError("Utility class");
}

}

原文地址:https://www.cnblogs.com/newworldnewstart/p/10696926.html