Spring系列之Redis的两种集成方式

本文介绍了SpringBoot中集成Redis的两种方式:Jedis和Spring-data-redis。通过详细步骤展示了从添加依赖、配置到实际使用的全过程,并提及使用Spring-data-redis时可能遇到的序列化问题及其解决方案。最后,文章建议使用Spring-data-redis,以利用其带来的便利。
摘要由CSDN通过智能技术生成

在工作中,我们用到分布式缓存的时候,第一选择就是Redis,今天介绍一下SpringBoot如何集成Redis的,分别使用Jedis和Spring-data-redis两种方式。

一、使用Jedis方式集成

1、增加依赖
<!-- spring-boot-starter-web不是必须的,这里是为了测试-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>
<dependency>
<!-- fastjson不是必须的,这里是为了测试-->
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.73</version>
</dependency>
2、配置项
redis.host=localhost
redis.maxTotal=5
redis.maxIdle=5
redis.testOnBorrow=true
#以下方式也可以,SpringBoot同样能将其解析注入到JedisPoolConfig中
#redis.max-total=3
#redis.max-idle=3
#redis.test-on-borrow=true
3、配置连接池
/**
 * @author 公-众-号:程序员
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值