161. Redis 的 Cloud Memorystore

161.1Spring 缓存

Redis 的 Cloud Memorystore提供了完全托管的内存中数据存储服务。 Cloud Memorystore 与 Redis 协议兼容,可轻松与Spring Caching集成。

您要做的就是创建一个 Cloud Memorystore 实例,并将其在application.properties文件中的 IP 地址用作spring.redis.host属性值。其他一切与设置由 Redis 支持的 Spring 缓存完全相同。

Note

Memorystore 实例和您的应用程序实例必须位于同一区域。

简而言之,需要以下依赖项:

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

然后,您可以对要缓存的方法使用org.springframework.cache.annotation.CacheableComments。

@Cacheable("cache1")
public String hello(@PathVariable String name) {
    ....
}

如果您对详细的操作指南感兴趣,请选中使用 Cloud Memorystore Codelab 的 Spring Boot 缓存

可以找到here的 Cloud Memorystore 文档。