【发布时间】: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