syy
syy

Reputation: 807

Spring boot Caffeine cache not taking unique parameter

I have the following piece of code:

import org.springframework.cache.annotation.Cacheable;

@Cacheable(value = "myCache", key = "{'myUniqueKey-', #name}")
public String getCacheValue(final String name) {
  return "Hello " + name;
}
<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
    <version>3.1.6</version>
</dependency>

When I call getCacheValue from another class in the following order:

getCacheValue("Brad")
getCacheValue("Emily")

The first calls sets the cache and key of myUniqueKey-Brad and prints "Hello Brad". The second call also prints "Hello Brad" because it never goes inside the method again. I also tried setting the key this way but still the same issue.

key = "'myUniqueKey-' + #name"

Why does the second call not go inside the method? I would think since I have #name in the key argument and the name is different, it would print "Hello Emily". What am I doing wrong?

Upvotes: 0

Views: 97

Answers (0)

Related Questions