redis如何实现序列化

时间:2022-12-06 01:09

redis如何实现序列化

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 redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {

RedisTemplate template = new RedisTemplate();

template.setConnectionFactory(redisConnectionFactory);

return template;

}

@Bean

@ConditionalOnMissingBean

public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {

StringRedisTemplate template = new StringRedisTemplate();

template.setConnectionFactory(redisConnectionFactory);

return template;

}

}

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());

}

}

标签:
随便看看
本类推荐
本类排行
热门标签

我国国家通讯社行不行啊细狗新年贺词祝福语2023年祝福语新年新年贺词新年贺词祝福语居家抗病毒小药箱除夕高速免费吗2022年除夕高速免费吗春联春联句子大全春联句子大全七字年夜饭十二道菜年夜饭十二道菜单年夜饭吃什么年夜饭吃什么菜吃年夜饭的寓意年夜饭黑芝麻菊花茶晒菊花茶腌腊肉送妈妈生日礼物汉白玉送闺蜜水钻送女人礼物爸爸生日送礼物送老公