mirror of https://gitee.com/stylefeng/roses
【cache】增加内存和redis缓存的默认实现
parent
8d01a663cd
commit
0957782210
|
@ -28,4 +28,9 @@ public interface CacheConstants {
|
|||
*/
|
||||
Long NONE_EXPIRED_TIME = 1000L * 3600 * 24 * 999;
|
||||
|
||||
/**
|
||||
* 默认缓存的过期时间,10分钟
|
||||
*/
|
||||
Long DEFAULT_CACHE_TIMEOUT = 1000L * 60 * 10;
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
内存缓存的默认配置
|
|
@ -0,0 +1,29 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>kernel-d-cache</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>memory-cache-spring-boot-starter</artifactId>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!--内存缓存的sdk-->
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>cache-sdk-memory</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -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<String, String> stringTimedCache() {
|
||||
return CacheUtil.newTimedCache(CacheConstants.DEFAULT_CACHE_TIMEOUT);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建默认的value是object类型的缓存
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2021/1/31 20:39
|
||||
*/
|
||||
@Bean
|
||||
public TimedCache<String, Object> objectTimedCache() {
|
||||
return CacheUtil.newTimedCache(CacheConstants.DEFAULT_CACHE_TIMEOUT);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cn.stylefeng.roses.kernel.cache.starter.memory.GunsMemoryCacheAutoConfiguration
|
|
@ -19,6 +19,8 @@
|
|||
<module>cache-api</module>
|
||||
<module>cache-sdk-memory</module>
|
||||
<module>cache-sdk-redis</module>
|
||||
<module>memory-cache-spring-boot-starter</module>
|
||||
<module>redis-spring-boot-starter</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
redis缓存的默认配置
|
|
@ -0,0 +1,29 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>kernel-d-cache</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>redis-spring-boot-starter</artifactId>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!--redis的sdk-->
|
||||
<dependency>
|
||||
<groupId>cn.stylefeng.roses</groupId>
|
||||
<artifactId>cache-sdk-redis</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -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<String, Object> objectRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||
RedisTemplate<String, Object> 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<String, String> stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||
RedisTemplate<String, String> 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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cn.stylefeng.roses.kernel.cache.starter.redis.GunsRedisCacheAutoConfiguration
|
Loading…
Reference in New Issue