cn.cliveyuan.tools.redis.RedisConfig Maven / Gradle / Ivy
The newest version!
package cn.cliveyuan.tools.redis;
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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Redis 配置
*
* @author Clive Yuan
* @date 2021/09/14
*/
@Configuration
public class RedisConfig {
@Bean("redisTemplate")
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
RedisTemplate redisTemplate = new RedisTemplate();
StringRedisSerializer redisKeySerializer = new StringRedisSerializer();
redisTemplate.setConnectionFactory(factory);
GenericJackson2JsonRedisSerializer redisValueSerializer = new GenericJackson2JsonRedisSerializer();
redisTemplate.setKeySerializer(redisKeySerializer);
redisTemplate.setValueSerializer(redisValueSerializer);
redisTemplate.setHashKeySerializer(redisKeySerializer);
redisTemplate.setHashValueSerializer(redisValueSerializer);
return redisTemplate;
}
@Bean
public RedisService redisService(RedisTemplate redisTemplate) {
return new RedisService(redisTemplate);
}
}