Adding caching annotations

The next step is to add the caching annotations, indicating when something needs to be added or removed from the cache. The following snippet shows an example:

    @Component    public class ExampleRepository implements Repository {      @Override      @Cacheable("something-cache-key")      public Something getSomething(String id) {          //Other code      }    }

Some of the annotations that are supported are as follows:

  • Cacheable: Used to cache the result of a method invocation. The default implementation constructs the key based on the parameters passed to the method. The method will not be invoked if the value is found in the cache.
  • CachePut: Similar to @Cacheable. A significant difference is that the method is always invoked and the result is ...

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.