当前位置: 首页 > news >正文

【项目搭建三】SpringBoot引入redis

添加依赖

本文使用spring data redis访问和操作redis,pom文件中加入以下依赖:

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-data-redis</artifactId>  
</dependency>

application.yml配置

application.yml中添加以下配置:

redis:host: 127.0.0.1        # 服务地址port: 6379             # 服务端口password: gusy1234     # 服务密码database: 0            # 数据库索引,默认0# 连接池配置,使用lettuce,可选jedislettuce:pool:max-active: 8        # 连接池最大连接数max-idle: 8          # 连接池最大空闲连接数max-wait: -1         # 连接池最大阻塞等待时间,负值表示没有限制         min-idle: 0          # 连接池最小空闲连接数timeout: 1000        # 连接超时时间(毫秒)

注:spring data redis中,可以配置lettuce或jedis作为客户端的连接池。springBoot 2.x默认使用的是lettuce。如果你使用jedis作为连接池,需定义一个JedisConnectionFactory注入到RedisTemplate中,但是这种方式在springBoot 2.x中已经过时,不建议使用。

测试用例

使用RedisTemplate操作redis,以下是测试用例:

@SpringBootTest
class AgedWebApplicationTests {@Autowired@Qualifier("redisTemplate")private RedisTemplate redisTemplate;@Testpublic void redisTest() {redisTemplate.boundValueOps("testKey").set("testValue");System.out.println(redisTemplate.boundValueOps("testKey").get());}
}

进入redis desktop manage查看保存结果:

可以看到value显示为二进制,这是由于redisTemplate默认使用jdk序列化导致的,这种方式生成的数据较大,性能较低,且只有Java应用能够反序列化。本文将用fastjson2作为序列化方式。

fastjson2作为序列化方式

pom文件中引入fastjson2:

<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.45</version>
</dependency>

 增加RedisConfig配置类:

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
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.StringRedisSerializer;/*** redis配置** @Author: gusy*/
@Slf4j
@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);// 使用Fastjson2序列化器GenericFastJsonRedisSerializer redisSerializer = new GenericFastJsonRedisSerializer();// 设置 value 的序列化规则和 key 的序列化规则template.setValueSerializer(redisSerializer);template.setHashValueSerializer(redisSerializer);template.setKeySerializer(new StringRedisSerializer());template.setHashKeySerializer(new StringRedisSerializer());template.afterPropertiesSet();log.info("redis自定义配置启动成功!");return template;}
}

再次执行测试用例,进入redis desktop manage查看结果:

好了,配置完成。

拓展:

RedisTemplat常用序列化方式:
1、JdkSerializationRedisSerializer:使用Java的序列化功能。这是默认的序列化机制,它使用标准的Java序列化方式。
2、StringRedisSerializer:字符串序列化,用于键(key)和值(value)都是字符串的情况。
3、Jackson2JsonRedisSerializer:使用Jackson库将对象序列化为JSON字符串。同fastjson序列化方式。
4、GenericJackson2JsonRedisSerializer:是Jackson2JsonRedisSerializer的泛型版本,可以方便地序列化多种类型,而不需要为每种类型提供类型信息。
5、GenericToStringSerializer:可以将对象序列化为字符串。

http://www.lryc.cn/news/284965.html

相关文章:

  • 漫谈广告机制设计 | 听闻RTA要搞二次竞价了?牛啊!
  • 第04章_IDEA的安装与使用(下)(IDEA断点调试,IDEA常用插件)
  • HBase鉴权设计以及Kerberos鉴权方法
  • 【华为GAUSS数据库】IDEA连接GAUSS数据库方法
  • [java基础揉碎]键盘输入语句
  • Redis 面试题 | 01.精选Redis高频面试题
  • Crow:实现点击下载功能
  • 2024年华为OD机试真题-内存冷热标记-Python-OD统一考试(C卷)
  • Webpack5入门到原理9:处理字体图标资源
  • 【Docker】在Windows操作系统安装Docker前配置环境
  • Webpack5入门到原理21:提升开发体验
  • YOLOv8改进 | Conv篇 | 在线重参数化卷积OREPA助力二次创新(提高推理速度 + FPS)
  • conda国内加速
  • RabbitMQ-数据持久化
  • JS-WebAPIs-本地存储(五)
  • 了解Vue中日历插件Fullcalendar
  • Cloudreve存储策略-通过从机存储来拓展容量
  • java进阶-jvm精讲及实战
  • vue中引入sass、scss
  • Java学习笔记(八)——Lambda表达式
  • 【JavaEE】CAS
  • Linux 系统之部署 h5ai 目录列表程序
  • MySQL自增ID耗尽探究:分析与解决方案
  • 操作系统-操作系统引导(磁盘 操作系统引导过程)
  • 基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖微信小程序端(十三)
  • SAP S/4HANA 2023 Fully-Activated Appliance 虚拟机版介绍
  • 【Docker篇】详细讲解容器相关命令
  • LSTM学习笔记
  • Android 13.0 Recent列表不显示某个app
  • 速盾网络:高防ip是什么