Spring has come up with two main annotations for caching; we will be using these throughout the chapter. The following are the two annotations:
@Cacheable
: This can be used to mark the method and return values that will be stored in the cache. This can be applied at the method or type level.When applied at the method level, the annotated method's return value is cached
When applied at type level, the return value of every method is cached
@CacheEvict
: This is used for releasing objects from cache memory.
Let us look at small implementation of using @Cacheable
annotations applied at type level. We are thinking of simple DAO class, with two methods with different names. We have used the @Cacheabl
e annotation, which takes three arguments:
Value
Key
Condition
No we can implement it:
@Cacheable(value = "product") public class ProductDAO { public Product findProduct(String Name, int price) { return new Product(Name,price); } public Product findAnotherProduct...