Results 1 to 6 of 6

Thread: Spring 3.1 Cache Abstraction: How do I switch the KeyGenerator?

  1. #1
    Join Date
    Nov 2009
    Posts
    5

    Default 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

  2. #2
    Join Date
    Nov 2009
    Posts
    5

    Default 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. #3
    Join Date
    Jan 2008
    Location
    NJ, USA
    Posts
    14

    Default

    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. #4
    Join Date
    Apr 2006
    Posts
    1

    Default

    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. #5
    Join Date
    Nov 2007
    Posts
    122

    Default

    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

  6. #6
    Join Date
    Feb 2012
    Posts
    7

    Default

    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
  •