最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
redis实现保存对象代码示例
时间:2022-06-29 10:39:40 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下redis实现保存对象代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
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 RedisTemplateredisTemplate(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(属性标签)可以操作对应属性数据了,既不需要重复存储数据,也不会带来序列化和并修复操控的问题
相关文章
- 幻兽帕鲁雷冠龙捕捉位置在哪 帕鲁雷冠龙位置介绍 07-12
- Solana与Bullish达成关键整合,助力稳定币发展 07-12
- 幻兽帕鲁花冠龙哪里可以抓到 帕鲁花冠龙捕捉地点介绍 07-12
- 幻兽帕鲁秘斯媞雅在哪里出生 帕鲁秘斯媞雅出没地点介绍 07-12
- 剑星桃乐丝服装怎么获取 桃乐丝服装获取方式介绍 07-12
- 无畏契约源能行动星礈怎么玩 星礈实战技巧教学 07-12