【问题标题】:Does @Cacheable#cacheNames have nothing to do with Redis?@Cacheable#cacheNames 和 Redis 没有关系吗?
【发布时间】:2019-06-24 07:48:27
【问题描述】:

我将我的服务注释如下

@Cacheable(cacheNames = {"some"},
           key = "{#req.some, #req.other, #req.property}")
public List<Some> listSome(SomeReq req) {
    ..
}

似乎可行,我通过 Medis 看到了这些密钥。 (couponspring.cache.redis.key-prefix 的值。)

coupon{Some},{Other},{Property}
coupon{Some},{Other},{Property}
coupon{Some},{Other},{Property}
coupon{Some},{Other},{Property}
coupon{Some},{Other},{Property}
coupon{Some},{Other},{Property}

cacheNames 在哪里运行?

如果我添加具有相同键模式的其他服务方法,是否可能发生冲突?

@Cacheable(cacheNames = {"someOther"},
           key = "{#req.some, #req.other, #req.property}")    
List<SomeOther> listSomeOthers(SomeOtherReq req) {
} 

【问题讨论】:

    标签: spring spring-data-redis


    【解决方案1】:

    如果我添加具有相同键模式的其他服务方法,是否可能发生冲突?

    不,因为cacheNames 是(工作方式类似)命名空间,因此您可以在不同的命名空间中使用相同的键。

    【讨论】:

    • 即使使用 Redis,这真的是事实吗?
    • 如果您查看 Redis 键 $keys *,您会发现每个键都有 cacheName 前缀。
    • 不,我没有看到 KEYS '*' 中的任何前置 cacheNames 值。我们在谈论春天吗?
    • 我终于明白了。我需要为RedisCacheManager 定义一个bean。
    • 对不起keys *,我通常使用Spring Boot,它提供了一个默认的缓存配置,每个键都附加了cacheName。
    【解决方案2】:

    我正在为更多的读者回答我自己的问题。

    简单地说,我需要为 RedisCacheManager 定义一个 bean 以使 cacheNames 工作。

    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        final RedisCacheManager cacheManager = RedisCacheManager.builder(connectionFactory)
                .cacheDefaults(defaultCacheConfig())
                .build();
        return cacheManager;
    }
    

    如您所见,我没有更改 defaultCacheConfig() 的任何内容。

    并且键是带有{cacheName}::前缀的属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 2019-02-28
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多