Reputation: 41
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?
Upvotes: 1
Views: 406