redis--StringRedisTemplate和RedisTemplate区别
StringRedisTemplate:
1)、只能存储string类型的值,因此不能存储如对象
2)、序列化为string,如:
RedisTemplate:
1)、可以存储任意类型,含对象。
2)、序列化采用jdk的,如:
redisTemplate.opsForValue().set("key-0", "hello");
redisTemplate.opsForValue().set("key-1", User.getSampleUser()); public static User getSampleUser() { User user = new User(); user.setId(123); user.setName("遥远2"); return user; }
在java中能正确读取,但是在client中就是jdk序列化后的字符了,不利于查看。
======================================
RedisTemplate使用总结:
1、如果采用默认的jdk序列化,可以不用自行指定序列化即不用再RedisConfig中配置序列化方式。但是,
@Qualifier("redisTemplate")//必须加此行 private RedisTemplate redisTemplate;//不能为RedisTemplate<K,V>
一定要加@Qualifier("redisTemplate"),否则会当成StringRedisTemplate,因而报错。
2、RedisTemplate也可当做StringRedisTemplate使用,但需要配置成string序列化:
@Configuration public class RedisConfig { @Bean("strRedisTemplate") public RedisTemplate<Object,Object> strRedisTemplate(RedisConnectionFactory redisConectionFactory) { RedisTemplate<Object,Object> template = new RedisTemplate<Object,Object>(); template.setConnectionFactory(redisConectionFactory); RedisSerializer<String> stringSerializer = new StringRedisSerializer(); template.setKeySerializer(stringSerializer);//对key序列化 template.setValueSerializer(stringSerializer);//对value序列化 return template; } }
但,意义不大,如果用的value是string的,直接使用StringRedisTemplate即可。
3、RedisTemplate对象序列化
import org.springframework.core.convert.converter.Converter; import org.springframework.core.serializer.support.DeserializingConverter; import org.springframework.core.serializer.support.SerializingConverter; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.SerializationException; public class RedisObjectSerializer implements RedisSerializer<Object> { private Converter<Object,byte[]> serializer = new SerializingConverter(); private Converter<byte[],Object> deserializer = new DeserializingConverter(); private static final byte[] EMPTY_ARRAY = new byte[0]; @Override public Object deserialize(byte[] bytes) throws SerializationException { if (isEmpty(bytes)) { return null; } return deserializer.convert(bytes); } @Override public byte[] serialize(Object t) throws SerializationException { if (t == null) { return EMPTY_ARRAY; } return serializer.convert(t); } private boolean isEmpty(byte[] data) { return (data == null || data.length == 0); } }
发现,插入后,依然是默认的jdk序列化。如图:
所以,此方式也不建议使用。
4、json序列化
参考:https://github.com/alibaba/fastjson/wiki/%E5%9C%A8-Spring-%E4%B8%AD%E9%9B%86%E6%88%90-Fastjson
分别对key、value序列化:
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.StringRedisSerializer; import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer; @Configuration public class RedisConfig { @Bean("jsonRedisTemplate") public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConectionFactory) { RedisTemplate<Object,Object> template = new RedisTemplate<Object,Object>(); template.setConnectionFactory(redisConectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericFastJsonRedisSerializer()); //template.setDefaultSerializer(new FastJsonRedisSerializer(Object.class)); return template; } }
使用默认的:
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 com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer; @Configuration public class RedisConfig { @Bean("jsonRedisTemplate") public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConectionFactory) { RedisTemplate<Object,Object> template = new RedisTemplate<Object,Object>(); template.setConnectionFactory(redisConectionFactory); //template.setKeySerializer(new StringRedisSerializer()); //template.setValueSerializer(new GenericFastJsonRedisSerializer()); template.setDefaultSerializer(new GenericFastJsonRedisSerializer()); return template; } }
此时,在redis-cli中,执行命令:
必须为key加上 \",才可以获取值。因此,最后总结:
当value存储的是string类型时,使用StringRedisTemplate。
否则,使用RedisTemplate,且,即使 使用RedisTemplate,也必须分别对key、value做序列化,而不是使用一个默认的,即: