java - 如何删除缓存中的条目
问题描述
我正在使用 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 语句中的条件,我将如何从缓存中删除客户对象?
解决方案
首先,看一下@CacheEvict
注解:
然后,看一下“条件”缓存:
@CacheEvict
具有许多与以下相同的属性@Cacheable
:
希望这可以帮助!