Reputation: 167
I am using Spring cache. I would like to delete a single entry within the cache if a particular condition is met in due time.
@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 {
/.../
}
}
How would I be able to delete the customer object from the cache if it fulfils the condition in the if statement?
Upvotes: 0
Views: 2441
Reputation: 7991
First, have a look at the @CacheEvict
annotation:
Then, have a look at "conditional" caching:
@CacheEvict
has many of the same attributes as @Cacheable
:
Hope this helps!
Upvotes: 1