redis實現序列化的示例:
Redis提供了兩個模板類RedisTemplate和StringRedisTemplate實現序列化,如:
1.RedisAutoConfiguration源碼,對key和value進行序列化:
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
//RedisTemplate的key和value都是Object類型的,如果進行網絡傳輸或將數據存儲到硬盤上就需對key和value進行序列化
public RedisTemplate
2.StringRedisTemplate源碼,將key和value都序列化為String類型的:
//StringRedisTemplate類繼承RedisTemplate
public class StringRedisTemplate extends RedisTemplate
{ //將key和value都序列化為String類型的,也是只支持String類型的key和value
public StringRedisTemplate() {
this.setKeySerializer(RedisSerializer.string());
this.setValueSerializer(RedisSerializer.string());
this.setHashKeySerializer(RedisSerializer.string());
this.setHashValueSerializer(RedisSerializer.string());
}
}