iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >Redis( 基础篇 ==> StringRedisTemplate的使用
  • 629
分享到

Redis( 基础篇 ==> StringRedisTemplate的使用

redis缓存哈希算法springbootjava 2023-09-28 15:09:55 629人浏览 独家记忆
摘要

本章导学: 为什么要使用StringRedisTemplateStringRedisTemplate操作String类型数据StringRedisTemplate操作Hash类型数据 一、 为什么要使用StringRedisTemplat

本章导学:

  • 为什么要使用StringRedisTemplate
  • StringRedisTemplate操作String类型数据
  • StringRedisTemplate操作Hash类型数据

一、 为什么要使用StringRedisTemplate

在我们为RedisTemplate指定序列化方式后(key为RedisSerializer.string(),value为GenericJackson2JSONRedisSerializer),我们存储value为实体类对象时,会产生如下现象:

这是因为,当我们传入的Value为实体类对象的时候,会用 GenericJackson2jsonRedisSerializer序列化器把java对象转为JSON格式,然后再存入Redis库中,在我们使用redisTemplate.opsForValue().get方法获取数据时,通过存入的@class属性,把JSON反序列化成JAVA对象。

这样一来我们在IDEA的控制器上很直观的就可以看到数据,但是也存在了一个缺点->浪费内存,因为我们要存放@class这一段额外的数据来反序列化JSON字符串

那为了节约内存,我们在处理Value时不使用GenericJackson2JsonRedisSerializer序列化器,时用RedisSerializer.string序列化器。这样一下,我们只需要在存入数据时,手动的把JAVA对象转变为JSON格式字符串,然后取数据时,再把JSON转回JAVA对象就好了。

而StringRedisTemplate它的key和Value默认就是String方式,我们不用自己再去定义RedisTemplate的配置类。

当然你要定义也可以,代码如下

package com.brrbaii.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.RedisSerializer;@Configurationpublic class RedisConfig {    @Bean    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){        //创建RedisTemplate对象        RedisTemplate template = new RedisTemplate<>();        //设置连接工厂        template.setConnectionFactory(redisConnectionFactory);        //创建JSON序列化工具        GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();        //设置KEY的序列化        template.seTKEySerializer(RedisSerializer.string());        template.setHashKeySerializer(RedisSerializer.string());        //设置VALUE的序列化,不用jsonRedisSerializer//        template.setValueSerializer(jsonRedisSerializer);//        template.setHashValueSerializer(jsonRedisSerializer);        template.setValueSerializer(RedisSerializer.string());        template.setHashValueSerializer(RedisSerializer.string());        //返回        return template;    }}

我们写一个测试类,注入StringRedisTemplate,然后手动进行序列化和反序列化

代码如下

    @Test    public void testJavaBean(){        User user = new User("brrbaii", 22);        //手动把user对象转为JSON字符串,这里使用Hutool工具类里的JSONUtil        String UserToStr = JSONUtil.toJsonStr(user);        //存入数据        stringRedisTemplate.opsForValue().set("user:1",UserToStr);        //取出数据,这里取出来的是JSON格式        String StrUser = stringRedisTemplate.opsForValue().get("user:1");        //手动把JSON字符串转回user对象        User userResult = JSONUtil.toBean(StrUser, User.class);        System.out.println(userResult);    }

查看Redis可视化界面,这时候"@class":"com.brrbaii.entity.User"就没了

查看idea控制台输出结果

 

二、StringRedisTemplate操作String类型数据

StringRedisTemplate它的key和Value默认就是String方式,我们直接存就好了,没什么好讲的 

三、 StringRedisTemplate操作Hash类型数据

 

使用StringRedisTemplate.opsForHash时用的方法不是按照Redis命令来命名了,而是采用java里的HashMap用的方法来命名

存单个field我们用put方法,批量存field我们用putAll方法,我们创建个map集合,put多个key-value就好了

取单个field时我们用get方法,取全部field时我们用entries方法

代码如下:

    @Test    public void testHash(){        //存单个field        stringRedisTemplate.opsForHash().put("hash01","hashName","brr");        Object o = stringRedisTemplate.opsForHash().get("hash01", "hashName");        System.out.println(o);        //存多个field        Map map = new HashMap();        map.put("name","bb");        map.put("sex","jender");        map.put("hobby","ball");        stringRedisTemplate.opsForHash().putAll("hash02",map);        Map hash02 = stringRedisTemplate.opsForHash().entries("hash02");        System.out.println(hash02);    }

运行结果

 

 

  

来源地址:https://blog.csdn.net/weixin_48841931/article/details/127355010

--结束END--

本文标题: Redis( 基础篇 ==> StringRedisTemplate的使用

本文链接: https://www.lsjlt.com/news/420201.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作