Caffeine cache does not work with String parameter as a key

86 Views Asked by At

I have a service class called TeamService annotated as @Service.

@Cacheable(value = CacheConfig.TEAM, key = "#root.methodName.concat(#nationality == null ? '' : #nationality )")
    public List<Team> getTeamList(String nationality ) {
       return teamFinder.getTeams().stream()
             .filter(team -> team.getNationality().equals(nationality))
             .toList();
}

Team finder is an external service. It finds all teams. Cachable works correctly if nationality parameter passed as null, but if I pass a value like ENG, then the cache does not work. Each time I send a request to this method, even if my nationality parameter is the same, the method starts, calls the teamFinder service then filter the results rather than return the result from cache. I have tried to use only nationality parameter as the key, it still did not work correctly. What is wrong for my code?

Could you tell me how can I use both method name and the nationality parameter as key value of the cache?

  • My caffeine version is 3.1.6
  • java version is 17
  • spring-boot-starter and spring-boot-starter-cache 3.1.2
0

There are 0 best solutions below