@CacheEvict
The @CacheEvict is used to trigger explicit cache eviction. By default most of caching frameworks expires the cache data after some defined duration. The @CacheEvict annotation is useful when you want to control explicit cache eviction upon some method calls. It is useful for removing stale or unused data from the underlying cache.
One of the notable features of @CacheEvict is that it can be used with void methods and as a result these methods tend to act as triggers and the return values are ignored. This is not the case with @Cacheable which adds/update data into the cache and thus requires a result.
The various properties of the @CacheEvict annotation are summarized as shown in Table 10-2 below.
Table 10-2. @CacheEvict settings details
Element Name | Element Type | Description |
value | String[] | Qualifier value for the specified cached operation. It may be used to determine the target cache (s), matching the qualifier value (or the bean name(s)) of (a) specific bean definition. This is a mandatory element and all other elements are optional. |
allEntries | boolean | Specifies whether all the entries in the cache to be removed or not. The default value is “true”. |
beforeInvocation | boolean | Specifies whether the eviction should occur before or after the method invocation. The default value is “false”. |
key | String | Spring Expression Language (SpEL) attribute, which can compute keys dynamically. The default value is “” and takes in all the method parameter names as keys. |
condition | String | Spring Expression Language (SpEL) attribute, which can be used for conditioning the method cache. The default value is “”, which means that the method will always be cached. |
In Listing 10-12, after saving the customer, evict the customers from the underlying cache so that the customer list is updated and kept in the cache at all times.
Listing 10-12. Usage of @CacheEvict annotation
[java]public class CustomerRepository{
@CacheEvict(value=“customer”, allEntries=true)
public Customer saveCustomer(String customerCode){
//….After saving customer, evict the map holding the cutsomers….
}
}[/java]
@CachePut
The @CachePut annotation allows you to “update” a cache entry without interfering with the method execution. This is very similar to @Cacheable but for entry update. The @CachePut annotation has exactly the same attributes as @Cacheable.
Using @CachePut and @Cacheable annotations on the same method is generally discouraged because they have different behaviors. @Cacheable annotation causes the method execution to be skipped by using the cache and @CachePut annotation forces the execution in order to execute a cache update. This could result in unexpected behaviors and could also result in exceptions.
In the case of @CachePut, shown in Listing 10-13, the method is always executed and its returned result is put into the cache, using the provided key generator and replaces the old entry as the case may be. The only case where the method is not executed is when you provide an optional @CachePut condition and the condition is not met.
Listing 10-13. Usage of @CachePut annotation
[java]public class CustomerRepository{
@CachePut(value=“customer”)
public Customer updateCustomer(String customerCode){
//….After saving customer, update the customers map
}
}[/java]
@Caching
It is a group annotation for multiple cache annotation explained in the above sections. It groups @Cacheable, @CacheEvict and @CachePut annotation into a single annotation. This can be called as a sort of stereotype annotation. The various properties of the @Caching annotation are summarized as shown in Table 10-3 below.
Table 10-3. @Caching settings details
Element Name | Element Type | Description |
cacheable | Cacheable | Attributes in @Cacheable annotation |
evict | CacheEvict | Attributes in @CacheEvict annotation |
put | CachePut | Attributes in @CachePut annotation |
Listing 10-14. Complete class showing the usage of various cache related annotations
[java]import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;
@Repository
public class JdbcCustomerRepository implements CustomerRepository {
//…….
private final Map<string, customer=""> customers =
new ConcurrentHashMap<string, customer="">();
JdbcCustomerRepository(Map<string, customer=""> customers){
this.customers = customers;
}
@Cacheable("customer")
public Customer getCustomer(String customerCode){
return customers.get(customerCode);
}
@CacheEvict(value="customer", key="customer.customerCode")
public void saveCustomer(Customer customer){
customers.put(customer.getCustomerCode(), customer);
}
public Collection getAllCustomers() {
return customers.values();
}
//…….
}[/java]
Page Visitors: 13097

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Every weekend i used to go to see this web site, for the reason that i want enjoyment, since
this this web page conations actually pleasant funny data too.
I don’t know whether i should take your comment as positive or negative.. :). If it is positive, thank you. If it is negative, please let me know what i can improve on to make it positive.. 🙂
Regards
Tomcy John
Hi,
Is there a PDF version of this article, or at least a Web version somewhere else? This Website is really bad (full of ads) and the code samples are incomplete.
Thanks,
Lukasz
Hi Lukasz,
I plan to have a ad free version of the entire book. For that, though, i plan to distribute with a small charge. By end of October I plan to release using “Gumroad”. If you are still interested, with a small fee you can download and enjoy an ad free version of the book.
Regards
Tomcy John