java - Spring Data Store Redis——使用多个缓存

标签 java spring-cache spring-data-redis

我有一个由两个网络服务器、两个 Redis 缓存和一个后端存储组成的应用程序。两个网络服务器和两个 redis 缓存位于西海岸和东海岸,以优化性能。到目前为止,我已经能够从我的网络服务器连接到我的第一个缓存,并连接到我的后端存储。但我正在寻找一种使用 Spring 将数据推送到两个 redis 缓存的方法。我已按如下方式配置我的两个 RedisManagers。

@Bean(name="CacheManager1")
public RedisCacheManager cacheManager() {
    RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
    redisCacheManager.setTransactionAware(true);
    redisCacheManager.setLoadRemoteCachesOnStartup(true);
    redisCacheManager.setUsePrefix(true);
    return redisCacheManager;
}

@Bean(name="CacheManager2")
public RedisCacheManager cacheManager2() {
    RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate2());
    redisCacheManager.setTransactionAware(true);
    redisCacheManager.setLoadRemoteCachesOnStartup(true);
    redisCacheManager.setUsePrefix(true);
    return redisCacheManager;
}

现在我已经尝试了一些随机的方法来缓存到这两个地方,但是这种特殊的方法失败了“Cacheable is not a Repeatable Annotation”

@Cacheable(cacheManager = "CacheManager1", value = "activityProfile", key = "#id")
@Cacheable(cacheManager = "CacheManager2", value = "activityProfile", key = "#id")
public ActivityProfile findActivityProfile(String id) {
    return activityProfileRepository.findOne(id);
}

是否有任何简单的方法可以为此使用 Spring?

最佳答案

Dan Ciborowski - 看这里...

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#cache-annotations-caching

Spring 引用解释了如何组合多个 Spring Cache Abstraction使用 @Caching 的注释(例如 @Cacheable@CachePut 等)。

因此对于可缓存,您可以执行以下操作。

    @Caching(cacheable = {@Cacheable("CacheManager1"), @Cacheable("CacheManager2")})
    public ActivityProfile findActivityProfile(String id) {
        return activityProfileRepository.findOne(id);
    }

关于java - Spring Data Store Redis——使用多个缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41401484/

相关文章:

java - Java Swing 中不显示进度监视器

java - 添加自定义处理器后 Apache Nifi 不启动

redis - 有什么方法可以在访问 Redis 后立即从中驱逐 key ?

spring-boot - 使用 Spring Cacheable 的 L1 + L2 缓存策略

java - 在 1 个测试方法中使用 junit @Rule、expectMessage()、匹配器来处理多个异常

java - 从死信 channel 重新传递消息

java - 使用@Cacheable的Spring缓存在启动时不起作用@PostConstruct

java - 基于 Spring 的单个 ehcache 管理器,用于多模块项目中的多个 ehcache.xml 文件

java - Spring @CachePut 用两个键放置相同的值

spring-data - 无法获得Redis的连接Spring数据Redis的模板