使用AWS ElastiCache在Spring Cache的同一集群中使用不同的缓存

时间:2016-06-13 13:34:12

标签: amazon-elasticache spring-cache

我正在通过http://cloud.spring.io/spring-cloud-aws/spring-cloud-aws.html努力将Spring Cache集成到我们的AWS应用程序中。

有人可以向我解释如何使用指南正确封装不同的@Cacheable类吗?

据我所知,当您使用@EnableElastiCache时,您需要指定在注释中在AWS中创建的Elasticache集群的名称:

@EnableElastiCache( @CacheClusterConfig( name = "myAwsCluster", expiration = 300 ) )

然后您拥有以使用与@Cacheable类中的缓存名称相同的群集名称:

@Cacheable( "myAwsCluster" )
public String expensiveMethod()

除非我遗漏了某些内容,否则这会完全破坏封装,因为您必须将注释上的值与您在AWS中创建的物理资源相关联。我错过了什么,或者这是春云希望你工作的方式?

此外,这意味着您需要为要使用的每个春季Cache类启动单独的AWS ElastiCache群集,这使得它非常昂贵并且禁止资源共享。

@CacheConfig( "myAwsCluster" )
public class Class1
{
    @Cacheable
    public void something()
    {
        ...
    }
}

@CacheConfig( "mySecondAwsCluster" )
public class Class2
{
    @Cacheable
    public void somethingElse()
    {
        ...
    }
}

1 个答案:

答案 0 :(得分:0)

@EnableElastiCache鼓励物理上分离缓存,而这可能并非一直都需要。

使用@EnableElastiCache进行缓存配置,而不使用@EnableCaching。它可以指向Elasticache。启用了Elasticache群集模式的Redis的示例配置。

spring.redis.cluster.nodes=<Elasticache Configuration endpoint>

现在,您可以将@Cacheable与逻辑高速缓存名称一起使用。