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

Let's talk about how Redis implements saving objects

WBOY
Release: 2022-08-24 09:20:38
forward
3086 people have browsed it

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
Copy after login

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;  
    }
}
Copy after login

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!

Related labels:
source:jb51.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template