【问题标题】:How to delete an entry in a cache如何删除缓存中的条目
【发布时间】:2019-07-04 15:00:04
【问题描述】:

我正在使用 Spring 缓存。如果在适当的时候满足特定条件,我想删除缓存中的单个条目。

   @Cacheable(value = "statusEligibility", key = "#customerId")
   @GetMapping
   public CustomerStatusDTO getCustomerStatus(@PathVariable String customerId) {

  Customer customer = cusomterPort.getAccount(customerId);
  Status status = service.getStatus(customer);

  //Logic:
  //If status equals required then forward call to service to assert it. then delete the account from cache
  //else return not_required
  if (status.equals(Cons.REQUIRED)) {
     /.../
  } else {
     /.../
  }

}

如果客户对象满足 if 语句中的条件,我将如何从缓存中删除它?

【问题讨论】:

  • 你试过private final @Autowired CacheManager manager在方法中调用manager.getCache("cacheName").evict(key);吗?
  • 我创建了一个用 cacheevict 注释的方法,其中包含相关的值和键。我已经写了一个单元测试,但它似乎没有通过缓存

标签: java spring spring-cache


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-18
  • 1970-01-01
  • 2010-10-29
  • 1970-01-01
  • 2012-09-16
  • 2014-10-23
相关资源
最近更新 更多