hls
hls

Reputation: 23

Spring with ehcache - @Cacheable method does not cache

When I call for the method I annotate with @Cacheable("myTest"), I know the cache is not being utilized because I did a print inside the method and it should not be printed if cache is used.

In the log, I know the cache is being initialized

DEBUG net.sf.ehcache.store.MemoryStore - Initialized net.sf.ehcache.store.MemoryStore for myTest

I should mention the way to call the method is regular Java new MyCacheObject().cacheMethod("key"); instead of using the Spring bean. Will the cache work in this way?

Upvotes: 0

Views: 322

Answers (1)

Ali Dehghani
Ali Dehghani

Reputation: 48193

I should mention the way to call the method is regular Java new MyCacheObject().cacheMethod("key"); instead of using the Spring bean. Will the cache work in this way?

Spring should be able to manage your object life cycle in order to create the required proxy that adds the caching behavior. So, this approach won't work. If you can't let the spring create the beans for you, the only option is to do the cache inspection manually in cacheMethod and implement the typical get-if-not-found-then-proceed-and-put-eventually workflow.

Upvotes: 1

Related Questions