您好,欢迎访问一九零五行业门户网

一起聊聊Redis如何实现保存对象

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了redis如何实现保存对象,具有很好的参考价值,希望对大家有所帮助。
推荐学习:redis视频教程
redis保存对象redis数据结构string——字符串hash——字典list——列表set——集合sorted set——有序集合redistemplate.opsforvalue();//操作字符串redistemplate.opsforhash();//操作hashredistemplate.opsforlist();//操作listredistemplate.opsforset();//操作setredistemplate.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; @configurationpublic 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(属性标签)可以操作对应属性数据了,既不需要重复存储数据,也不会带来序列化和并修复操控的问题
推荐学习:redis视频教程
以上就是一起聊聊redis如何实现保存对象的详细内容。
其它类似信息

推荐信息