From 051fd6342eaf9061820d8ca446b37892943dd756 Mon Sep 17 00:00:00 2001 From: xiongxiaoyang <773861846@qq.com> Date: Thu, 12 May 2022 11:15:37 +0800 Subject: [PATCH] =?UTF-8?q?build(cache):=20=E9=9B=86=E6=88=90=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=20caffeine=20=E6=9C=AC=E5=9C=B0=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E5=92=8C=20redis=20=E8=BF=9C=E7=A8=8B=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 14 +++ .../core/common/constant/CommonConsts.java | 16 ++++ .../novel/core/config/CacheConfig.java | 83 ++++++++++++++++ .../novel/core/constant/CacheConsts.java | 94 +++++++++++++++++++ .../core/constant/SystemConfigConsts.java | 10 ++ src/main/resources/application.yml | 7 ++ 6 files changed, 224 insertions(+) create mode 100644 src/main/java/io/github/xxyopen/novel/core/common/constant/CommonConsts.java create mode 100644 src/main/java/io/github/xxyopen/novel/core/config/CacheConfig.java create mode 100644 src/main/java/io/github/xxyopen/novel/core/constant/CacheConsts.java create mode 100644 src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java diff --git a/pom.xml b/pom.xml index 7049195..ee65be1 100644 --- a/pom.xml +++ b/pom.xml @@ -58,6 +58,20 @@ test + + + org.springframework.boot + spring-boot-starter-cache + + + org.springframework.boot + spring-boot-starter-data-redis + + + com.github.ben-manes.caffeine + caffeine + + org.springframework.boot spring-boot-devtools diff --git a/src/main/java/io/github/xxyopen/novel/core/common/constant/CommonConsts.java b/src/main/java/io/github/xxyopen/novel/core/common/constant/CommonConsts.java new file mode 100644 index 0000000..1909516 --- /dev/null +++ b/src/main/java/io/github/xxyopen/novel/core/common/constant/CommonConsts.java @@ -0,0 +1,16 @@ +package io.github.xxyopen.novel.core.common.constant; + +/** + * 通用常量 + * + * @author xiongxiaoyang + * @date 2022/5/12 + */ +public class CommonConsts { + + private CommonConsts(){} + + public static final String YES = "yes"; + + public static final String NO = "no"; +} diff --git a/src/main/java/io/github/xxyopen/novel/core/config/CacheConfig.java b/src/main/java/io/github/xxyopen/novel/core/config/CacheConfig.java new file mode 100644 index 0000000..8a13fbe --- /dev/null +++ b/src/main/java/io/github/xxyopen/novel/core/config/CacheConfig.java @@ -0,0 +1,83 @@ +package io.github.xxyopen.novel.core.config; + +import com.github.benmanes.caffeine.cache.Caffeine; +import io.github.xxyopen.novel.core.constant.CacheConsts; +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.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheManager; +import org.springframework.data.redis.cache.RedisCacheWriter; +import org.springframework.data.redis.connection.RedisConnectionFactory; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * 缓存配置类 + * + * @author xiongxiaoyang + * @date 2022/5/12 + */ +@Configuration +public class CacheConfig { + + /** + * Caffeine 缓存管理器 + */ + @Bean + @Primary + public CacheManager caffeineCacheManager() { + SimpleCacheManager cacheManager = new SimpleCacheManager(); + + List caches = new ArrayList<>(CacheConsts.CacheEnum.values().length); + for (CacheConsts.CacheEnum c : CacheConsts.CacheEnum.values()) { + if (c.isLocal()) { + Caffeine caffeine = Caffeine.newBuilder().recordStats().maximumSize(c.getMaxSize()); + if (c.getTtl() > 0) { + caffeine.expireAfterWrite(Duration.ofSeconds(c.getTtl())); + } + caches.add(new CaffeineCache(c.getName(), caffeine.build())); + } + } + + cacheManager.setCaches(caches); + return cacheManager; + } + + /** + * Redis 缓存管理器 + */ + @Bean + public CacheManager redisCacheManager(RedisConnectionFactory connectionFactory) { + RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory); + + RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig() + .disableCachingNullValues().prefixCacheNameWith(CacheConsts.REDIS_CACHE_PREFIX); + + Map cacheMap = new LinkedHashMap<>(CacheConsts.CacheEnum.values().length); + for (CacheConsts.CacheEnum c : CacheConsts.CacheEnum.values()) { + if (c.isRemote()) { + if (c.getTtl() > 0) { + cacheMap.put(c.getName(), RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues() + .prefixCacheNameWith(CacheConsts.REDIS_CACHE_PREFIX).entryTtl(Duration.ofSeconds(c.getTtl()))); + } else { + cacheMap.put(c.getName(), RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues() + .prefixCacheNameWith(CacheConsts.REDIS_CACHE_PREFIX)); + } + } + } + + RedisCacheManager redisCacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig, cacheMap); + redisCacheManager.setTransactionAware(true); + redisCacheManager.initializeCaches(); + return redisCacheManager; + } + +} diff --git a/src/main/java/io/github/xxyopen/novel/core/constant/CacheConsts.java b/src/main/java/io/github/xxyopen/novel/core/constant/CacheConsts.java new file mode 100644 index 0000000..fd1a2b4 --- /dev/null +++ b/src/main/java/io/github/xxyopen/novel/core/constant/CacheConsts.java @@ -0,0 +1,94 @@ +package io.github.xxyopen.novel.core.constant; + +/** + * 缓存相关常量 + * + * @author xiongxiaoyang + * @date 2022/5/12 + */ +public class CacheConsts { + + /** + * 本项目 Redis 缓存前缀 + * */ + public static final String REDIS_CACHE_PREFIX = "Cache::Novel::"; + + + /** + * Caffeine 缓存管理器 + * */ + public static final String CAFFEINE_CACHE_MANAGER = "caffeineCacheManager"; + + /** + * Redis 缓存管理器 + * */ + public static final String REDIS_CACHE_MANAGER = "redisCacheManager"; + + /** + * 首页小说推荐缓存 + * */ + public static final String HOME_BOOK_CACHE_NAME = "homeBookCache"; + + /** + * 首页友情链接缓存 + * */ + public static final String HOME_FRIEND_LINK_CACHE_NAME = "homeFriendLinkCache"; + + /** + * 缓存配置常量 + */ + public enum CacheEnum { + + HOME_BOOK_CACHE(1,HOME_BOOK_CACHE_NAME,0,1), + + HOME_FRIEND_LINK_CACHE(2,HOME_FRIEND_LINK_CACHE_NAME,1000,1) + + ; + + /** + * 缓存类型 0-本地 1-本地和远程 2-远程 + */ + private int type; + /** + * 缓存的名字 + */ + private String name; + /** + * 失效时间(秒) 0-永不失效 + */ + private int ttl; + /** + * 最大容量 + */ + private int maxSize; + + CacheEnum(int type, String name, int ttl, int maxSize) { + this.type = type; + this.name = name; + this.ttl = ttl; + this.maxSize = maxSize; + } + + public boolean isLocal() { + return type <= 1; + } + + public boolean isRemote() { + return type >= 1; + } + + public String getName() { + return name; + } + + public int getTtl() { + return ttl; + } + + public int getMaxSize() { + return maxSize; + } + + } + +} diff --git a/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java b/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java new file mode 100644 index 0000000..32960f1 --- /dev/null +++ b/src/main/java/io/github/xxyopen/novel/core/constant/SystemConfigConsts.java @@ -0,0 +1,10 @@ +package io.github.xxyopen.novel.core.constant; + +/** + * 系统配置相关常量 + * + * @author xiongxiaoyang + * @date 2022/5/12 + */ +public class SystemConfigConsts { +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 74a19c3..ca8e1aa 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -3,3 +3,10 @@ spring: url: jdbc:mysql://localhost:3306/novel?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: test123456 + profiles: + active: dev + redis: + host: 127.0.0.1 + port: 6379 + password: 123456 +