简体   繁体   中英

How to debug spring cacheable?

We are trying to figure out how to tell whether spring's annotation "@Cacheable" is working or not. So far we've been debugging the application (running the IDE in debug's mode), and what we can see is that we are actually getting within the method's implementation. Wasn't that exactly not supposed to happen? Considered that it is cached.

The debugging turn out to be as expected (ie the cached method "should skip" once is effectively chached). The issue probed to be an environment misconfiguration in the application's environment.

I achieved this by simply Autowiring the cacheManager. And then adding this code

Object cached = cacheManager.getCache([your cache name]).get([your_cache_key]);
if (cached == null) {
     cached = cachableAnnotatedMethod();
     cacheManager.getCache([your cache name]).put([your cache key], cached);
}

I know this is pretty naive as cacheable might work different, but would take you to the point on most of the cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM