Redis怎么实现保存对象
作者:慢热型
时间:2023-04-24
浏览:0
redis保存对象redis数据结构String——字符串Hash——字典List——列表Set——集合SortedSet——有序集合redisTemplate.opsForValue();//操作字符串redisTemplate.opsForHash();//操作hashredisTemplate.opsForList();//操作listredisTemplate.opsForSet();//操作setredisTemplate.opsForZSet();//操作有序set保存对象RedisConfi
redis保存对象
redis数据结构
String——字符串Hash——字典List——列表Set——集合Sorted Set——有序集合
redisTemplate.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 redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashKeySerializer(new GenericJackson2JsonRedisSerializer());
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
} 

测试成功。
redis存放对象的两种方式
数据格式
用户id为查找的key
存储的value用户对象包括姓名,年龄,生日等等
如果用普通的key-value结构来存储,主要有以下2种方式存储
方式一(String)

这种方式是使用list或者set这些来存储的,这样的方式其实也可以达到我们想要的效果,但是因为每次修改属性都需要三步走,性能开销非常大。1.先反序列化;2,修改;3.序列化
方式二(hash)
这种方式其实也有两种写法
写法一:

这种写法不仅能够达成目标,而且解决了资源消耗过大的问题,但是也引起了另一个问题,就是用户的id数据冗余
写法二:

通过key(用户id)+field(属性标签)可以操作对应属性数据了,既不需要重复存储数据,也不会带来序列化和并修复操控的问题
作者最新文章
CPU硅脂涂抹方法图解及正确操作步骤
2026-09-22 15:23
网易2026年Q2财报:营收301亿元,游戏收入增10%,三款重点新游披露进展
2026-09-08 17:51
PDF怎么添加页码?页码位置和起始页怎么设置?
2026-09-03 09:11
图片文件怎么转换成PDF?多张图片如何按顺序合成?
2026-09-02 19:33
CorelDRAW绘制正弦曲线的两种方法:贝塞尔工具与变形工具
2026-09-02 16:08
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多



































