I am looking to use a custom key generator for storing key-value pairs in Redis.
I am placing the key generator in a class that extends ‘CachingConfigurerSupport’. The key generator in it’s basic form looks like this:
@Bean
@Override
public KeyGenerator keyGenerator() {
return (target, method, params) -> {
StringBuilder sb = new StringBuilder();
int index = 0;
while (index < params.length) {
sb.append(params[index].toString());
if (index < params.length - 1)
sb.append("_");
index++;
}
//TODO- append desired request header values here
return sb.toString();
};
}
A sample controller method where this is invoked from is like this:
@Cacheable("StarWarsCharacters")
@RequestMapping(value = "/starwarscharacters")
public List<String> getAllStarWarsCharacters() {
System.out.println("execute getAllStarWarsCharacters!");
List<String> starWarsChars = new ArrayList<>();
starWarsChars.add("Yoda");
starWarsChars.add("Skywalker");
starWarsChars.add("Han Solo");
starWarsChars.add("R2-D2");
return starWarsChars;
}
I am interested in getting to the RequestHeader as it contains common attributes across all service endpoints and the values of those matter to keys generated. Any suggestions on how to get that?
One possible implementation is to pass in @RequestHeader as a param and use that. But that would require @RequestHeader attribute for all invocations of @Cacheable annotated controller methods which I am trying to avoid by having one common implementation of CacheConfigurerSupport::keyGenerator() that all invocations can utilize.
Any pointers will be greatly appreciated!
Aucun commentaire:
Enregistrer un commentaire