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

3,329

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

博客地址:blog.720ui.com/

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

Redis Cache 集成

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

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

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

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

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

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.database=1
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=500
spring.redis.pool.min-idle=0
spring.redis.timeout=0

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

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

源代码

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

(完)

更多精彩文章,尽在「服务端思维」微信公众号!