-
Feb 13th, 2011, 04:48 AM
#1
Spring 3.1 Cache Abstraction: How do I switch the KeyGenerator?
Hello everybody,
I have a String as a key in my cache. Since the DefaultKeyGenerator uses hashCode() for generating the key I cannot use it, because there may be collisions.
It's no problem to provide another implementation of KeyGenerator, but I'm missing a simple way to use it. There is no such attribute on <cache:annotation-driven/> nor on CacheManager nor on Cache, just on the Aspect itself, but I don't wanna declare it myself.
Can somebody help me?
Tobias
-
Feb 15th, 2011, 02:07 AM
#2
Attribute key
Okay, still don't know how to switch the default KeyGenerator implementation, but I realized that I don't really need it when I use the key attribute on the @Cacheable annotation.
By the way, there is an error in the reference documentation. You need to reference the parameter with a hash: key="#parametername", otherwise Spel is trying to find a property parametername on some helper class.
-
Mar 1st, 2011, 12:31 PM
#3
Another problem with Default implementation, it's calculating hashCode of method param, but doesn't include methodName in calculations.
I remember reading documentation, hashCode is combination of methodName + params.
-
Jun 6th, 2011, 06:44 PM
#4
I've ran into the same problem of not having the method name used in the hashCode computation.
I can't figure out how to specify my own implementation of org.springframework.cache.KeyGenerator, the documentation mentions it should be possible.
-
Jun 22nd, 2011, 11:55 AM
#5
Have you figured it out? I ran into the same issue? There is setter method in CacheAspectSupport for injecting customKeygenerator but there is no way how to configure it.
Regards
Harshi
-
Oct 23rd, 2012, 04:06 PM
#6
Old thread, but here is how you do it:
<cache:annotation-driven key-generator="myKeyGenerator" />
<bean id="myKeyGenerator" class="com.whereever.spring.MyLessSuckyKeyGenerato r" />
As for the DefaultKeyGenerator, you can bypass it with:
@Cacheable(value="myCache",key="#root.methodName,# parameter,...")
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules