0

Spring caching uses key parameter as the cache key or the parameter passed to the function if key is not present. What will be the key if both the key and function parameter are missing?

Consider the folllowing case where neither key parameter nor function parameter exists. How spring caching work in such a case?

@Cacheable(value="cacheTest")
public string sayHello(){
    return "hello"
}
Yashasvi Raj Pant
  • 1,274
  • 4
  • 13
  • 33
  • You will have problems in a similar use, except that there is no problem. https://stackoverflow.com/questions/48888760/cachable-on-methods-without-input-parameters – İsmail Y. Jan 31 '21 at 16:13

1 Answers1

1

See the current Spring Cache Abstraction documentation on the subject of cache keys, here.

In a nutshell...

"If no params are given, return SimpleKey.EMPTY."

In your case, Spring will create an "empty" SimpleKey (i.e. SimpleKey.EMPTY) as the cache key for the result ("hello") of your sayHello() @Cacheable service method.

By way of example, see this test case.

John Blum
  • 7,381
  • 1
  • 20
  • 30