mirror of https://gitee.com/stylefeng/roses
【7.0.4】简化redisTemplate创建流程
parent
ac9a0dbf40
commit
a969fb6cb1
|
@ -0,0 +1,61 @@
|
||||||
|
package cn.stylefeng.roses.kernel.cache.redis.util;
|
||||||
|
|
||||||
|
import cn.stylefeng.roses.kernel.cache.redis.serializer.FastJson2JsonRedisSerializer;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RedisTemplate创建工具类
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2021/5/17 16:50
|
||||||
|
*/
|
||||||
|
public class CreateRedisTemplateUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 穿件序列化器
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2021/5/17 16:48
|
||||||
|
*/
|
||||||
|
public static RedisSerializer<?> fastJson2JsonRedisSerializer() {
|
||||||
|
return new FastJson2JsonRedisSerializer<>(Object.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建value是object类型的redis操作类
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2021/5/17 16:49
|
||||||
|
*/
|
||||||
|
public static <T> RedisTemplate<String, T> createObject(RedisConnectionFactory redisConnectionFactory) {
|
||||||
|
RedisTemplate<String, T> 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/5/17 16:49
|
||||||
|
*/
|
||||||
|
public static RedisTemplate<String, String> createString(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -27,12 +27,12 @@ package cn.stylefeng.roses.kernel.cache.redis.starter;
|
||||||
import cn.stylefeng.roses.kernel.cache.redis.operator.DefaultRedisCacheOperator;
|
import cn.stylefeng.roses.kernel.cache.redis.operator.DefaultRedisCacheOperator;
|
||||||
import cn.stylefeng.roses.kernel.cache.redis.operator.DefaultStringRedisCacheOperator;
|
import cn.stylefeng.roses.kernel.cache.redis.operator.DefaultStringRedisCacheOperator;
|
||||||
import cn.stylefeng.roses.kernel.cache.redis.serializer.FastJson2JsonRedisSerializer;
|
import cn.stylefeng.roses.kernel.cache.redis.serializer.FastJson2JsonRedisSerializer;
|
||||||
|
import cn.stylefeng.roses.kernel.cache.redis.util.CreateRedisTemplateUtil;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 基于redis缓存的默认配置,默认提供两个RedisTemplate工具类,其他的各个模块自行配置
|
* 基于redis缓存的默认配置,默认提供两个RedisTemplate工具类,其他的各个模块自行配置
|
||||||
|
@ -62,14 +62,7 @@ public class GunsRedisCacheAutoConfiguration {
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public RedisTemplate<String, Object> objectRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
public RedisTemplate<String, Object> objectRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||||
RedisTemplate<String, Object> template = new RedisTemplate<>();
|
return CreateRedisTemplateUtil.createObject(redisConnectionFactory);
|
||||||
template.setConnectionFactory(redisConnectionFactory);
|
|
||||||
template.setKeySerializer(new StringRedisSerializer());
|
|
||||||
template.setValueSerializer(fastJson2JsonRedisSerializer());
|
|
||||||
template.setHashKeySerializer(new StringRedisSerializer());
|
|
||||||
template.setHashValueSerializer(fastJson2JsonRedisSerializer());
|
|
||||||
template.afterPropertiesSet();
|
|
||||||
return template;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -80,14 +73,8 @@ public class GunsRedisCacheAutoConfiguration {
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public RedisTemplate<String, String> gunsStringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
public RedisTemplate<String, String> gunsStringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
|
||||||
RedisTemplate<String, String> template = new RedisTemplate<>();
|
return CreateRedisTemplateUtil.createString(redisConnectionFactory);
|
||||||
template.setConnectionFactory(redisConnectionFactory);
|
|
||||||
template.setKeySerializer(new StringRedisSerializer());
|
|
||||||
template.setValueSerializer(new StringRedisSerializer());
|
|
||||||
template.setHashKeySerializer(new StringRedisSerializer());
|
|
||||||
template.setHashValueSerializer(new StringRedisSerializer());
|
|
||||||
template.afterPropertiesSet();
|
|
||||||
return template;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue