diff --git a/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/constants/CacheConstants.java b/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/constants/CacheConstants.java index 5b65293bd..b1ac28dcd 100644 --- a/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/constants/CacheConstants.java +++ b/kernel-d-cache/cache-api/src/main/java/cn/stylefeng/roses/kernel/cache/api/constants/CacheConstants.java @@ -28,4 +28,9 @@ public interface CacheConstants { */ Long NONE_EXPIRED_TIME = 1000L * 3600 * 24 * 999; + /** + * 默认缓存的过期时间,10分钟 + */ + Long DEFAULT_CACHE_TIMEOUT = 1000L * 60 * 10; + } diff --git a/kernel-d-cache/memory-cache-spring-boot-starter/README.md b/kernel-d-cache/memory-cache-spring-boot-starter/README.md new file mode 100644 index 000000000..ebe16886c --- /dev/null +++ b/kernel-d-cache/memory-cache-spring-boot-starter/README.md @@ -0,0 +1 @@ +内存缓存的默认配置 diff --git a/kernel-d-cache/memory-cache-spring-boot-starter/pom.xml b/kernel-d-cache/memory-cache-spring-boot-starter/pom.xml new file mode 100644 index 000000000..86e7f38e0 --- /dev/null +++ b/kernel-d-cache/memory-cache-spring-boot-starter/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + + cn.stylefeng.roses + kernel-d-cache + 1.0.0 + ../pom.xml + + + memory-cache-spring-boot-starter + + jar + + + + + + cn.stylefeng.roses + cache-sdk-memory + 1.0.0 + + + + + diff --git a/kernel-d-cache/memory-cache-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/cache/starter/memory/GunsMemoryCacheAutoConfiguration.java b/kernel-d-cache/memory-cache-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/cache/starter/memory/GunsMemoryCacheAutoConfiguration.java new file mode 100644 index 000000000..52bc67880 --- /dev/null +++ b/kernel-d-cache/memory-cache-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/cache/starter/memory/GunsMemoryCacheAutoConfiguration.java @@ -0,0 +1,40 @@ +package cn.stylefeng.roses.kernel.cache.starter.memory; + +import cn.hutool.cache.CacheUtil; +import cn.hutool.cache.impl.TimedCache; +import cn.stylefeng.roses.kernel.cache.api.constants.CacheConstants; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * 基于内存缓存的默认配置 + * + * @author fengshuonan + * @date 2021/1/31 20:32 + */ +@Configuration +public class GunsMemoryCacheAutoConfiguration { + + /** + * 创建默认的value是string类型的缓存 + * + * @author fengshuonan + * @date 2021/1/31 20:39 + */ + @Bean + public TimedCache stringTimedCache() { + return CacheUtil.newTimedCache(CacheConstants.DEFAULT_CACHE_TIMEOUT); + } + + /** + * 创建默认的value是object类型的缓存 + * + * @author fengshuonan + * @date 2021/1/31 20:39 + */ + @Bean + public TimedCache objectTimedCache() { + return CacheUtil.newTimedCache(CacheConstants.DEFAULT_CACHE_TIMEOUT); + } + +} diff --git a/kernel-d-cache/memory-cache-spring-boot-starter/src/main/resources/META-INF/spring.factories b/kernel-d-cache/memory-cache-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..7a1a26e02 --- /dev/null +++ b/kernel-d-cache/memory-cache-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + cn.stylefeng.roses.kernel.cache.starter.memory.GunsMemoryCacheAutoConfiguration diff --git a/kernel-d-cache/pom.xml b/kernel-d-cache/pom.xml index 014f7f1a2..8d26684a4 100644 --- a/kernel-d-cache/pom.xml +++ b/kernel-d-cache/pom.xml @@ -19,6 +19,8 @@ cache-api cache-sdk-memory cache-sdk-redis + memory-cache-spring-boot-starter + redis-spring-boot-starter diff --git a/kernel-d-cache/redis-spring-boot-starter/README.md b/kernel-d-cache/redis-spring-boot-starter/README.md new file mode 100644 index 000000000..792bae7e5 --- /dev/null +++ b/kernel-d-cache/redis-spring-boot-starter/README.md @@ -0,0 +1 @@ +redis缓存的默认配置 diff --git a/kernel-d-cache/redis-spring-boot-starter/pom.xml b/kernel-d-cache/redis-spring-boot-starter/pom.xml new file mode 100644 index 000000000..3a1d6c65c --- /dev/null +++ b/kernel-d-cache/redis-spring-boot-starter/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + + cn.stylefeng.roses + kernel-d-cache + 1.0.0 + ../pom.xml + + + redis-spring-boot-starter + + jar + + + + + + cn.stylefeng.roses + cache-sdk-redis + 1.0.0 + + + + + diff --git a/kernel-d-cache/redis-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/cache/starter/redis/GunsRedisCacheAutoConfiguration.java b/kernel-d-cache/redis-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/cache/starter/redis/GunsRedisCacheAutoConfiguration.java new file mode 100644 index 000000000..dbd7d9938 --- /dev/null +++ b/kernel-d-cache/redis-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/cache/starter/redis/GunsRedisCacheAutoConfiguration.java @@ -0,0 +1,67 @@ +package cn.stylefeng.roses.kernel.cache.starter.redis; + +import cn.stylefeng.roses.kernel.cache.serializer.FastJson2JsonRedisSerializer; +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; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * 基于redis缓存的默认配置,默认提供两个RedisTemplate工具类,其他的各个模块自行配置 + * + * @author fengshuonan + * @date 2021/1/31 20:33 + */ +@Configuration +public class GunsRedisCacheAutoConfiguration { + + /** + * Redis的value序列化器 + * + * @author fengshuonan + * @date 2021/1/31 20:44 + */ + @Bean + public RedisSerializer fastJson2JsonRedisSerializer() { + return new FastJson2JsonRedisSerializer<>(Object.class); + } + + /** + * value是object类型的redis操作类 + * + * @author fengshuonan + * @date 2021/1/31 20:45 + */ + @Bean + public RedisTemplate objectRedisTemplate(RedisConnectionFactory redisConnectionFactory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(redisConnectionFactory); + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(fastJson2JsonRedisSerializer()); + template.setHashKeySerializer(new StringRedisSerializer()); + template.setHashValueSerializer(fastJson2JsonRedisSerializer()); + template.afterPropertiesSet(); + return template; + } + + /** + * value是string类型的redis操作类 + * + * @author fengshuonan + * @date 2021/1/31 20:45 + */ + @Bean + public RedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(redisConnectionFactory); + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(new StringRedisSerializer()); + template.setHashKeySerializer(new StringRedisSerializer()); + template.setHashValueSerializer(new StringRedisSerializer()); + template.afterPropertiesSet(); + return template; + } + +} diff --git a/kernel-d-cache/redis-spring-boot-starter/src/main/resources/META-INF/spring.factories b/kernel-d-cache/redis-spring-boot-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..5ce2611e1 --- /dev/null +++ b/kernel-d-cache/redis-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + cn.stylefeng.roses.kernel.cache.starter.redis.GunsRedisCacheAutoConfiguration