0

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?

Jacobs2000
  • 856
  • 2
  • 15
  • 25
  • Maybe provide a delegating intercept for Spring Cache (see [answer](https://stackoverflow.com/questions/34308003/spring-aop-that-intercepts-org-springframework-cache-interceptor-cacheintercepto)? – Ben Manes May 13 '21 at 02:39
  • I ended up using an internal profiling mechanism (AOP-based) to determine if the service method was called. Started a discussion on Caffeine - https://github.com/ben-manes/caffeine/discussions/555 . – Jacobs2000 May 23 '21 at 13:12

0 Answers0