大勇看世界,学校展板,牛金贵
stringredistemplate与redistemplate区别点
两者的关系是stringredistemplate继承redistemplate。
两者的数据是不共通的;也就是说stringredistemplate只能管理stringredistemplate里面的数据,redistemplate只能管理redistemplate中的数据。
其实他们两者之间的区别主要在于他们使用的序列化类:
redistemplate使用的是jdkserializationredisserializer 存入数据会将数据先序列化成字节数组然后在存入redis数据库。
stringredistemplate使用的是stringredisserializer
使用时注意事项:
当你的redis数据库里面本来存的是字符串数据或者你要存取的数据就是字符串类型数据的时候,那么你就使用
stringredistemplate即可。
但是如果你的数据是复杂的对象类型,而取出的时候又不想做任何的数据转换,直接从redis里面取出一个对象,那么使用
redistemplate是更好的选择。
redistemplate使用时常见问题:
redistemplate 中存取数据都是字节数组。当redis中存入的数据是可读形式而非字节数组时,使用redistemplate取值的时候会无法获取导出数据,获得的值为null。可以使用 stringredistemplate 试试。
redistemplate中定义了5种数据结构操作
stringredistemplate常用操作
stringredistemplate的使用
springboot中使用注解@autowired 即可
@autowired
public stringredistemplate stringredistemplate;
使用样例:
@restcontroller @requestmapping("/user") public class userresource { private static final logger log = loggerfactory.getlogger(userresource.class); @autowired private userservice userservice; @autowired public stringredistemplate stringredistemplate; @requestmapping("/num") public string countnum() { string usernum = stringredistemplate.opsforvalue().get("usernum"); if(stringutils.isnull(usernum)){ stringredistemplate.opsforvalue().set("usernum", userservice.countnum().tostring()); } return usernum; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
浅析我对 String、StringBuilder、StringBuffer 的理解
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
Springboot整合freemarker 404问题解决方案
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
网友评论