瀏覽代碼

perf(消费服务): 账户余额的缓存操作不再直接使用RedisUtils操作,而是使用spring-cache 的注解操作

autumnal_wind 11 月之前
父節點
當前提交
52fc236274

+ 1 - 4
ruoyi-server/ruoyi-server-consume/src/main/java/org/dromara/server/consume/business/BaseBusiness.java

@@ -213,10 +213,7 @@ public class BaseBusiness {
      */
     public void resetUserBalance(Long userId, BigDecimal balance) {
         log.info("[请求交易完成]-[更新人员余额缓存]");
-        String strUserId = String.valueOf(userId);
-
-        RedisUtils.delCacheMapValue(CacheNames.USER_TOTAL_BALANCE, strUserId);
-        RedisUtils.setCacheMapValue(CacheNames.USER_TOTAL_BALANCE, strUserId, balance);
+        bagService.cacheUserTotalBalance(userId, balance);
     }
 
     /**

+ 7 - 5
ruoyi-server/ruoyi-server-consume/src/main/java/org/dromara/server/consume/business/InitBusiness.java

@@ -169,16 +169,18 @@ public class InitBusiness {
      */
     public void initUserBalance() {
         List<PtBagVo> list = bagService.selectBalanceBag();
-        Map<String, BigDecimal> sumMap = new HashMap<>();
+        Map<Long, BigDecimal> sumMap = new HashMap<>();
         for (PtBagVo vo : list) {
-            String userId = String.valueOf(vo.getUserId());
+            Long userId = vo.getUserId();
             BigDecimal amount = vo.getBalance();
             sumMap.merge(userId, amount, BigDecimal::add);
         }
-        for (Map.Entry<String, BigDecimal> entry : sumMap.entrySet()) {
-            RedisUtils.setCacheMapValue(CacheNames.USER_TOTAL_BALANCE, entry.getKey(), entry.getValue());
+        for (Map.Entry<Long, BigDecimal> entry : sumMap.entrySet()) {
+            // RedisUtils.setCacheMapValue(CacheNames.USER_TOTAL_BALANCE, entry.getKey(), entry.getValue());
+            bagService.cacheUserTotalBalance(entry.getKey(), entry.getValue());
         }
-        RedisUtils.expire(CacheNames.USER_TOTAL_BALANCE, Duration.ofHours(5));
+
+        // RedisUtils.expire(CacheNames.USER_TOTAL_BALANCE, Duration.ofHours(5));
         log.info("初始化人员余额参数完成");
     }
 

+ 2 - 0
ruoyi-server/ruoyi-server-consume/src/main/java/org/dromara/server/consume/service/IPtBagService.java

@@ -117,5 +117,7 @@ public interface IPtBagService {
     void updateConsumeBalance(List<PtBagVo> bagVos);
 
     List<PtBagVo> selectBalanceBag();
+
+    BigDecimal cacheUserTotalBalance(Long userId, BigDecimal balance);
     // boolean updateBalanceByByConsume(PtBagBo bo);
 }

+ 6 - 0
ruoyi-server/ruoyi-server-consume/src/main/java/org/dromara/server/consume/service/impl/PtBagServiceImpl.java

@@ -25,6 +25,7 @@ import org.dromara.server.consume.domain.bo.PtBagBo;
 import org.dromara.server.consume.domain.vo.PtBagVo;
 import org.dromara.server.consume.mapper.PtBagMapper;
 import org.dromara.server.consume.service.IPtBagService;
+import org.springframework.cache.annotation.CachePut;
 import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Propagation;
@@ -445,4 +446,9 @@ public class PtBagServiceImpl implements IPtBagService {
         bag.setEncryptBalance(encryptValue);
     }
 
+    @Override
+    @CachePut(cacheNames = CacheNames.USER_TOTAL_BALANCE, key = "#userId")
+    public BigDecimal cacheUserTotalBalance(Long userId, BigDecimal balance) {
+        return balance;
+    }
 }