Using Spring 3.2 with EhCache 2.9. I've annotated a zero-parameter method as follows:
@Cacheable(value="myList", key="#result.method.name")
protected List<MyObject> getMyList() {
//db query
//return list of results
}
EhCache config:
<cache name="myList"
statistics="true"
maxEntriesLocalHeap="1"
timeToLiveSeconds="3600">
<persistence strategy="none" />
</cache>
I'd like the database results to be cached. Since this method has no parameters, I've chosen the method name to be the cache key.
When I test this, the database is hit every method invocation and I'm not sure why. Any ideas?
UPDATE
So after troubleshooting I found something interesting. Currently the getMyList
method (on which the caching is defined) is in the same class that calls it. That method basically calls a DAO to query for the list. If I move getMyList
outside to another class which just acts as a proxy, and then I change the original invoker to call this new proxy instead, then the caching works. I can't explain why. Any input?