Declarative Annotation-based caching
In Spring applications, Spring's abstraction provides the following Annotations for caching declaration:
@Cacheable
: This indicates that before execution of the actual method, look at the return value of that method in the cache. If the value is available, return this cached value, if the value is not available, then invoke the actual method, and put the returned value into the cache.@CachePut
: This updates the cache without checking if the value is available or not. It always invokes the actual method.@CacheEvict
: This is responsible for triggering cache eviction.@Caching
: This is used for grouping multiple annotations to be applied on a method at once.@CacheConfig
: This indicates to Spring to share some common cache-related settings at the class level.
Let us now take a closer look at each annotation.
The @Cacheable annotation
@Cacheable
marks a method for caching. Its result is stored in a cache. For all subsequent invocations of that method with the same...