Commit 3d944af7 authored by 赵啸非's avatar 赵啸非

修正redis 最大连接数

parent 29ff6d88
package com.mortals.xhx.tools.lock.service.impl; package com.mortals.xhx.tools.lock.service.impl;
import com.mortals.framework.service.ICacheService; import com.mortals.framework.service.ICacheService;
import com.mortals.xhx.common.keys.RedisCacheKeys;
import com.mortals.xhx.tools.lock.service.ILockService; import com.mortals.xhx.tools.lock.service.ILockService;
import lombok.extern.apachecommons.CommonsLog; import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
/** /**
* redis 实现分布式锁机制 * redis 实现分布式锁机制
* @author zxfei * @author zxfei
*/ */
@CommonsLog @CommonsLog
@Service("redisLockService") //@Service("redisLockService")
//@ConditionalOnProperty("application.cache.redis") //@ConditionalOnProperty("application.cache.redis")
public class RedisLockServiceImpl implements ILockService { public class RedisLockServiceImpl implements ILockService {
private static ThreadLocal<String> threadLocal = new ThreadLocal<>(); private static ThreadLocal<String> threadLocal = new ThreadLocal<>();
...@@ -28,32 +25,34 @@ public class RedisLockServiceImpl implements ILockService { ...@@ -28,32 +25,34 @@ public class RedisLockServiceImpl implements ILockService {
@Override @Override
public boolean lock(String lockKey, Long unlockSec) { public boolean lock(String lockKey, Long unlockSec) {
try { return false;
boolean ret; // try {
if (ObjectUtils.isEmpty(unlockSec)) { // boolean ret;
ret = cacheService.hincrBy(RedisCacheKeys.getCoopsDistributedLockKey(), lockKey, 1) == 1; // if (ObjectUtils.isEmpty(unlockSec)) {
} else { // ret = cacheService.hincrBy(RedisCacheKeys.getCoopsDistributedLockKey(), lockKey, 1) == 1;
ret = cacheService.hincrByForTime(RedisCacheKeys.getCoopsDistributedLockKey(), lockKey, 1, unlockSec) == 1; // } else {
} // ret = cacheService.hincrByForTime(RedisCacheKeys.getCoopsDistributedLockKey(), lockKey, 1, unlockSec) == 1;
if (ret) { // }
threadLocal.set(lockKey); // if (ret) {
} // threadLocal.set(lockKey);
return ret; // }
} catch (Exception e) { // return ret;
log.error(String.format("分布式加锁出错 lockKey:%s%s", lockKey, ObjectUtils.isEmpty(unlockSec) ? "" : ",unlockSec:" + unlockSec), e); // } catch (Exception e) {
return false; // log.error(String.format("分布式加锁出错 lockKey:%s%s", lockKey, ObjectUtils.isEmpty(unlockSec) ? "" : ",unlockSec:" + unlockSec), e);
} // return false;
// }
} }
@Override @Override
public boolean unlock() { public boolean unlock() {
try { // try {
cacheService.hdel(RedisCacheKeys.getCoopsDistributedLockKey(), threadLocal.get()); // cacheService.hdel(RedisCacheKeys.getCoopsDistributedLockKey(), threadLocal.get());
threadLocal.remove(); // threadLocal.remove();
return true; // return true;
} catch(Exception e) { // } catch(Exception e) {
log.error(String.format("分布式解锁出错 lockKey:%s", threadLocal.get()), e); // log.error(String.format("分布式解锁出错 lockKey:%s", threadLocal.get()), e);
return false; // return false;
} // }
return false;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment