在Spring Boot应用程序中使用Redis缓存的步骤:

1、要获得Redis连接,我们可以使用Lettuce或Jedis客户端库,Spring Boot 2.0启动程序spring-boot-starter-data-redis默认使用了Lettuce。要获得Redis的池化连接工厂,我们需要在类路径上提供commons-pool2,下面展示使用Lettuce情况下的Maven依赖项配置:

org.springframework.bootspring-boot-starter-data-redisorg.apache.commonscommons-pool2

2、在application.properties中配置spring.redis.*前缀的参数:

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.lettuce.pool.max-active=7
spring.redis.lettuce.pool.max-idle=7
spring.redis.lettuce.pool.min-idle=2
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.shutdown-timeout=200ms
spring.cache.redis.cache-null-values=false
spring.cache.redis.time-to-live=600000
spring.cache.redis.use-key-prefix=true

3、在 @SpringBootApplication类使用@EnableCaching激活Redis:

@SpringBootApplication
@EnableCaching
publicclassSpringBootAppStarter {
publicstaticvoidmain(String[] args) {
SpringApplication.run(SpringBootAppStarter.class, args);
}
}

如果想用Jedis 客户端,需要排除Lettuce,加入Jedis:

org.springframework.bootspring-boot-starter-data-redisio.lettucelettuce-coreredis.clientsjedis

jedis将自动在类路径上解决commons-pool2, application配置:

spring.redis.host=localhost 
spring.redis.port=6379
spring.redis.password= spring.redis.jedis.pool.max-active=7
spring.redis.jedis.pool.max-idle=7
spring.redis.jedis.pool.min-idle=2
spring.redis.jedis.pool.max-wait=-1ms

写在最后:欢迎留言讨论,加关注,持续更新!!!

阿里P7级教你如何在Spring Boot应用程序中使用Redis的更多相关文章

  1. Spring boot 2.x 中使用redis

    一.添加Maven  依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...

  2. 如何在Spring Boot中使用Cookies

    一. 导读 本文大纲 读取HTTP Cookie 设置HTTP Cookie 读取所有Cookie[] 为Cookie设置过期时间 Https与Cookie HttpOnly Cookie 删除Coo ...

  3. 阿里架构师的这一份Spring boot使用心得:网友看到都收藏了

    阿里架构师的这一份Spring boot使用心得: 这一份PDF将从Spring Boot的出现开始讲起,到基本的环境搭建,进而对Spring的IOC及AOP进行详细讲解.以此作为理论基础,接着进行数 ...

  4. 如何在Spring boot中修改默认端口

    文章目录 介绍 使用Property文件 在程序中指定 使用命令行参数 值生效的顺序 如何在Spring boot中修改默认端口 介绍 Spring boot为应用程序提供了很多属性的默认值.但是有时 ...

  5. 如何在Spring Boot开启事务

    说到事务,那什么是事务呢? 事务(Transaction),一般是指要做的或所做的事情. 原子性(Atomicity):事务作为一个整体被执行,包含在其中的对数据库的操作要么全部被执行,要么都不执行. ...

  6. 手把手教你定制标准Spring Boot starter,真的很清晰

    写在前面 我们每次构建一个 Spring 应用程序时,我们都不希望从头开始实现具有「横切关注点」的内容:相反,我们希望一次性实现这些功能,并根据需要将它们包含到任何我们要构建的应用程序中 横切关注点 ...

  7. Spring Boot 2.x 缓存应用 Redis注解与非注解方式入门教程

    Redis 在 Spring Boot 2.x 中相比 1.5.x 版本,有一些改变.redis 默认链接池,1.5.x 使用了 jedis,而2.x 使用了 lettuce Redis 接入 Spr ...

  8. 使用Maven插件构建Spring Boot应用程序镜像

    使用Maven插件构建Spring Boot应用程序的Docker镜像. 环境准备 1.Linux系统 2.安装JDK,Maven 3.安装Docker 应用实践 1.在应用程序根目录下添加Docke ...

  9. docker 部署Spring Boot:Docker化Spring Boot应用程序

    第一章 1.创建项目存放目录 mkdir /root/sproot -p 2.准备好Spring Boot应用程序 jar 包 testrest.jar 第二章 1. 安装docker 在所有节点执行 ...

随机推荐

  1. 浅谈Delphi高效使用TreeView

    本来我一直都是使用递归算法, 效率很低 下边这段代码是我原来写的 ------------------------------------------------------------------- ...

  2. 【c# 学习笔记】c#中的语句

    1.条件语句: if语句: bool condition = true; if (condition) { } else if (condition) { } else { } switch语句: b ...

  3. Spring 分布式事务详解

    在学习分布式事务的过程中会遇到以下关键名词: 相关名词: XA :XA规范的目的是允许多个资源(如数据库,应用服务器,消息队列,等等)在同一事务中访问,这样可以使ACID属性跨越应用程序而保持有效.X ...

  4. CentOS 7 命令

    常用命令 文件与目录操作 命令 解析 cd /home 进入 ‘/home’ 目录 cd .. 返回上一级目录 cd ../.. 返回上两级目录 cd - 返回上次所在目录 cp file1 file ...

  5. 【miscellaneous】视频浓缩摘要简介

    视频摘要,就是以自动或者半自动的方式,通过分析视频的结构和内容存在的时空冗余,从原始视频中提取有意义的片段,将它们以某种特点的方式重新组合成紧凑的.能够充分表现视频语义内容的浓缩视频. 一.静态视频摘 ...

  6. CenOS 7 java链接redis数据库

    完整代码 public class App { public static void main(String[] args) { System.out.println("Hello Worl ...

  7. easyui loadFilter 使用

    $('#selectChannelForSignup_getBill').combobox({ url:'../channel/listAdvanceChannelPage', queryParams ...

  8. python map函数(23)

    截至到目前为止,其实我们已经接触了不少的python内置函数,而map函数也是其中之一,map函数是根据指定函数对指定序列做映射,在开发中使用map函数也是有效提高程序运行效率的办法之一. 一.语法定 ...

  9. Python基础——matplotlib库的使用与绘图可视化

    1.matplotlib库简介: Matplotlib 是一个 Python 的 2D绘图库,开发者可以便捷地生成绘图,直方图,功率谱,条形图,散点图等. 2.Matplotlib 库使用: 注:由于 ...

  10. PCL学习(五)如何在mesh模型上sample更多点及三维物体姿态估计

    ---恢复内容开始--- 最近在做关于物体姿态估计的项目 基本思路就是 我们在估计物体的pose的时候,需要用分割得到的点云与模型库中的模型做匹配 1.通过基于RANSANC的SAC-IA将点云和模型 ...