Commit 636ab521 authored by gaoyingwei's avatar gaoyingwei

修改 订单定时关闭

parent e8b9ce13
......@@ -2,6 +2,7 @@ package org.rcisoft.business.cmsActivity.service.impl;
import cn.hutool.core.lang.Assert;
import org.apache.commons.lang3.StringUtils;
import org.rcisoft.core.service.CyRedisService;
import org.rcisoft.core.util.IStockCallback;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
......@@ -42,6 +43,9 @@ public class StockService {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired(required = false)
private CyRedisService cyRedisServiceImpl;
/**
* 执行扣库存的脚本
*/
......@@ -152,29 +156,31 @@ public class StockService {
* @return
*/
public long addStock(String key, Long expire, int num) {
boolean hasKey = redisTemplate.hasKey(key);
Object value = cyRedisServiceImpl.get(key);
// 判断key是否存在,存在就直接更新
if (hasKey) {
if (value != null) {
redisTemplate.opsForValue().increment(key, num);
return num;
}
Assert.notNull(expire,"初始化库存失败,库存过期时间不能为null");
RLock redisLock = redissonClient.getLock(key);
try {
if (redisLock.tryLock()) {
// 获取到锁后再次判断一下是否有key
hasKey = redisTemplate.hasKey(key);
if (!hasKey) {
// 初始化库存
redisTemplate.opsForValue().set(key, num, expire, TimeUnit.SECONDS);
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
redisLock.unlock();
}
//
// if (expire != null) {
// Assert.notNull(expire, "初始化库存失败,库存过期时间不能为null");
// RLock redisLock = redissonClient.getLock(key);
// try {
// if (redisLock.tryLock()) {
// // 获取到锁后再次判断一下是否有key
// value = cyRedisServiceImpl.get(key)
// if (hasKey == null) {
// // 初始化库存
// redisTemplate.opsForValue().set(key, num, expire, TimeUnit.SECONDS);
// }
// }
// } catch (Exception e) {
// logger.error(e.getMessage(), e);
// } finally {
// redisLock.unlock();
// }
// }
return num;
}
......
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