With a restful api call such as
myserver/api/mydata?req?%7Bcode%3A%2244%22%2C%20name%3A%22sally%22%2C%20version%3A7%7D
where the parameters passed in are code=44, name=sally, and version=7, and I want to cache the response using only name and version as keys -- but not code.
What I want to do is this:
@Cacheable(value="myCache", key="#name.concat(#version)")
def mydata() {
.....
}
But #name and #version both resolve to null. Apparently that format only works for method parameters, not params.name or params.version.
If I leave the key as default, the cache uses all three params as the key (if I change one, the cache is not used). That would be great except I want the cache to be used regardless of 'code' and based on 'name' and 'version' as keys.
My guess is that I will need to override the default KeyGenerator, but I have tried and failed. Any advice on how to do this?
Or is there a way to access the params properties with the @Cacheable annotation?
Phil, The simplest solution would be to define the name and version as parameters of the controller action:
@Cacheable(value="myCache", key="#name.concat(#version)")
def mydata(String name, String version) {
}
If the name and version parameters are not always provided simply add a default value in the method signature:
@Cacheable(value="myCache", key="#name.concat(#version)")
def mydata(String name = null, String version = 0) {
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With