import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;
@Component
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(RedisSerializer.string());// key 序列化规则
redisTemplate.setHashKeySerializer(RedisSerializer.string());// hash key 序列化规则
redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Object.class));// value 序列化规则
redisTemplate.setHashValueSerializer(new GenericToStringSerializer<>(Object.class)); // hash value 序列化规则
redisTemplate.setConnectionFactory(factory); //绑定 RedisConnectionFactory
return redisTemplate; //返回设置好的 RedisTemplate
}
}
02-16
1万+
09-02
505