0

I'm using redisson with a jcache abstraction, simply put I have this:

public class MyService{

      @Cacheable("cacheA")
      public String returnSomethingAfterLongTime(String parameter){
               //...
      }

      @Cacheable("cacheA")
      public String returnSomethingElse(String parameter){

      }
}

Problem is that both of them create a redis key like "cacheA::parameter", in other words the class and method name are not taken into account.

This causes a problem if the string "parameter" is a common word because I have to be aware of every part of code where "cacheA" is used so to make sure that no inefficiency is brought up due to the fact that the "parameter" key could be replicated among calls.

Is there something that I'm doing wrong?

Phate
  • 6,066
  • 15
  • 73
  • 138

1 Answers1

3

It looks like you can specify a "key" attribute to customize it to cache based on method name.

Spring Cacheable key attribute

There are a lot of good examples and answers on this post.

I've never personally used Spring Cache, but it looks like you can specify @Cacheable("cacheA", key="#parameter") and the value of parameter will be used as the key rather than the word "parameter".

Seth Speaks
  • 153
  • 5
  • Mmm...I was hoping there existed something of more "sofisticated" than creating a keygenerator myself. Like for example something calculating hash of parameters and so on – Phate Mar 11 '20 at 19:06
  • I've updated my answer with a good reference post. You don't have to write your own keygen. You could use a uniquely identifying property in your class or something like that using the SpEL syntax. There are great examples in the link. – Seth Speaks Mar 11 '20 at 19:20