6. 缓存
云环境中的缓存对于应用程序来说非常有用,可以减少延迟并节省数据库往返。 减少数据库往返可以显著降低对数据库实例的要求。Spring 框架 从 3.1 版开始,提供了一个统一的缓存抽象,以允许在类似于 声明易。
Spring Cloud AWS 将 Amazon ElastiCache 服务集成到 Spring 统一的 缓存抽象,提供基于 memcached 和 Redis 协议的缓存管理器。对 Spring 的缓存支持 Cloud AWS 为 ElastiCache 提供了自己的 memcached 实现,并使用 Spring Data Redis 进行 Redis 缓存。
6.1. 配置 Redis 缓存的依赖项
Spring Cloud AWS 提供了自己的 memcached 缓存实现,因此不需要其他依赖项。对于 Redis Spring Cloud AWS 依赖于 Spring Data Redis 来支持缓存,并允许使用多个 Redis 驱动程序。Spring Cloud AWS 支持 Spring Data Redis 支持的所有 Redis 驱动程序(目前是 Jedis、JRedis、SRP 和 Lettuce)和 Jedis 在内部用于针对 ElastiCache 进行测试。示例中显示了 Redis 与 Jedis 的依赖关系定义
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring-data-redis.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
Spring Cloud AWS 将自动检测 Redis 驱动程序并自动使用其中一个。
6.2. 使用 XML 配置缓存
Spring Cloud AWS 的缓存支持驻留在上下文模块中,因此可以在上下文模块 已导入到项目中。缓存集成提供了自己的命名空间来配置缓存集群,这些缓存集群是 托管在 Amazon ElastiCache 服务中。下一个示例包含缓存集群和 Spring 配置以启用声明性、基于注释的缓存。
<beans xmlns:aws-cache="http://www.springframework.org/schema/cloud/aws/cache"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/cloud/aws/cache
http://www.springframework.org/schema/cloud/aws/cache/spring-cloud-aws-cache.xsd
http://www.springframework.org/schema/cache
https://www.springframework.org/schema/cache/spring-cache.xsd">
<aws-context:context-credentials>
...
</aws-context:context-credentials>
<aws-cache:cache-manager>
<aws-cache:cache-cluster name="CacheCluster" />
</aws-cache:cache-manager>
<cache:annotation-driven />
</beans>
上面的配置配置了cache-manager
使用一个名为CacheCluster
代表 ElasticCache 集群。
6.2.1. 混合缓存
应用程序可能需要由一个中央缓存群集维护的多个缓存。The Spring Cloud AWS 缓存支持允许在一个缓存管理器内定义多个缓存,也可以使用外部定义的缓存 在缓存管理器中。
下面的示例演示了一个配置示例,其中包含一个预配置的缓存,其中包含cache-ref
元素
(可能是本地缓存)和cache-cluster
ElastiCache 缓存集群的配置。
<beans ...>
<aws-cache:cache-manager id="cacheManager">
<aws-cache:cache-ref ref="memcached" />
<aws-cache:cache-cluster name="SimpleCache"/>
</aws-cache:cache-manager>
</beans>
6.2.2. 定义到期时间
Spring 缓存分界不支持到期时间配置,并将其留给缓存实现 以支持到期时间。Spring Cloud AWS 缓存配置支持每个缓存的到期时间设置。这 到期时间将传递给 memcached 服务。
这cache-cluster
元素接受一个过期属性,该属性定义了过期时间(以秒为单位)。
未配置值意味着存在无限的到期时间。
<beans>
<aws-cache:cache-manager>
<aws-cache:cache-cluster expiration="10000" name="CacheCluster" />
</aws-cache:cache-manager>
</beans>
6.3. 使用 Java 配置配置缓存
Spring Cloud AWS 还支持使用 Java 配置类进行缓存配置。在任何Configuration
类
可以使用org.springframework.cloud.aws.cache.config.annotation.EnableElastiCache
Spring Cloud AWS 提供的注释。下一个示例显示了两个缓存集群的配置。
@EnableElastiCache({@CacheClusterConfig(name = "firstCache"), @CacheClusterConfig(name = "secondCache")})
public class ApplicationConfiguration {
}
如果您将value 属性为空,则 CloudFormation 堆栈中的所有缓存(如果可用)
将自动配置。 |
6.3.1. 配置缓存的到期时间
Java 配置还允许配置缓存的到期时间。这可以为所有人完成
缓存使用defaultExpiration
属性,如下例所示。
@EnableElastiCache(defaultExpiration = 23)
public class ApplicationConfiguration {
}
可以使用@CacheClusterConfig
annotations expiration 属性,如下所示(使用秒作为
值)。
@EnableElastiCache({@CacheClusterConfig(name = "firstCache", expiration = 23), @CacheClusterConfig(name = "secondCache", expiration = 42)})
public class ApplicationConfiguration {
}
6.4. 在 Spring Boot 中配置缓存
缓存将在 Spring Boot 中自动配置,无需任何显式配置属性。
6.5. 使用缓存
根据缓存的配置,开发人员可以批注其方法,以将缓存用于方法返回值。 下一个示例包含应缓存返回值的服务的缓存声明
@Service
public class ExpensiveService {
@Cacheable("CacheCluster")
public String calculateExpensiveValue(String key) {
...
}
}
6.6. Memcached 客户端实现
Java 有不同的 memcached 客户端实现,最著名的是 Spymemcached 和 XMemcached。 Amazon AWS 支持动态配置,并提供基于 Spymemcached 的增强型 memcached 客户端,以支持基于 中央配置终结点。
Spring Cloud AWS 依赖于 Amazon ElastiCache 客户端实现,因此依赖于它。
6.7. 使用 CloudFormation
Amazon ElastiCache 集群也可以在堆栈中进行配置,然后供应用程序使用。Spring Cloud AWS 还支持按逻辑名称查找堆栈配置的缓存集群,并解析为物理 名字。以下示例显示了 CloudFormation 模板中的缓存集群配置。
"CacheCluster": {
"Type": "AWS::ElastiCache::CacheCluster",
"Properties": {
"AutoMinorVersionUpgrade": "true",
"Engine": "memcached",
"CacheNodeType": "cache.t2.micro",
"CacheSubnetGroupName" : "sample",
"NumCacheNodes": "1",
"VpcSecurityGroupIds": ["sample1"]
}
}
然后,缓存集群可以与名称CacheCluster
在应用程序配置中,如下所示:
<beans...>
<aws-cache:cache-manager>
<aws-cache:cache-cluster name="CacheCluster" expiration="15"/>
</aws-cache:cache-manager>
<beans>
通过上述配置,可以在不同的环境中部署多个堆栈的应用程序 无需在应用程序内部进行任何配置更改。