JCache API comes with several built in interceptors for CDI making its usage decoupled from the cache API itself and more user friendly.
Let’s have a look to this API.
CacheResult: the method execution killer
Probable one of the most common use cache is to avoid to pay the cost of a method each time you call it.
Reasons can be as different as:
- Computation done by the method is expensive
- The method contacts a remote service and you want to cut off the implied latency
- The method accesses a rate limited resource
- ….
In this cache @CacheResult brings a nice and easy to setup solution. Simply decorating the method with @CacheResult you will avoid the actual method invocation after the first call and while it is cached.
Basic usage
Here a sample using a service simulating a slow method: