Authored by LUOXC

fixbug

  1 +package com.yohoufo.common.cache;
  2 +
  3 +import com.yoho.core.redis.cluster.annotation.Redis;
  4 +import com.yoho.core.redis.cluster.operations.nosync.YHRedisTemplate;
  5 +import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations;
  6 +import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
  7 +import lombok.extern.slf4j.Slf4j;
  8 +import org.springframework.stereotype.Service;
  9 +
  10 +import java.util.concurrent.TimeUnit;
  11 +
  12 +@Service
  13 +@Slf4j
  14 +public class RedisLock {
  15 +
  16 + @Redis("gwNoSyncRedis")
  17 + private YHRedisTemplate redis;
  18 +
  19 + @Redis("gwNoSyncRedis")
  20 + private YHValueOperations valueOperations;
  21 +
  22 + public boolean acquire(RedisKeyBuilder key, final long timeout, final TimeUnit unit) {
  23 + try {
  24 + String redisLockValue = "Y";
  25 + if (redisLockValue.equals(valueOperations.get(key))) {
  26 + return false;
  27 + }
  28 + valueOperations.set(key, redisLockValue, timeout, unit);
  29 + return true;
  30 + } catch (Exception e) {
  31 + release(key);
  32 + return false;
  33 + }
  34 + }
  35 +
  36 + public void release(RedisKeyBuilder key) {
  37 + try {
  38 + redis.delete(key);
  39 + } catch (Exception e) {
  40 + try {
  41 + redis.delete(key);
  42 + } catch (Exception e1) {
  43 + log.warn("release lock {} fail", key, e);
  44 + }
  45 + }
  46 + }
  47 +
  48 +
  49 +}
1 package com.yohoufo.order.service.seller.quickDeliver; 1 package com.yohoufo.order.service.seller.quickDeliver;
2 2
  3 +import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
3 import com.yoho.error.ServiceError; 4 import com.yoho.error.ServiceError;
4 import com.yoho.error.exception.ServiceException; 5 import com.yoho.error.exception.ServiceException;
5 import com.yohobuy.ufo.model.order.bo.GoodsInfo; 6 import com.yohobuy.ufo.model.order.bo.GoodsInfo;
@@ -15,6 +16,8 @@ import com.yohobuy.ufo.model.order.resp.PageResp; @@ -15,6 +16,8 @@ import com.yohobuy.ufo.model.order.resp.PageResp;
15 import com.yohobuy.ufo.model.order.resp.SellerGoodsPageResp; 16 import com.yohobuy.ufo.model.order.resp.SellerGoodsPageResp;
16 import com.yohoufo.common.alarm.EventBusPublisher; 17 import com.yohoufo.common.alarm.EventBusPublisher;
17 import com.yohoufo.common.alarm.SmsAlarmEvent; 18 import com.yohoufo.common.alarm.SmsAlarmEvent;
  19 +import com.yohoufo.common.cache.RedisLock;
  20 +import com.yohoufo.common.exception.UfoServiceException;
18 import com.yohoufo.dal.order.SellerOrderGoodsViewMapper; 21 import com.yohoufo.dal.order.SellerOrderGoodsViewMapper;
19 import com.yohoufo.dal.order.model.SellerOrderGoods; 22 import com.yohoufo.dal.order.model.SellerOrderGoods;
20 import com.yohoufo.order.model.QuickDeliverOrderContext; 23 import com.yohoufo.order.model.QuickDeliverOrderContext;
@@ -37,6 +40,7 @@ import org.springframework.stereotype.Service; @@ -37,6 +40,7 @@ import org.springframework.stereotype.Service;
37 import java.math.BigDecimal; 40 import java.math.BigDecimal;
38 import java.util.Arrays; 41 import java.util.Arrays;
39 import java.util.List; 42 import java.util.List;
  43 +import java.util.concurrent.TimeUnit;
40 44
41 /** 45 /**
42 * @author chenchao 46 * @author chenchao
@@ -76,10 +80,23 @@ public class QuickDeliverGoodsService { @@ -76,10 +80,23 @@ public class QuickDeliverGoodsService {
76 @Autowired 80 @Autowired
77 private SellerNoticeFacade sellerNoticeFacade; 81 private SellerNoticeFacade sellerNoticeFacade;
78 82
79 - public DepositPublishResp publish(QuickDeliverOrderSubmitReq req){ 83 + @Autowired
  84 + private RedisLock redisLock;
80 85
81 - QuickDeliverOrderContext qdoc = quickDeliverPublishProcessor.buildPublishCtx(req);  
82 - return doPublish(qdoc); 86 + public DepositPublishResp publish(QuickDeliverOrderSubmitReq req) {
  87 + RedisKeyBuilder redisLockKey = RedisKeyBuilder.newInstance()
  88 + .appendFixed("ufo:order:lock:publishQuickDeliverGoods:")
  89 + .appendVar(req.getUid() + "-" + req.getStorageId());
  90 + if (redisLock.acquire(redisLockKey, 1, TimeUnit.SECONDS)) {
  91 + try {
  92 + QuickDeliverOrderContext qdoc = quickDeliverPublishProcessor.buildPublishCtx(req);
  93 + return doPublish(qdoc);
  94 + } finally {
  95 + redisLock.release(redisLockKey);
  96 + }
  97 + } else {
  98 + throw new UfoServiceException(400, "寄存商品上架中,稍后再试");
  99 + }
83 } 100 }
84 101
85 102