Authored by LUOXC

fixbug

package com.yohoufo.common.cache;
import com.yoho.core.redis.cluster.annotation.Redis;
import com.yoho.core.redis.cluster.operations.nosync.YHRedisTemplate;
import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
@Slf4j
public class RedisLock {
@Redis("gwNoSyncRedis")
private YHRedisTemplate redis;
@Redis("gwNoSyncRedis")
private YHValueOperations valueOperations;
public boolean acquire(RedisKeyBuilder key, final long timeout, final TimeUnit unit) {
try {
String redisLockValue = "Y";
if (redisLockValue.equals(valueOperations.get(key))) {
return false;
}
valueOperations.set(key, redisLockValue, timeout, unit);
return true;
} catch (Exception e) {
release(key);
return false;
}
}
public void release(RedisKeyBuilder key) {
try {
redis.delete(key);
} catch (Exception e) {
try {
redis.delete(key);
} catch (Exception e1) {
log.warn("release lock {} fail", key, e);
}
}
}
}
... ...
package com.yohoufo.order.service.seller.quickDeliver;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohobuy.ufo.model.order.bo.GoodsInfo;
... ... @@ -15,6 +16,8 @@ import com.yohobuy.ufo.model.order.resp.PageResp;
import com.yohobuy.ufo.model.order.resp.SellerGoodsPageResp;
import com.yohoufo.common.alarm.EventBusPublisher;
import com.yohoufo.common.alarm.SmsAlarmEvent;
import com.yohoufo.common.cache.RedisLock;
import com.yohoufo.common.exception.UfoServiceException;
import com.yohoufo.dal.order.SellerOrderGoodsViewMapper;
import com.yohoufo.dal.order.model.SellerOrderGoods;
import com.yohoufo.order.model.QuickDeliverOrderContext;
... ... @@ -37,6 +40,7 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* @author chenchao
... ... @@ -76,10 +80,23 @@ public class QuickDeliverGoodsService {
@Autowired
private SellerNoticeFacade sellerNoticeFacade;
public DepositPublishResp publish(QuickDeliverOrderSubmitReq req){
@Autowired
private RedisLock redisLock;
QuickDeliverOrderContext qdoc = quickDeliverPublishProcessor.buildPublishCtx(req);
return doPublish(qdoc);
public DepositPublishResp publish(QuickDeliverOrderSubmitReq req) {
RedisKeyBuilder redisLockKey = RedisKeyBuilder.newInstance()
.appendFixed("ufo:order:lock:publishQuickDeliverGoods:")
.appendVar(req.getUid() + "-" + req.getStorageId());
if (redisLock.acquire(redisLockKey, 1, TimeUnit.SECONDS)) {
try {
QuickDeliverOrderContext qdoc = quickDeliverPublishProcessor.buildPublishCtx(req);
return doPublish(qdoc);
} finally {
redisLock.release(redisLockKey);
}
} else {
throw new UfoServiceException(400, "寄存商品上架中,稍后再试");
}
}
... ...