31. Caching
The Spring Framework provides support for transparently adding caching to an application. At its core, the abstraction applies caching to methods, reducing thus the number of executions based on the information available in the cache. The caching logic is applied transparently, without any interference to the invoker.
|
Note |
Check the relevant section of the Spring Framework reference for more details. |
In a nutshell, adding caching to an operation of your service is as easy as adding the relevant annotation to its method:
import javax.cache.annotation.CacheResult;
import org.springframework.stereotype.Component;
_@Component_
public class MathService {
_@CacheResult_
public int computePiDecimal(int i) {
// ...
}
}
|
Note |
You can either use the standard JSR-107 (JCache) annotations or Spring’s own caching annotations transparently. We strongly advise you however to not mix and match them. |
|
Tip |
It is also possible to update or evict data from the cache transparently. |