redis如何实现保存对象
目录
- redis保存对象
- redis数据结构
- 保存对象
- redis存放对象的两种方式
- 数据格式
- 方式一(String)
- 方式二(hash)
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(属性标签)可以操作对应属性数据了,既不需要重复存储数据,也不会带来序列化和并修复操控的问题
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
您可能感兴趣的文章
- 12-22使用mysql记录从url返回的http GET请求数据操作
- 12-22详解sql中exists和in的语法与区别
- 12-22hive从mysql导入数据量变多的解决方案
- 12-22如何为PostgreSQL的表自动添加分区
- 12-22postgresql 实现得到时间对应周的周一案例
- 12-22sqoop export导出 map100% reduce0% 卡住的多种原因及解决
- 12-22mysql查询条件not in 和 in的区别及原因说明
- 12-22解决mysql使用not in 包含null值的问题
- 12-22解决从集合运算到mysql的not like找不出NULL的问题
- 12-22postgresql 实现多表关联删除


阅读排行
推荐教程
- 12-11mysql代码执行结构实例分析【顺序、分支、循环结构】
- 12-08添加mysql的用户名和密码是什么语句?
- 12-20PhpMyAdmin出现错误数据无法导出怎么办?
- 12-19Redis中实现查找某个值的范围
- 12-15浅析mysql迁移到clickhouse的5种方法
- 12-15CentOS7 64位下MySQL5.7安装与配置教程
- 12-14Mysql大型SQL文件快速恢复方案分享
- 12-14mysql 5.7.27 安装配置方法图文教程
- 12-13MySQL给新建用户并赋予权限最简单的方法
- 12-13关于MySQL索引的深入解析





