Home  >  Article  >  Database  >  Why does redis need to be serialized?

Why does redis need to be serialized?

(*-*)浩
(*-*)浩Original
2019-11-22 10:33:048950browse

Why does redis need to be serialized?

#The ultimate purpose of serialization is to allow objects to be stored across platforms and transmitted over the network. The way we carry out cross-platform storage and network transmission is IO, and the data format supported by our IO is byte array. (Recommended study: Redis video tutorial)

Through the above, I think you already know that any data that requires "cross-platform storage" and "network transmission" needs to be serialized.

Essentially, both storage and network transmission require saving an object state into a cross-platform recognized byte format, and then other platforms can parse and restore object information through byte information.

Comparison of redis serialization methods:

The default method of redis is JdkSerializationRedisSerializer

JdkSerializationRedisSerializer: Use the serialization provided by JDK Function.

The advantage is that you do not 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, in JSON format 5 About times, this will consume a lot of memory of 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 concise, and there is no need to implement the Serializable interface.

But the disadvantage is also very fatal, that is, there is a type parameter in the constructor of this class, and the type information of the object to be serialized (.class object) must be provided. By looking at the source code, we found that it only uses type information during the deserialization process.

Problem: Using the default JDK serialization method, "garbled characters" will appear when viewing the k-v value in the RDM tool, which is inconvenient to view.

Solution: Customize the serialization method, use 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配置
 *
 * @author LinJie
 */
@Configuration
public class RedisConfig {

    /**
     * Redis repository redis repository.
     *
     * @param redisTemplate the redis template
     * @return the redis repository
     */
    @Bean
    public RedisRepository redisRepository(RedisTemplate redisTemplate) {
        // 使用Jackson2JsonRedisSerialize 替换默认序列化
        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采用String的序列化方式
        redisTemplate.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return new RedisRepository(redisTemplate);
    }
}

For more Redis-related technical articles, please visit the Redis database usage tutorial column Get studying!

The above is the detailed content of Why does redis need to be serialized?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn