序列化和反序列化
序列化是指将对象转换为字节序列的过程,以便于存储或传输。在序列化过程中,对象的状态信息将被转换为字节流,可以保存到文件中或通过网络传输给其他计算机。反序列化则是将字节序列恢复为对象的过程。
反序列化是指将字节序列恢复为对象的过程。在序列化中,对象的状态信息被转换为字节流以便于存储或传输,而在反序列化中,这些字节流将被重新转换为原始对象的状态。(PS:作者很久之前被问到当时脑子断片了没答出来哈哈~~)
说人话:就是对象和字节码互转
SpringBoot集成Redis有多种序列化和反序列化的方式(这里只举例常用的几种)
package com.excesys.config;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
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.RedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(factory);
//redis序列化--key采用String的序列化方式,value采用json的序列化方式
// template.setKeySerializer(RedisSerializer.string());
// template.setHashKeySerializer(RedisSerializer.string());
// template.setValueSerializer(RedisSerializer.json());
// template.setHashValueSerializer(RedisSerializer.json());
//Jackson序列化--key采用String的序列化方式,value采用Jackson的序列化方式
// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
// //序列化包括类型描述 否则反向序列化实体会报错,一律都为JsonObject
// ObjectMapper mapper = new ObjectMapper();
// mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
// jackson2JsonRedisSerializer.setObjectMapper(mapper);
// template.setKeySerializer(RedisSerializer.string());
// template.setHashKeySerializer(RedisSerializer.string());
// template.setValueSerializer(jackson2JsonRedisSerializer);
// template.setHashValueSerializer(jackson2JsonRedisSerializer);
//FastJson序列化--key采用String的序列化方式,value采用FastJson的序列化方式
GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
template.setKeySerializer(RedisSerializer.string());
template.setHashKeySerializer(RedisSerializer.string());
template.setValueSerializer(fastJsonRedisSerializer);
template.setHashValueSerializer(fastJsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
几种方式的序列化和反序列化的取值耗时
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Test
void contextLoads() {
ProductOrder po = new ProductOrder(1l, "1752579478791368704", "Python书籍", 521.98, 3, "12345678901234567890");
long st = System.currentTimeMillis();
redisTemplate.opsForValue().set("test", po);
ProductOrder test = (ProductOrder) redisTemplate.opsForValue().get("test");
long et = System.currentTimeMillis();
System.out.println("Reids序列化和反序列化:" + test + "耗时:" + (et - st));
}
默认:
默认:ProductOrder(id=1, out_trade_no=1752579478791368704, subject=Python书籍, 、total_amount=521.98, order_status=3, product_code=12345678901234567890)耗时:1184
默认:ProductOrder(id=1, out_trade_no=1752579478791368704, subject=Python书籍, total_amount=521.98, order_status=3, product_code=12345678901234567890)耗时:1016
默认:ProductOrder(id=1, out_trade_no=1752579478791368704, subject=Python书籍, total_amount=521.98, order_status=3, product_code=12345678901234567890)耗时:1037
Jackson
jackson:ProductOrder(id=1, out_trade_no=1752579478791368704, subject=Python书籍, total_amount=521.98, order_status=3, product_code=12345678901234567890)耗时:1192
jackson:ProductOrder(id=1, out_trade_no=1752579478791368704, subject=Python书籍, total_amount=521.98, order_status=3, product_code=12345678901234567890)耗时:1042
jackson:ProductOrder(id=1, out_trade_no=1752579478791368704, subject=Python书籍, total_amount=521.98, order_status=3, product_code=12345678901234567890)耗时:1016
FastJson
fastjson:ProductOrder(id=1, out_trade_no=1752579478791368704, subject=Python书籍, total_amount=521.98, order_status=3, product_code=12345678901234567890)耗时:962
fastjson:ProductOrder(id=1, out_trade_no=1752579478791368704, subject=Python书籍, total_amount=521.98, order_status=3, product_code=12345678901234567890)耗时:1232
fastjson:ProductOrder(id=1, out_trade_no=1752579478791368704, subject=Python书籍, total_amount=521.98, order_status=3, product_code=12345678901234567890)耗时:1000