I am wondering if there is a built in way to know wheather or not an object is returned frome SpringBoot cache. I am using SpringBoot 2.2.2 and Caffeine 2.8.0. I am creating a Caffeine bean as follows:
@Bean
public Caffeine caffeineConfig() {
return Caffeine.newBuilder().maximumSize(1000).expireAfterWrite(4, TimeUnit.HOURS);
}
I noticed that the builder has a removalListener method. What I need is something like objectServedFromCacheListener. In other words, I would like to get notified when an object is served or returned from the cache. Given that such a method does not exist, how can I know when an object is returned from the cache? I can use service method invocation time, expecting that a real invocation would take over a few seconds, and if the method returns within less than a second it would mean that the object was returned from a cache. But this seems awkward. I have an internal profiling mechanism, based on which I can determine if the service method was called. But I was expecting there to be a built in way. Is there?