【问题标题】:Using CachePut and Cacheable使用 CachePut 和 Cacheable
【发布时间】:2020-09-30 05:48:33
【问题描述】:

我有两种方法,如下所示。一种用@CachePut 注释,另一种用@Cacheable 注释。它们都使用相同的键。根据其他帖子的一些答案,我认为@cacheput 替换了该值。如果是这种情况,如果文档中的某个字段有更新,是否会为相同的 id 执行带有 @Cacheable 注释的方法。

@CachePut(value="testcache",key="#document.id")
public Document update(Document document){
// code to update db
return document
}

@Cacheable(value="testcache")
public Document getDocument(String id){
// query database and fetch document
 return document
}

【问题讨论】:

    标签: spring-boot hazelcast spring-cache


    【解决方案1】:

    只有当 key 在缓存中没有值 时,才会执行带有 @Cacheable 注释的方法。因此,您的 @CachePut 是否更新了文档并不重要。只要文档在缓存中,getDocument(id) 就不会进入数据库。 (CachePut 会为您更新数据库和缓存

    【讨论】:

      猜你喜欢
      • 2015-04-15
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 2019-12-09
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 2015-11-26
      相关资源
      最近更新 更多