Bladeren bron

任务来源:机场

任务描述:添加redis锁
develop
wubin 1 jaar geleden
bovenliggende
commit
126b16edd8
1 gewijzigde bestanden met toevoegingen van 55 en 18 verwijderingen
  1. +55
    -18
      tuoheng-admin/src/main/java/com/tuoheng/admin/utils/RedisLock.java

+ 55
- 18
tuoheng-admin/src/main/java/com/tuoheng/admin/utils/RedisLock.java Bestand weergeven



import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;


import java.util.List;
import java.util.concurrent.TimeUnit;

/** /**
* redis分布式锁 * redis分布式锁
* @author :小肖 * @author :小肖
@Autowired @Autowired
private StringRedisTemplate redisTemplate; private StringRedisTemplate redisTemplate;


// /**
// * 加锁
// * @param key
// * @param value
// * @return
// */
// public boolean lock(String key,String value){
// if(redisTemplate.opsForValue().setIfAbsent(key,value)){
// // 加锁成功
// return true;
// }
//
// // 如果锁过期
// String currentValue = redisTemplate.opsForValue().get(key);
// if(!StringUtils.isEmpty(currentValue) &&
// Long.parseLong(currentValue) < System.currentTimeMillis()){
//
// String oldValue = redisTemplate.opsForValue().getAndSet(key,value);
// if(!StringUtils.isEmpty(oldValue) &&
// oldValue.equals(oldValue)){
// return true;
// }
// }
//
// // 锁已经被其它线程获取
// return false;
// }

/** /**
* 加锁
* redis 分布式锁
*
* @param key * @param key
* @param value * @param value
* @param timeout
* @param unit
* @return * @return
*/ */
public boolean lock(String key,String value){
if(redisTemplate.opsForValue().setIfAbsent(key,value)){
// 加锁成功
return true;
public boolean setLock(String key, String value, Long timeout, TimeUnit unit) {
if (StringUtils.isEmpty(key) || StringUtils.isEmpty(value) || timeout == null || unit == null || timeout <= 0) {
return false;
} }
SessionCallback<Boolean> sessionCallback = new SessionCallback<Boolean>() {
List<Object> exec = null;


// 如果锁过期
String currentValue = redisTemplate.opsForValue().get(key);
if(!StringUtils.isEmpty(currentValue) &&
Long.parseLong(currentValue) < System.currentTimeMillis()){

String oldValue = redisTemplate.opsForValue().getAndSet(key,value);
if(!StringUtils.isEmpty(oldValue) &&
oldValue.equals(oldValue)){
return true;
@Override
public Boolean execute(RedisOperations operations) throws DataAccessException {
operations.multi();
redisTemplate.opsForValue().setIfAbsent(key, value);
redisTemplate.expire(key, timeout, unit);
exec = operations.exec();
if (exec.size() > 0) {
return (Boolean) exec.get(0);
}
return false;
} }
}

// 锁已经被其它线程获取
return false;
};
return redisTemplate.execute(sessionCallback);
} }


/** /**

Laden…
Annuleren
Opslaan