Spring Boot中如何配置Redis缓存?

Sou*_*aik 2 java spring caching redis spring-boot

如何使用 Spring Boot 配置 Redis 缓存。据我所知,这只是application.properties文件中的一些更改,但不知道具体是什么。

Con*_*est 5

要在 Spring boot 应用程序中使用Redis缓存,您需要做的就是在application.properties文件中设置这些

spring.cache.type=redis
spring.redis.host=localhost //add host name here
spring.redis.port=6379
Run Code Online (Sandbox Code Playgroud)

在您的中添加此依赖项pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

此外,您必须@EnableCaching在主应用程序类上使用 并@Cacheable在方法上使用注释才能使用缓存。redis这就是在 Spring boot 应用程序中使用所需的全部内容。您可以通过自动装配 CacheManager 在任何类中使用它,在本例中为 RedisCacheManager。

@Autowired
RedisCacheManager redisCacheManager;
Run Code Online (Sandbox Code Playgroud)