From a28e5d751bcc3f3309fe90d39bbee44fa56e2a4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=91=E5=B9=B4=E6=A2=A6=E4=B8=8E=E7=A0=96?= <2847127106@qq.com> Date: Tue, 1 Oct 2024 19:54:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BF=A1=E6=81=AF=E5=AE=8C=E6=88=90Caffeine?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E7=AE=A1=E7=90=86=E5=99=A8=E7=9A=84=E5=88=86?= =?UTF-8?q?=E5=8C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/caffeine/bean/CaffeineManager.java | 31 ++++++++++--------- .../rebbit/GoOnlineRabbitConsumer.java | 5 +-- .../src/main/resources/bootstrap.yml | 2 ++ 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/bean/CaffeineManager.java b/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/bean/CaffeineManager.java index 3eac18a..768553b 100644 --- a/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/bean/CaffeineManager.java +++ b/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/bean/CaffeineManager.java @@ -1,7 +1,8 @@ package com.muyu.common.caffeine.bean; -import com.github.benmanes.caffeine.cache.Cache; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import com.github.benmanes.caffeine.cache.Caffeine; import org.springframework.cache.CacheManager; import org.springframework.cache.caffeine.CaffeineCache; @@ -10,6 +11,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import java.util.ArrayList; +import java.util.List; /** @@ -19,8 +21,13 @@ import java.util.ArrayList; * @CreatedDate: 2024/9/26 上午11:52 * @FilePath: com.muyu.common.caffeine.config */ + +@Slf4j @Component public class CaffeineManager { + @Value("#{'${cacheNames}'.split(',')}") + private List cacheNames; + /** * 创建缓存管理器 * @return 缓存管理器实例 @@ -28,21 +35,15 @@ public class CaffeineManager { @Bean public CacheManager cacheManager() { SimpleCacheManager cacheManager = new SimpleCacheManager(); - cacheManager.setCaches(getCaches()); + cacheManager.setCaches(cacheNames.stream() + .map(name -> new CaffeineCache( + name, + Caffeine.newBuilder() + .recordStats() + .build())) + .toList()); + log.info("缓存管理器初始化完成,缓存分区:{}", cacheNames); return cacheManager; } - - private ArrayList getCaches() { - ArrayList caches = new ArrayList<>(); - // 故障信息缓存 - caches.add(new CaffeineCache("fault", Caffeine.newBuilder().build())); - // 围栏信息缓存 - caches.add(new CaffeineCache("fence", Caffeine.newBuilder().build())); - // 预警策略规则缓存 - caches.add(new CaffeineCache("warming", Caffeine.newBuilder().build())); - return caches; - } - - } diff --git a/cloud-data-processing/src/main/java/com/muyu/data/processing/rebbit/GoOnlineRabbitConsumer.java b/cloud-data-processing/src/main/java/com/muyu/data/processing/rebbit/GoOnlineRabbitConsumer.java index 195e182..726ace3 100644 --- a/cloud-data-processing/src/main/java/com/muyu/data/processing/rebbit/GoOnlineRabbitConsumer.java +++ b/cloud-data-processing/src/main/java/com/muyu/data/processing/rebbit/GoOnlineRabbitConsumer.java @@ -28,10 +28,7 @@ import java.io.IOException; public class GoOnlineRabbitConsumer { @Resource private RedisTemplate redisTemplate; -// @Resource -// private SimpleCacheManager simpleCacheManager; - @Resource - private CacheUtils cacheUtils; + @RabbitListener(queuesToDeclare = {@Queue(RabbitConstants.GO_ONLINE_QUEUE)}) diff --git a/cloud-data-processing/src/main/resources/bootstrap.yml b/cloud-data-processing/src/main/resources/bootstrap.yml index 4bbeb8f..b40171b 100644 --- a/cloud-data-processing/src/main/resources/bootstrap.yml +++ b/cloud-data-processing/src/main/resources/bootstrap.yml @@ -68,3 +68,5 @@ spring: logging: level: com.muyu.system.mapper: DEBUG + +cacheNames: fault,fence,warming