6. 缓存

在云环境中使用缓存有助于应用程序减少延迟并节省数据库往返次数。减少数据库往返次数可显著降低对数据库实例的要求。Spring 框架自 3.1 版本起提供了一个统一的缓存抽象,以便在应用程序中实现类似于声明式事务的声明式缓存。spring-doc.cadn.net.cn

Spring Cloud AWS 将 Amazon ElastiCache 服务集成到 Spring 统一缓存抽象中,提供基于 memcached 和 Redis 协议的缓存管理器。Spring Cloud AWS 的缓存支持为 ElastiCache 提供了其自身的 memcached 实现,并使用 Spring Data Redis 来处理 Redis 缓存。spring-doc.cadn.net.cn

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),其中在针对 ElastiCache 进行测试时内部使用 Jedis。以下示例展示了包含 Jedis 的 Redis 依赖定义。spring-doc.cadn.net.cn

<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 驱动程序,并将自动使用其中一种。spring-doc.cadn.net.cn

6.2. 使用 XML 配置缓存

Spring Cloud AWS 的缓存支持位于上下文模块中,因此如果项目中已导入上下文模块,则可以使用该功能。缓存集成提供其自身的命名空间,用于配置托管在 Amazon ElastiCache 服务中的缓存集群。以下示例包含缓存集群的配置以及启用声明式、基于注解的缓存的 Spring 配置。spring-doc.cadn.net.cn

<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 集群spring-doc.cadn.net.cn

6.2.1. 混合缓存

应用程序可能需要多个缓存,这些缓存由一个中央缓存集群进行维护。Spring Cloud AWS 缓存支持允许在同一个缓存管理器中定义多个缓存,同时也可将外部定义的缓存用于该缓存管理器中。spring-doc.cadn.net.cn

下面的示例展示了一个配置示例,其中包含一个预配置的缓存,该缓存具有 cache-ref 个元素(这可能是一个本地缓存),以及一个 cache-cluster 的 ElastiCache 缓存集群配置。spring-doc.cadn.net.cn

<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 服务。spring-doc.cadn.net.cn

元素 cache-cluster 接受一个 expiration 属性,用于定义过期时间(单位:秒)。未配置任何值意味着存在无限期的过期时间。spring-doc.cadn.net.cn

<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 类中,可使用 Spring Cloud AWS 提供的 org.springframework.cloud.aws.cache.config.annotation.EnableElastiCache 注解来配置缓存。以下示例展示了两个缓存集群的配置。spring-doc.cadn.net.cn

@EnableElastiCache({@CacheClusterConfig(name = "firstCache"), @CacheClusterConfig(name = "secondCache")})
public class ApplicationConfiguration {
}
如果将 value 属性留空,则您 CloudFormation 堆栈中的所有缓存(如果可用)都将自动配置。

6.3.1. 配置缓存的过期时间

Java 配置还允许配置缓存的过期时间。可以使用以下示例中的 defaultExpiration 属性为所有缓存设置过期时间。spring-doc.cadn.net.cn

@EnableElastiCache(defaultExpiration = 23)
public class ApplicationConfiguration {
}

缓存过期时间可以在缓存级别通过 @CacheClusterConfig 注解的 expiration 属性来定义,如下所示(以秒为单位)。spring-doc.cadn.net.cn

@EnableElastiCache({@CacheClusterConfig(name = "firstCache", expiration = 23), @CacheClusterConfig(name = "secondCache", expiration = 42)})
public class ApplicationConfiguration {
}

6.4. 在 Spring Boot 中配置缓存

缓存将在 Spring Boot 中自动配置,无需任何显式配置属性。spring-doc.cadn.net.cn

6.5. 使用缓存

根据缓存的配置,开发者可以使用注解来标记其方法,以利用缓存来存储方法的返回值。下一个示例为一个服务类中的缓存声明,该服务的返回值应被缓存。spring-doc.cadn.net.cn

@Service
public class ExpensiveService {

    @Cacheable("CacheCluster")
    public String calculateExpensiveValue(String key) {
        ...
    }
}

6.6. Memcached 客户端实现

对于 Java 开发,有不同可用的 memcached 客户端实现,最突出的是 SpymemcachedXMemcachedspring-doc.cadn.net.cn

spring-doc.cadn.net.cn

Amazon AWS 支持动态配置,并基于 自动发现 将增强的 memcached 客户端交付给新的节点,该客户端基于 Spymemcached,基于中央配置终结点。spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

Spring Cloud AWS依赖于Amazon ElastiCache客户端实现,因此对其有依赖关系。spring-doc.cadn.net.cn

6.7. 使用CloudFormation

Amazon ElastiCache 集群也可以在一个堆栈内进行配置,然后由应用程序使用。Spring Cloud AWS 还支持通过逻辑名称查找由堆栈配置的缓存集群,并将其解析为物理名称。下面的示例显示了 CloudFormation 模板内部的缓存集群配置。spring-doc.cadn.net.cn

"CacheCluster": {
    "Type": "AWS::ElastiCache::CacheCluster",
    "Properties": {
        "AutoMinorVersionUpgrade": "true",
        "Engine": "memcached",
        "CacheNodeType": "cache.t2.micro",
        "CacheSubnetGroupName" : "sample",
        "NumCacheNodes": "1",
        "VpcSecurityGroupIds": ["sample1"]
    }
}

缓存集群随后可以在应用配置中使用名称CacheCluster,如下面所示:spring-doc.cadn.net.cn

<beans...>
    <aws-cache:cache-manager>
        <aws-cache:cache-cluster name="CacheCluster" expiration="15"/>
    </aws-cache:cache-manager>
<beans>

在上述配置下,应用程序可以在不同的环境中部署多个堆栈,而无需对应用程序内的配置进行任何更改。spring-doc.cadn.net.cn