diff --git a/continew-admin-common/pom.xml b/continew-admin-common/pom.xml index a26a68fb..1790fca4 100644 --- a/continew-admin-common/pom.xml +++ b/continew-admin-common/pom.xml @@ -81,6 +81,12 @@ continew-starter-data-mybatis-plus + + + top.charles7c.continew + continew-starter-cache-springcache + + top.charles7c.continew diff --git a/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/SpringCacheConfiguration.java b/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/SpringCacheConfiguration.java deleted file mode 100644 index 05438343..00000000 --- a/continew-admin-common/src/main/java/top/charles7c/continew/admin/common/config/SpringCacheConfiguration.java +++ /dev/null @@ -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 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))); - }; - } -} diff --git a/continew-admin-webapi/src/main/java/top/charles7c/continew/admin/ContiNewAdminApplication.java b/continew-admin-webapi/src/main/java/top/charles7c/continew/admin/ContiNewAdminApplication.java index ab1ed1bb..7eab9e15 100644 --- a/continew-admin-webapi/src/main/java/top/charles7c/continew/admin/ContiNewAdminApplication.java +++ b/continew-admin-webapi/src/main/java/top/charles7c/continew/admin/ContiNewAdminApplication.java @@ -16,28 +16,25 @@ 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.extern.slf4j.Slf4j; - -import io.swagger.v3.oas.annotations.Hidden; - import org.dromara.x.file.storage.spring.EnableFileStorage; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; 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.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 java.net.InetAddress; + /** * 启动程序 * @@ -45,6 +42,7 @@ import top.charles7c.continew.starter.core.autoconfigure.project.ProjectProperti * @since 2022/12/8 23:15 */ @Slf4j +@EnableCaching @EnableFileStorage @RestController @SpringBootApplication diff --git a/continew-admin-webapi/src/main/resources/config/application-dev.yml b/continew-admin-webapi/src/main/resources/config/application-dev.yml index 29b65fef..d377d860 100644 --- a/continew-admin-webapi/src/main/resources/config/application-dev.yml +++ b/continew-admin-webapi/src/main/resources/config/application-dev.yml @@ -55,33 +55,31 @@ spring.liquibase: # 配置文件路径 change-log: classpath:/db/changelog/db.changelog-master.yaml ---- ### Redis 配置(单机版) -spring.data: - redis: - # 地址 - host: ${REDIS_HOST:127.0.0.1} - # 端口(默认 6379) - port: ${REDIS_PORT:6379} - # 密码(未设置密码时可为空或注释掉) - password: ${REDIS_PWD:123456} - # 数据库索引 - database: ${REDIS_DB:0} - # 连接超时时间 - timeout: 10s - # 是否开启 SSL - ssl: - enabled: false - redisson: - enabled: true - mode: SINGLE - ---- ### Spring Cache 配置 -spring.cache: - redis: - # 缓存过期时长(单位:毫秒,默认 -1,表示永不过期) - time-to-live: 7200000 - # 是否允许缓存空值(默认 true,表示允许,可以解决缓存穿透问题) - cache-null-values: true +--- ### 缓存配置 +spring: + ## Spring Cache 配置 + cache: + type: REDIS + data: + ## Redis 配置(单机版) + redis: + # 地址 + host: ${REDIS_HOST:127.0.0.1} + # 端口(默认 6379) + port: ${REDIS_PORT:6379} + # 密码(未设置密码时可为空或注释掉) + password: ${REDIS_PWD:123456} + # 数据库索引 + database: ${REDIS_DB:0} + # 连接超时时间 + timeout: 10s + # 是否开启 SSL + ssl: + enabled: false + # Redisson 配置 + redisson: + enabled: true + mode: SINGLE --- ### 验证码配置 continew-starter.captcha: diff --git a/continew-admin-webapi/src/main/resources/config/application-prod.yml b/continew-admin-webapi/src/main/resources/config/application-prod.yml index f21b1978..0396bb3d 100644 --- a/continew-admin-webapi/src/main/resources/config/application-prod.yml +++ b/continew-admin-webapi/src/main/resources/config/application-prod.yml @@ -57,33 +57,31 @@ spring.liquibase: # 配置文件路径 change-log: classpath:/db/changelog/db.changelog-master.yaml ---- ### Redis 配置(单机版) -spring.data: - redis: - # 地址 - host: ${REDIS_HOST:127.0.0.1} - # 端口(默认 6379) - port: ${REDIS_PORT:6379} - # 密码(未设置密码时可为空或注释掉) - password: ${REDIS_PWD:123456} - # 数据库索引 - database: ${REDIS_DB:0} - # 连接超时时间 - timeout: 10s - # 是否开启 SSL - ssl: - enabled: false - redisson: - enabled: true - mode: SINGLE - ---- ### Spring Cache 配置 -spring.cache: - redis: - # 缓存过期时长(单位:毫秒,默认 -1,表示永不过期) - time-to-live: 7200000 - # 是否允许缓存空值(默认 true,表示允许,可以解决缓存穿透问题) - cache-null-values: true +--- ### 缓存配置 +spring: + ## Spring Cache 配置 + cache: + type: REDIS + data: + ## Redis 配置(单机版) + redis: + # 地址 + host: ${REDIS_HOST:127.0.0.1} + # 端口(默认 6379) + port: ${REDIS_PORT:6379} + # 密码(未设置密码时可为空或注释掉) + password: ${REDIS_PWD:123456} + # 数据库索引 + database: ${REDIS_DB:0} + # 连接超时时间 + timeout: 10s + # 是否开启 SSL + ssl: + enabled: false + # Redisson 配置 + redisson: + enabled: true + mode: SINGLE --- ### 验证码配置 continew-starter.captcha: