【cache】增加内存和redis缓存的默认实现

pull/3/head
fengshuonan 2021-01-31 20:57:17 +08:00
parent 8d01a663cd
commit 0957782210
10 changed files with 178 additions and 0 deletions

View File

@ -28,4 +28,9 @@ public interface CacheConstants {
*/
Long NONE_EXPIRED_TIME = 1000L * 3600 * 24 * 999;
/**
* 10
*/
Long DEFAULT_CACHE_TIMEOUT = 1000L * 60 * 10;
}

View File

@ -0,0 +1 @@
内存缓存的默认配置

View File

@ -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>

View File

@ -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 {
/**
* valuestring
*
* @author fengshuonan
* @date 2021/1/31 20:39
*/
@Bean
public TimedCache<String, String> stringTimedCache() {
return CacheUtil.newTimedCache(CacheConstants.DEFAULT_CACHE_TIMEOUT);
}
/**
* valueobject
*
* @author fengshuonan
* @date 2021/1/31 20:39
*/
@Bean
public TimedCache<String, Object> objectTimedCache() {
return CacheUtil.newTimedCache(CacheConstants.DEFAULT_CACHE_TIMEOUT);
}
}

View File

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.stylefeng.roses.kernel.cache.starter.memory.GunsMemoryCacheAutoConfiguration

View File

@ -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>

View File

@ -0,0 +1 @@
redis缓存的默认配置

View File

@ -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>

View File

@ -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;
/**
* redisRedisTemplate
*
* @author fengshuonan
* @date 2021/1/31 20:33
*/
@Configuration
public class GunsRedisCacheAutoConfiguration {
/**
* Redisvalue
*
* @author fengshuonan
* @date 2021/1/31 20:44
*/
@Bean
public RedisSerializer<?> fastJson2JsonRedisSerializer() {
return new FastJson2JsonRedisSerializer<>(Object.class);
}
/**
* valueobjectredis
*
* @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;
}
/**
* valuestringredis
*
* @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;
}
}

View File

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.stylefeng.roses.kernel.cache.starter.redis.GunsRedisCacheAutoConfiguration