Amira
Amira

Reputation: 3270

How to call caffeine stats in a seperate class

Is there a way to call caffeine cache in a seperate class implementation.

I wrote a Cache configurtion :

`@Configuration
 @EnableCaching(mode = AdviceMode.ASPECTJ)
 public class CacheConfiguration {

@Bean
public Caffeine caffeineConfig() {
    return Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).recordStats();
}

@Bean
public CacheManager cacheManager(Caffeine caffeine) {
    CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
    caffeineCacheManager.getCache("addresses");
    caffeineCacheManager.setCaffeine(caffeine);
    return caffeineCacheManager;
}
}`

I want to log statistics in another class so I did this:

public class RequestLoggingFilter extends AbstractRequestLoggingFilter {
     @Autowired
     public Caffeine                 caffeineConfig;
     
     @Override
     protected void afterRequest(HttpServletRequest request, String message) {
    // Payload could be logged only after request is processed (it uses a 
     ContentCachingRequestWrapper internally)
          this.logger.info(message);
          this.logger.info(this.caffeineConfig.build().stats().hitCount());
     }
 }

From my opinion the .build() should be called only one time. Is there another way or best practice to do it?

Upvotes: 0

Views: 409

Answers (0)

Related Questions