Book Image

Mastering Spring Application Development

By : Anjana Mankale
Book Image

Mastering Spring Application Development

By: Anjana Mankale

Overview of this book

Table of Contents (19 chapters)
Mastering Spring Application Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Spring annotations for caching


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.

@Cacheable usage

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 @Cacheable 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...