In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
How does redis serialize? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
The ultimate goal of serialization is to enable objects to be stored across platforms and transmitted over the network. The way we do cross-platform storage and network transmission is IO, and the data format supported by our IO is byte arrays.
From the above, I think you already know that any data that requires "cross-platform storage" and "network transmission" needs to be serialized.
In essence, storage and network transmission need to save an object state into a cross-platform recognized byte format, and then other platforms can restore object information through byte information parsing.
Comparison of redis serialization methods:
The default mode of redis is JdkSerializationRedisSerializer
JdkSerializationRedisSerializer: use the serialization capabilities provided by JDK.
The advantage is that you don't need to provide type information (class) when deserializing, but the disadvantage is that you need to implement the Serializable interface, and the serialized result is very large, about five times the size of the JSON format, which consumes a lot of memory on the redis server.
Jackson2JsonRedisSerializer: use the Jackson library to serialize objects into JSON strings.
The advantage is that it is fast, the serialized string is short and short, and there is no need to implement the Serializable interface.
But the disadvantage is also fatal, that is, there is a type parameter in the constructor of this class, which must provide the type information (.class object) of the object to be serialized. By looking at the source code, it is found that it only uses type information during deserialization.
Problem: using the default JDK serialization method, there will be "garbled" when viewing the kmurv value in the RDM tool, which is not convenient to view.
Solution: customize the serialization method, using Jackson2JsonRedisSerializer
Import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;/** * Redis configuration * * @ author LinJie * / @ Configurationpublic class RedisConfig {/ * Redis repository redis repository. * * @ param redisTemplate the redis template * @ return the redis repository * / @ Bean public RedisRepository redisRepository (RedisTemplate redisTemplate) {/ / use Jackson2JsonRedisSerialize to replace the default serialization Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer (Object.class); ObjectMapper objectMapper = new ObjectMapper (); objectMapper.setVisibility (PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping (ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper (objectMapper) StringRedisSerializer stringRedisSerializer = new StringRedisSerializer (); / / key uses String serialization method redisTemplate.setKeySerializer (stringRedisSerializer); / / hash key also uses String serialization method redisTemplate.setHashKeySerializer (stringRedisSerializer); / / value serialization mode uses jackson redisTemplate.setValueSerializer (jackson2JsonRedisSerializer); / / hash value serialization mode uses jackson redisTemplate.setHashValueSerializer (jackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet () Return new RedisRepository (redisTemplate);}} this is the answer to the question about how to serialize redis. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.