From fb80b235ed9958580d908be000b5a5ad92c8274e Mon Sep 17 00:00:00 2001 From: stylefeng Date: Fri, 10 Jan 2025 15:07:26 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=908.3.3=E3=80=91=E3=80=90dict=E3=80=91?= =?UTF-8?q?=E3=80=90cache=E3=80=91=E6=B7=BB=E5=8A=A0=E5=AD=97=E5=85=B8?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel-s-dict/dict-business/pom.xml | 11 +++- .../modular/cache/DictInfoMemoryCache.java | 27 +++++++++ .../modular/cache/DictInfoRedisCache.java | 27 +++++++++ .../modular/constants/DictCacheConstants.java | 40 +++++++++++++ .../dict-spring-boot-starter/pom.xml | 14 +++++ .../DictMemoryCacheAutoConfiguration.java | 58 ++++++++++++++++++ .../DictRedisCacheAutoConfiguration.java | 59 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 3 + 8 files changed, 237 insertions(+), 2 deletions(-) create mode 100644 kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/cache/DictInfoMemoryCache.java create mode 100644 kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/cache/DictInfoRedisCache.java create mode 100644 kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/constants/DictCacheConstants.java create mode 100644 kernel-s-dict/dict-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/dict/starter/DictMemoryCacheAutoConfiguration.java create mode 100644 kernel-s-dict/dict-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/dict/starter/DictRedisCacheAutoConfiguration.java create mode 100644 kernel-s-dict/dict-spring-boot-starter/src/main/resources/META-INF/spring.factories diff --git a/kernel-s-dict/dict-business/pom.xml b/kernel-s-dict/dict-business/pom.xml index 8a7e48de7..3db42ed45 100644 --- a/kernel-s-dict/dict-business/pom.xml +++ b/kernel-s-dict/dict-business/pom.xml @@ -25,11 +25,18 @@ - + com.javaguns.roses - cache-api + cache-sdk-memory ${roses.version} + true + + + com.javaguns.roses + cache-sdk-redis + ${roses.version} + true diff --git a/kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/cache/DictInfoMemoryCache.java b/kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/cache/DictInfoMemoryCache.java new file mode 100644 index 000000000..cb55b7350 --- /dev/null +++ b/kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/cache/DictInfoMemoryCache.java @@ -0,0 +1,27 @@ +package cn.stylefeng.roses.kernel.dict.modular.cache; + +import cn.hutool.cache.impl.TimedCache; +import cn.stylefeng.roses.kernel.cache.memory.AbstractMemoryCacheOperator; +import cn.stylefeng.roses.kernel.dict.api.pojo.DictDetail; +import cn.stylefeng.roses.kernel.dict.modular.constants.DictCacheConstants; + +/** + * 字典信息的缓存 + *

+ * key是职位id,value是字典详情 + * + * @author fengshuonan + * @since 2025/1/10 14:55 + */ +public class DictInfoMemoryCache extends AbstractMemoryCacheOperator { + + public DictInfoMemoryCache(TimedCache timedCache) { + super(timedCache); + } + + @Override + public String getCommonKeyPrefix() { + return DictCacheConstants.DICT_INFO_CACHE_PREFIX; + } + +} \ No newline at end of file diff --git a/kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/cache/DictInfoRedisCache.java b/kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/cache/DictInfoRedisCache.java new file mode 100644 index 000000000..7dec7a5e5 --- /dev/null +++ b/kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/cache/DictInfoRedisCache.java @@ -0,0 +1,27 @@ +package cn.stylefeng.roses.kernel.dict.modular.cache; + +import cn.stylefeng.roses.kernel.cache.redis.AbstractRedisCacheOperator; +import cn.stylefeng.roses.kernel.dict.api.pojo.DictDetail; +import cn.stylefeng.roses.kernel.dict.modular.constants.DictCacheConstants; +import org.springframework.data.redis.core.RedisTemplate; + +/** + * 字典信息的缓存 + *

+ * key是职位id,value是字典详情 + * + * @author fengshuonan + * @since 2025/1/10 14:10 + */ +public class DictInfoRedisCache extends AbstractRedisCacheOperator { + + public DictInfoRedisCache(RedisTemplate redisTemplate) { + super(redisTemplate); + } + + @Override + public String getCommonKeyPrefix() { + return DictCacheConstants.DICT_INFO_CACHE_PREFIX; + } + +} diff --git a/kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/constants/DictCacheConstants.java b/kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/constants/DictCacheConstants.java new file mode 100644 index 000000000..cd3dbc22f --- /dev/null +++ b/kernel-s-dict/dict-business/src/main/java/cn/stylefeng/roses/kernel/dict/modular/constants/DictCacheConstants.java @@ -0,0 +1,40 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * 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. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.dict.modular.constants; + +/** + * 字典的缓存常量 + * + * @author fengshuonan + * @since 2025/1/10 14:50 + */ +public interface DictCacheConstants { + + /** + * 字典信息的缓存前缀 + */ + String DICT_INFO_CACHE_PREFIX = "DICT:INFO:"; + +} diff --git a/kernel-s-dict/dict-spring-boot-starter/pom.xml b/kernel-s-dict/dict-spring-boot-starter/pom.xml index fdb963190..1f66241d9 100644 --- a/kernel-s-dict/dict-spring-boot-starter/pom.xml +++ b/kernel-s-dict/dict-spring-boot-starter/pom.xml @@ -31,6 +31,20 @@ ${roses.version} + + + com.javaguns.roses + cache-sdk-memory + ${roses.version} + true + + + com.javaguns.roses + cache-sdk-redis + ${roses.version} + true + + diff --git a/kernel-s-dict/dict-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/dict/starter/DictMemoryCacheAutoConfiguration.java b/kernel-s-dict/dict-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/dict/starter/DictMemoryCacheAutoConfiguration.java new file mode 100644 index 000000000..fb2421972 --- /dev/null +++ b/kernel-s-dict/dict-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/dict/starter/DictMemoryCacheAutoConfiguration.java @@ -0,0 +1,58 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * 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. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.dict.starter; + +import cn.hutool.cache.CacheUtil; +import cn.hutool.cache.impl.TimedCache; +import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; +import cn.stylefeng.roses.kernel.dict.api.pojo.DictDetail; +import cn.stylefeng.roses.kernel.dict.modular.cache.DictInfoMemoryCache; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * 字典的内存缓存 + * + * @author fengshuonan + * @since 2025/1/10 15:03 + */ +@Configuration +@ConditionalOnMissingClass("org.springframework.data.redis.connection.RedisConnectionFactory") +public class DictMemoryCacheAutoConfiguration { + + /** + * 字典的缓存 + * + * @author fengshuonan + * @since 2025/1/10 15:03 + */ + @Bean + public CacheOperatorApi dictInfoCache() { + TimedCache themeCache = CacheUtil.newTimedCache(1000 * 3600L); + return new DictInfoMemoryCache(themeCache); + } + +} diff --git a/kernel-s-dict/dict-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/dict/starter/DictRedisCacheAutoConfiguration.java b/kernel-s-dict/dict-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/dict/starter/DictRedisCacheAutoConfiguration.java new file mode 100644 index 000000000..e81f3b694 --- /dev/null +++ b/kernel-s-dict/dict-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/dict/starter/DictRedisCacheAutoConfiguration.java @@ -0,0 +1,59 @@ +/* + * Copyright [2020-2030] [https://www.stylefeng.cn] + * + * 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. + * + * Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: + * + * 1.请不要删除和修改根目录下的LICENSE文件。 + * 2.请不要删除和修改Guns源码头部的版权声明。 + * 3.请保留源码和相关描述文件的项目出处,作者声明等。 + * 4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns + * 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns + * 6.若您的项目无法满足以上几点,可申请商业授权 + */ +package cn.stylefeng.roses.kernel.dict.starter; + +import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi; +import cn.stylefeng.roses.kernel.cache.redis.util.CreateRedisTemplateUtil; +import cn.stylefeng.roses.kernel.dict.api.pojo.DictDetail; +import cn.stylefeng.roses.kernel.dict.modular.cache.DictInfoRedisCache; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; + +/** + * 字典的Redis缓存 + * + * @author fengshuonan + * @since 2025/1/10 15:03 + */ +@Configuration +@ConditionalOnClass(name = "org.springframework.data.redis.connection.RedisConnectionFactory") +public class DictRedisCacheAutoConfiguration { + + /** + * 字典的Redis缓存 + * + * @author fengshuonan + * @since 2025/1/10 15:05 + */ + @Bean + public CacheOperatorApi dictInfoCache(RedisConnectionFactory redisConnectionFactory) { + RedisTemplate redisTemplate = CreateRedisTemplateUtil.createObject(redisConnectionFactory); + return new DictInfoRedisCache(redisTemplate); + } + +} diff --git a/kernel-s-dict/dict-spring-boot-starter/src/main/resources/META-INF/spring.factories b/kernel-s-dict/dict-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..a7d6fe456 --- /dev/null +++ b/kernel-s-dict/dict-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + cn.stylefeng.roses.kernel.dict.starter.DictMemoryCacheAutoConfiguration,\ + cn.stylefeng.roses.kernel.dict.starter.DictRedisCacheAutoConfiguration \ No newline at end of file