Bladeren bron

同步销量

xiongzhu 3 jaren geleden
bovenliggende
commit
7865160d09

+ 22 - 2
src/main/java/com/izouma/nineth/listener/UpdateSaleListener.java

@@ -1,5 +1,6 @@
 package com.izouma.nineth.listener;
 
+import com.izouma.nineth.config.RedisKeys;
 import com.izouma.nineth.service.CollectionService;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -7,6 +8,9 @@ import org.apache.rocketmq.spring.annotation.ConsumeMode;
 import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
 import org.apache.rocketmq.spring.core.RocketMQListener;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.data.redis.core.BoundHashOperations;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
 @Service
@@ -19,10 +23,26 @@ import org.springframework.stereotype.Service;
 @ConditionalOnProperty(value = "general.notify-server", havingValue = "false", matchIfMissing = true)
 public class UpdateSaleListener implements RocketMQListener<Long> {
 
-    private CollectionService collectionService;
+    private CollectionService             collectionService;
+    private RedisTemplate<String, Object> redisTemplate;
 
     @Override
     public void onMessage(Long id) {
-        collectionService.syncSale(id);
+        BoundHashOperations<String, String, Integer> ops = redisTemplate.boundHashOps(RedisKeys.UPDATE_SALE);
+        ops.increment(id.toString(), 1);
+    }
+
+    @Scheduled(fixedRate = 10000)
+    public void updateSale() {
+        BoundHashOperations<String, String, Integer> ops = redisTemplate.boundHashOps(RedisKeys.UPDATE_SALE);
+        for (String id : ops.keys()) {
+            Long val = ops.increment(id, -1);
+            if (val <= 0) {
+                ops.delete(id);
+            } else if (val > 1) {
+                ops.put(id, 1);
+            }
+            collectionService.syncSale(Long.parseLong(id));
+        }
     }
 }

+ 3 - 22
src/main/java/com/izouma/nineth/listener/UpdateStockListener.java

@@ -1,6 +1,5 @@
 package com.izouma.nineth.listener;
 
-import com.izouma.nineth.config.RedisKeys;
 import com.izouma.nineth.service.CollectionService;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -8,9 +7,6 @@ import org.apache.rocketmq.spring.annotation.ConsumeMode;
 import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
 import org.apache.rocketmq.spring.core.RocketMQListener;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.data.redis.core.BoundHashOperations;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
 @Service
@@ -22,26 +18,11 @@ import org.springframework.stereotype.Service;
         consumeMode = ConsumeMode.ORDERLY)
 @ConditionalOnProperty(value = "general.notify-server", havingValue = "false", matchIfMissing = true)
 public class UpdateStockListener implements RocketMQListener<Long> {
-    private CollectionService             collectionService;
-    private RedisTemplate<String, Object> redisTemplate;
+
+    private CollectionService collectionService;
 
     @Override
     public void onMessage(Long id) {
-        BoundHashOperations<String, Long, Integer> ops = redisTemplate.boundHashOps(RedisKeys.UPDATE_SALE);
-        ops.increment(id, 1);
-    }
-
-    @Scheduled(fixedRate = 5000)
-    public void updateStock() {
-        BoundHashOperations<String, Long, Integer> ops = redisTemplate.boundHashOps(RedisKeys.UPDATE_SALE);
-        for (Long id : ops.keys()) {
-            Long val = ops.increment(id, -1);
-            if (val <= 0) {
-                ops.delete(id);
-            } else if (val > 1) {
-                ops.put(id, 1);
-            }
-            collectionService.syncStock(id);
-        }
+        collectionService.syncStock(id);
     }
 }

+ 5 - 5
src/main/java/com/izouma/nineth/service/CollectionService.java

@@ -380,14 +380,14 @@ public class CollectionService {
         return stock;
     }
 
-    public synchronized Long getStock(Long id) {
+    public synchronized Integer getStock(Long id) {
         BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_STOCK + id);
-        Long stock = (Long) ops.get();
+        Integer stock = (Integer) ops.get();
         if (stock == null) {
             Boolean success = ops.setIfAbsent(Optional.ofNullable(collectionRepo.getStock(id))
                     .orElse(0), 7, TimeUnit.DAYS);
             log.info("创建redis库存:{}", success);
-            return (Long) ops.get();
+            return (Integer) ops.get();
         } else {
             return stock;
         }
@@ -414,7 +414,7 @@ public class CollectionService {
         return increaseSale(id, -number);
     }
 
-    //    @Debounce(key = "#id", delay = 500)
+    @Debounce(key = "#id", delay = 500)
     public void syncStock(Long id) {
         Integer stock = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_STOCK + id);
         if (stock != null) {
@@ -424,7 +424,7 @@ public class CollectionService {
         }
     }
 
-    @Debounce(key = "#id", delay = 500)
+//    @Debounce(key = "#id", delay = 500)
     public void syncSale(Long id) {
         Integer sale = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_SALE + id);
         if (sale != null) {

+ 1 - 1
src/main/java/com/izouma/nineth/service/OrderService.java

@@ -110,7 +110,7 @@ public class OrderService {
             throw new BusinessException("签名已过期");
         }
 
-        Long stock = collectionService.getStock(collectionId);
+        Integer stock = collectionService.getStock(collectionId);
         if (stock == null || stock <= 0) {
             throw new BusinessException("藏品已售罄", ErrorCode.SOLD_OUT);
         }