Announcement Announcement Module
No announcement yet.
Spring 3.1 Cache Abstraction: How do I switch the KeyGenerator? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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?


  • #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.


    • #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.


      • #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.


        • #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.



          • #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,...")