Spring Boot 揭秘与实战(二) 数据缓存篇

文章目录

  1. 1. Redis Cache 集成
  2. 2. 源代码

本文,讲解 Spring Boot 如何集成 Redis Cache,实现缓存。

在阅读「Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门」后,对 Spring Boot 集成缓存机制有一定了解后,我们来了解下 Redis Cache 的使用。

Redis Cache 集成

Redis Cache 有非常丰富的使用场景,如果有兴趣的话,可以阅读这篇文章「Redis实战(五) 聊聊Redis使用场景」。

在 Spring Boot 中集成 Redis Cache 非常容易,只需要两个步骤。

首先,在 pom.xml 中增加Redis 依赖。

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-redis</artifactId>
  4. </dependency>

第二步,在 src/main/resources/application.properties 中配置数据源信息。

  1. spring.redis.host=localhost
  2. spring.redis.port=6379
  3. spring.redis.password=
  4. spring.redis.database=1
  5. spring.redis.pool.max-active=8
  6. spring.redis.pool.max-wait=-1
  7. spring.redis.pool.max-idle=500
  8. spring.redis.pool.min-idle=0
  9. spring.redis.timeout=0

运行起来,控制台打印的日志信息,说明已经是 Redis Cache 实例,说明 Redis Cache 开启成功了。

  1. Bean 'cacheManager' of type [class org.springframework.data.redis.cache.RedisCacheManager]

源代码

相关示例完整代码: springboot-action

(完)

微信公众号
原文地址:https://www.cnblogs.com/cnblog-long/p/7243055.html