refactor: 适配 ContiNew Starter Spring Cache(缓存模块)

This commit is contained in:
Charles7c 2024-01-13 23:54:04 +08:00
parent 706e488e84
commit 754de79200
5 changed files with 64 additions and 156 deletions

View File

@ -81,6 +81,12 @@
<artifactId>continew-starter-data-mybatis-plus</artifactId> <artifactId>continew-starter-data-mybatis-plus</artifactId>
</dependency> </dependency>
<!-- ContiNew Starter 缓存模块 - Spring Cache -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-cache-springcache</artifactId>
</dependency>
<!-- ContiNew Starter 消息模块 - 邮件 --> <!-- ContiNew Starter 消息模块 - 邮件 -->
<dependency> <dependency>
<groupId>top.charles7c.continew</groupId> <groupId>top.charles7c.continew</groupId>

View File

@ -1,92 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.continew.admin.common.config;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.*;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.json.JSONUtil;
/**
* Spring Cache 配置
*
* @author Charles7c
* @since 2022/12/28 23:17
*/
@Slf4j
@EnableCaching
@Configuration
@RequiredArgsConstructor
public class SpringCacheConfiguration extends CachingConfigurerSupport {
private final ObjectMapper objectMapper;
/**
* 解决 Spring Cache@Cacheable缓存乱码问题
*/
@Bean
public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
ObjectMapper objectMapperCopy = objectMapper.copy();
objectMapperCopy.activateDefaultTyping(objectMapperCopy
.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair
.fromSerializer(new GenericJackson2JsonRedisSerializer(objectMapperCopy)));
CacheProperties.Redis redisCacheProperties = cacheProperties.getRedis();
if (null != redisCacheProperties.getTimeToLive()) {
redisCacheConfiguration = redisCacheConfiguration.entryTtl(redisCacheProperties.getTimeToLive());
}
if (!redisCacheProperties.isCacheNullValues()) {
redisCacheConfiguration = redisCacheConfiguration.disableCachingNullValues();
}
return redisCacheConfiguration;
}
/**
* 自定义缓存 key 生成策略如果 @Cacheable 不指定 key则默认使用该策略
*/
@Bean
@Override
public KeyGenerator keyGenerator() {
return (target, method, params) -> {
String key = StrUtil.toUnderlineCase(method.getName()).toUpperCase();
Map<String, Object> paramMap = MapUtil.newHashMap(params.length);
for (int i = 0; i < params.length; i++) {
paramMap.put(String.valueOf(i), params[i]);
}
return String.format("%s:%s", key, DigestUtil.sha256Hex(JSONUtil.toJsonStr(paramMap)));
};
}
}

View File

@ -16,28 +16,25 @@
package top.charles7c.continew.admin; package top.charles7c.continew.admin;
import java.net.InetAddress; import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.URLUtil;
import cn.hutool.extra.spring.SpringUtil;
import io.swagger.v3.oas.annotations.Hidden;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import io.swagger.v3.oas.annotations.Hidden;
import org.dromara.x.file.storage.spring.EnableFileStorage; import org.dromara.x.file.storage.spring.EnableFileStorage;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.URLUtil;
import cn.hutool.extra.spring.SpringUtil;
import top.charles7c.continew.starter.core.autoconfigure.project.ProjectProperties; import top.charles7c.continew.starter.core.autoconfigure.project.ProjectProperties;
import java.net.InetAddress;
/** /**
* 启动程序 * 启动程序
* *
@ -45,6 +42,7 @@ import top.charles7c.continew.starter.core.autoconfigure.project.ProjectProperti
* @since 2022/12/8 23:15 * @since 2022/12/8 23:15
*/ */
@Slf4j @Slf4j
@EnableCaching
@EnableFileStorage @EnableFileStorage
@RestController @RestController
@SpringBootApplication @SpringBootApplication

View File

@ -55,33 +55,31 @@ spring.liquibase:
# 配置文件路径 # 配置文件路径
change-log: classpath:/db/changelog/db.changelog-master.yaml change-log: classpath:/db/changelog/db.changelog-master.yaml
--- ### Redis 配置(单机版) --- ### 缓存配置
spring.data: spring:
redis: ## Spring Cache 配置
# 地址 cache:
host: ${REDIS_HOST:127.0.0.1} type: REDIS
# 端口(默认 6379 data:
port: ${REDIS_PORT:6379} ## Redis 配置(单机版)
# 密码(未设置密码时可为空或注释掉) redis:
password: ${REDIS_PWD:123456} # 地址
# 数据库索引 host: ${REDIS_HOST:127.0.0.1}
database: ${REDIS_DB:0} # 端口(默认 6379
# 连接超时时间 port: ${REDIS_PORT:6379}
timeout: 10s # 密码(未设置密码时可为空或注释掉)
# 是否开启 SSL password: ${REDIS_PWD:123456}
ssl: # 数据库索引
enabled: false database: ${REDIS_DB:0}
redisson: # 连接超时时间
enabled: true timeout: 10s
mode: SINGLE # 是否开启 SSL
ssl:
--- ### Spring Cache 配置 enabled: false
spring.cache: # Redisson 配置
redis: redisson:
# 缓存过期时长(单位:毫秒,默认 -1表示永不过期 enabled: true
time-to-live: 7200000 mode: SINGLE
# 是否允许缓存空值(默认 true表示允许可以解决缓存穿透问题
cache-null-values: true
--- ### 验证码配置 --- ### 验证码配置
continew-starter.captcha: continew-starter.captcha:

View File

@ -57,33 +57,31 @@ spring.liquibase:
# 配置文件路径 # 配置文件路径
change-log: classpath:/db/changelog/db.changelog-master.yaml change-log: classpath:/db/changelog/db.changelog-master.yaml
--- ### Redis 配置(单机版) --- ### 缓存配置
spring.data: spring:
redis: ## Spring Cache 配置
# 地址 cache:
host: ${REDIS_HOST:127.0.0.1} type: REDIS
# 端口(默认 6379 data:
port: ${REDIS_PORT:6379} ## Redis 配置(单机版)
# 密码(未设置密码时可为空或注释掉) redis:
password: ${REDIS_PWD:123456} # 地址
# 数据库索引 host: ${REDIS_HOST:127.0.0.1}
database: ${REDIS_DB:0} # 端口(默认 6379
# 连接超时时间 port: ${REDIS_PORT:6379}
timeout: 10s # 密码(未设置密码时可为空或注释掉)
# 是否开启 SSL password: ${REDIS_PWD:123456}
ssl: # 数据库索引
enabled: false database: ${REDIS_DB:0}
redisson: # 连接超时时间
enabled: true timeout: 10s
mode: SINGLE # 是否开启 SSL
ssl:
--- ### Spring Cache 配置 enabled: false
spring.cache: # Redisson 配置
redis: redisson:
# 缓存过期时长(单位:毫秒,默认 -1表示永不过期 enabled: true
time-to-live: 7200000 mode: SINGLE
# 是否允许缓存空值(默认 true表示允许可以解决缓存穿透问题
cache-null-values: true
--- ### 验证码配置 --- ### 验证码配置
continew-starter.captcha: continew-starter.captcha: