網站首頁 編程語言 正文
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<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; } }
測試成功。
redis存放對象的兩種方式
數據格式
- 用戶id為查找的key
- 存儲的value用戶對象包括姓名,年齡,生日等等
- 如果用普通的key-value結構來存儲,主要有以下2種方式存儲
方式一(String)
這種方式是使用list或者set這些來存儲的,這樣的方式其實也可以達到我們想要的效果,但是因為每次修改屬性都需要三步走,性能開銷非常大。1.先反序列化;2,修改;3.序列化
方式二(hash)
這種方式其實也有兩種寫法
寫法一:
這種寫法不僅能夠達成目標,而且解決了資源消耗過大的問題,但是也引起了另一個問題,就是用戶的id數據冗余
寫法二:
通過key(用戶id)+field(屬性標簽)可以操作對應屬性數據了,既不需要重復存儲數據,也不會帶來序列化和并修復操控的問題
原文鏈接:https://blog.csdn.net/heni6560/article/details/81117609
相關推薦
- 2022-05-13 Python Anaconda安裝sasl,thrift,thrift-sasl 并連接PyHive
- 2022-03-27 Android顯示富文本+夜間深色模式_Android
- 2022-02-05 Numpy中不同維度數組之間的計算
- 2022-06-21 C語言詳解結構體的內存對齊與大小計算_C 語言
- 2023-06-03 一文帶你吃透Python中的os和sys模塊_python
- 2023-01-13 BatchNorm2d原理、作用及pytorch中BatchNorm2d函數的參數使用_python
- 2022-05-08 python如何生成密碼字典_python
- 2023-01-19 C++深度探索虛函數覆蓋示例_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支