mirror of
https://github.com/201206030/novel.git
synced 2025-04-27 07:30:50 +00:00
build(cache): 集成配置 caffeine 本地缓存和 redis 远程缓存
This commit is contained in:
parent
b8113b0176
commit
051fd6342e
14
pom.xml
14
pom.xml
@ -58,6 +58,20 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- 缓存相关 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-cache</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||
<artifactId>caffeine</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
|
@ -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";
|
||||
}
|
@ -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<CaffeineCache> caches = new ArrayList<>(CacheConsts.CacheEnum.values().length);
|
||||
for (CacheConsts.CacheEnum c : CacheConsts.CacheEnum.values()) {
|
||||
if (c.isLocal()) {
|
||||
Caffeine<Object, Object> 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<String, RedisCacheConfiguration> 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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package io.github.xxyopen.novel.core.constant;
|
||||
|
||||
/**
|
||||
* 系统配置相关常量
|
||||
*
|
||||
* @author xiongxiaoyang
|
||||
* @date 2022/5/12
|
||||
*/
|
||||
public class SystemConfigConsts {
|
||||
}
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user