From 8741c20ddbfdb4c366e54c9dea1ba613efe63c58 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: Thu, 26 Sep 2024 19:26:50 +0800 Subject: [PATCH] =?UTF-8?q?Caffeine=E7=BC=93=E5=AD=98=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloud-common/cloud-common-caffeine/pom.xml | 37 +++++++ .../common/caffeine/CaffeineCacheUtils.java | 98 +++++++++++++++++++ .../caffeine/bean/CaffeineManagerBean.java | 37 +++++++ .../caffeine/constents/CaffeineContent.java | 16 +++ ...ot.autoconfigure.AutoConfiguration.imports | 3 + 5 files changed, 191 insertions(+) create mode 100644 cloud-common/cloud-common-caffeine/pom.xml create mode 100644 cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/CaffeineCacheUtils.java create mode 100644 cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/bean/CaffeineManagerBean.java create mode 100644 cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/constents/CaffeineContent.java create mode 100644 cloud-common/cloud-common-caffeine/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports diff --git a/cloud-common/cloud-common-caffeine/pom.xml b/cloud-common/cloud-common-caffeine/pom.xml new file mode 100644 index 0000000..70fbb7d --- /dev/null +++ b/cloud-common/cloud-common-caffeine/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + com.muyu + cloud-common + 3.6.3 + + + cloud-common-caffeine + + + 17 + 17 + UTF-8 + + + + + com.muyu + cloud-common-redis + + + + com.github.ben-manes.caffeine + caffeine + 2.9.3 + + + com.github.ben-manes.caffeine + caffeine + + + + diff --git a/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/CaffeineCacheUtils.java b/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/CaffeineCacheUtils.java new file mode 100644 index 0000000..88c8e78 --- /dev/null +++ b/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/CaffeineCacheUtils.java @@ -0,0 +1,98 @@ +package com.muyu.common.caffeine; + + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.muyu.common.caffeine.constents.CaffeineContent; +import com.muyu.common.redis.service.RedisService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.ObjectUtils; +import org.springframework.cache.caffeine.CaffeineCache; +import org.springframework.cache.support.SimpleCacheManager; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.TimeUnit; /** + * @Author: 胡杨 + * @Name: CaffeineUtils + * @Description: 缓存工具类 + * @CreatedDate: 2024/9/26 下午2:53 + * @FilePath: com.muyu.common.caffeine + */ +@Slf4j +@Component +public class CaffeineCacheUtils { + @Resource + private RedisService redisService; + @Resource + private SimpleCacheManager simpleCacheManager; + + + /** + * 车辆上线 - 新增缓存 + */ + public void addCarCache(String vin) { + ArrayList caches = new ArrayList<>(); + // 从Redis中获取缓存信息 + Map cacheMap = redisService.getCacheMap(CaffeineContent.CAR_VIN+vin); + cacheMap.forEach((key, value) -> { + Cache cache = Caffeine.newBuilder().build(); + cache.put(key, value); + // 全部存储到 CaffeineCache集合 + caches.add(new CaffeineCache(vin, cache)); + }); + simpleCacheManager.setCaches(caches); + log.info("车辆编码:{},本地缓存完成...",vin); + } + + /** + * 车辆下线 - 删除缓存 + */ + public void deleteCarCache(String vin) { + if (hasCarVinCache(vin)) { + log.warn("车辆编码:{},本地缓存不存在该车辆信息...", vin); + return; + } + simpleCacheManager.getCache(vin).invalidate(); + log.info("车辆编码:{},本地缓存删除完成...", vin); + } + + /** + * 获取车辆信息缓存 + */ + public Object getCarCache(String vin, String key) { + if (hasCarVinKeyCache(vin, key)){ + log.warn("车辆编码:{},本地缓存不存在该车辆信息...",vin); + return null; + } + return simpleCacheManager.getCache(vin).get(key).get(); + } + + /** + * 获取车辆信息缓存 + */ + public T getCarCache(String vin, String key, Class type) { + if (hasCarVinKeyCache(vin,key)){ + log.warn("车辆编码:{},本地缓存不存在该车辆信息...",vin); + return null; + } + return simpleCacheManager.getCache(vin).get(key, type); + } + + /** + * 判断缓存存在与否 + */ + public Boolean hasCarVinCache(String vin) { + return ObjectUtils.isNotEmpty(simpleCacheManager.getCache(vin)); + } + + /** + * 判断缓存的Key存在与否 + */ + public Boolean hasCarVinKeyCache(String vin,String key) { + return hasCarVinCache(vin) && ObjectUtils.isNotEmpty(simpleCacheManager.getCache(vin).get(key).get()); + } +} diff --git a/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/bean/CaffeineManagerBean.java b/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/bean/CaffeineManagerBean.java new file mode 100644 index 0000000..5c7f922 --- /dev/null +++ b/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/bean/CaffeineManagerBean.java @@ -0,0 +1,37 @@ +package com.muyu.common.caffeine.bean; + + +import com.github.benmanes.caffeine.cache.Cache; +import com.muyu.common.redis.service.RedisService; +import org.springframework.cache.CacheManager; +import org.springframework.cache.caffeine.CaffeineCache; +import org.springframework.cache.support.SimpleCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.ArrayList; + + +/** + * @Author: 胡杨 + * @Name: CaffeineCacheConfig + * @Description: Caffeine管理器 + * @CreatedDate: 2024/9/26 上午11:52 + * @FilePath: com.muyu.common.caffeine.config + */ +@Component +public class CaffeineManagerBean { + + /** + * 创建缓存管理器 + * @return 缓存管理器实例 + */ + @Bean + public SimpleCacheManager simpleCacheManager() { + return new SimpleCacheManager(); + } + + + +} diff --git a/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/constents/CaffeineContent.java b/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/constents/CaffeineContent.java new file mode 100644 index 0000000..faa0c1b --- /dev/null +++ b/cloud-common/cloud-common-caffeine/src/main/java/com/muyu/common/caffeine/constents/CaffeineContent.java @@ -0,0 +1,16 @@ +package com.muyu.common.caffeine.constents; + +/** + * @Author: 胡杨 + * @Name: CaffeineContent + * @Description: Caffeine常量 + * @CreatedDate: 2024/9/26 下午12:06 + * @FilePath: com.muyu.common.caffeine.constents + */ + +public class CaffeineContent { + + public static final String CAR_VIN = "car:Vin"; + + public static final String VIN = "vin"; +} diff --git a/cloud-common/cloud-common-caffeine/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/cloud-common/cloud-common-caffeine/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..70148cf --- /dev/null +++ b/cloud-common/cloud-common-caffeine/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1,3 @@ +com.muyu.cloud.common.saas.interceptor.WebMvcSaaSConfig +com.muyu.cloud.common.many.datasource.ManyDataSource +com.muyu.cloud.common.many.datasource.factory.DruidDataSourceFactory