Recommended learning: Redis video tutorial
String
——StringHash
——DictionaryList
——ListSet
——SetSorted Set
——Ordered setredisTemplate.opsForValue();//操作字符串 redisTemplate.opsForHash();//操作hash redisTemplate.opsForList();//操作list redisTemplate.opsForSet();//操作set redisTemplate.opsForZSet();//操作有序set
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.
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
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!