Search code examples
springcachingkeyspring-cache

Spring Caching. what is the cache key if there is no key as well as parameter


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"
}

Solution

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