Home >Database >Redis >Let's talk about how Redis implements saving objects

Let's talk about how Redis implements saving objects

WBOY
WBOYforward
2022-08-24 09:20:383074browse

Recommended learning: Redis video tutorial

redis save object

redis data structure

  • String——String
  • Hash——Dictionary
  • List——List
  • Set——Set
  • Sorted Set——Ordered set
redisTemplate.opsForValue();//操作字符串
redisTemplate.opsForHash();//操作hash
redisTemplate.opsForList();//操作list
redisTemplate.opsForSet();//操作set
redisTemplate.opsForZSet();//操作有序set

Save object

RedisConfig.java

package com.wj.demo.config; 
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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
 
@Configuration
public class RedisConfig {
 
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {  
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();  
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.afterPropertiesSet();  
        return template;  
    }
}

Test successful.

Two ways to store objects in redis

Data format

  • The user id is the key to be searched
  • The stored value user object includes the name, Age, birthday, etc.
  • If you use an ordinary key-value structure to store it, there are two main ways to store it

Method 1 (String)

This method uses list or set to store. This method can actually achieve the effect we want, but because each modification of attributes requires three steps, the performance overhead is very large. . 1. Deserialize first; 2. Modify; 3. Serialization

Method 2 (hash)

There are actually two ways to write this method

Writing 1:

This way of writing not only achieves the goal, but also solves the problem of excessive resource consumption, but it also causes another problem, which is the user’s id Data redundancy

Writing method two:

The corresponding attribute data can be operated through the key (user id) field (attribute label) , there is no need to store data repeatedly, nor will it cause problems with serialization and repair and manipulation

Recommended learning: Redis video tutorial

The above is the detailed content of Let's talk about how Redis implements saving objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete