Customizing the cache key

You can customize the cache key by using a key attribute of @Cacheable and the @CachePut annotation. The cache key is derived by a SpEL expression using properties of the object as highlighted key attribute in the following snippet of code. Let's look at the following examples:

    @Cacheable(cacheNames=" accountCache ", key="#accountId") 
    public Account findAccount(Long accountId) 
 
    @Cacheable(cacheNames=" accountCache ", key="#account.accountId") 
    public Account findAccount(Account account) 
 
    @CachePut(value=" accountCache ", key="#account.accountId") 
    Account save(Account account); 

You can see in the preceding code snippets how we have created the cache key by using the key attribute of the @Cacheable annotation.

Let's see another attribute of these annotations in a Spring application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset