Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
Caching Abstraction ignores method name? [SPR-8933] #13573
When I read the reference documentation I thought the caching abstraction is on method level, but it seems it is only on the parameter value of the methods.
If I have several methods with the same parameter type it get always the same return value:
Is this the desired behaviour? I would expect:
If I use different caches it works.
I can't access github from my place (firewall) so I have added a tar with a small maven project showing this problem.
Affects: 3.1 GA
5 votes, 9 watchers
Alexander Derenbach commented
If this is the intended behaviour it would be nice if either the value field of
or to add a new annotation for that to Spring itself
Costin Leau commented
This is not a bug. All 4 methods use the same key and the same cache, thus, after you call test1, all the other methods will automatically find in the cache the same value.
Ned Mules commented
Thanks for your work on this great feature.
But I agree with Alex that its behaviour is counter-intuitive.
I may be missing something but why would you typically want methods with different names and the same parameters to return the same values from the cache?
For example, if I was building a music catalogue I would like to do this:
As far as I can see, for this to work I would have to define different caches (with accompanying boilerplate ehcache XML configuration and associated maintenance overhead) for Artists, AlbumLists, etc, etc, and remember to use the correct one with the correct method. Or as you said, I would have to explicitly specify the method name as a key wherever I use
My solution to this was to write a "MethodAwareCacheKeyGenerator" which implements KeyGenerator and is manually wired into a CacheInterceptor in my
Could you please reconsider changing this behaviour or at least explicitly documenting the default behaviour and how to make it cache on method name?
At the moment, the documentation says "Out of the box, the caching abstraction uses a simple hash-code based KeyGenerator that computes the key based on the hashes of all objects used for method invocation".
My assumption was that the method name was one of the "objects" used. It would help if the documentation stated that only the arguments are used by default, and not the method name.
Alexander Derenbach commented
I am glad that I am not the only one who miss understood it. (or expected something else).
this works fine.
But I agree with you that at least the documentation could perhaps mention the behavior a bit better, since they are talking about "method caching" which leads me to my expecting of the cache.
Mukarram Baig commented
Hello Costin and gang - Thanks for the great work thus far! I too tend to agree with Ned that the behavior is slightly counter-intuitive. I was trying to cache calls to a third party JAR via the cache:advice route and Ned's suggestion to have a MethodAwareCacheKeyGenerator helped me out. Can we provide such a key generator from Spring rather than having each user hand-roll their own? I am attaching an implementation that I am using.
Alex Wajda commented
Please do not close it as "Invalid" because it is pretty much valid.
I would vote for making the