【问题标题】:Different Caches in the same cluster for Spring Cache using AWS ElastiCache使用 AWS ElastiCache 的 Spring Cache 的同一集群中的不同缓存
【发布时间】:2016-10-13 22:58:12
【问题描述】:

我正在通过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 中创建的物理资源联系起来。是我遗漏了什么,还是 Spring Cloud 期望您的工作方式?

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

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

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

【问题讨论】:

  • 您找到解决方案了吗?
  • 老实说,我不记得了——这可能意味着我没有找到解决方案。我认为这是一个简短的研究问题,并没有走得太远(否则我可能会记得)
  • 对于类似情况的人,我已经添加了答案

标签: amazon-elasticache spring-cache


【解决方案1】:

@EnableElastiCache 鼓励缓存的物理分离,这可能不是人们一直需要的。

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

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

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

【讨论】:

    猜你喜欢
    • 2012-09-10
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 2017-05-04
    • 2018-05-03
    • 2018-10-29
    • 1970-01-01
    • 2019-09-12
    相关资源
    最近更新 更多