I'm using Spring Caching framework along with redis. Following is the Cacheable I use
@Cacheable(value = "oauth2token", key="#value + #type")
public OAuth2Token findOneByValueAndType(String value, String type);
I was just trying to create a key that I know. This gives error
14:20:21,199 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/veip-web].[Resteasy]] (http-/0.0.0.0:8080-63) JBWEB000236: Servlet.service() for servlet Resteasy threw exception: org.springframework.expression.spel.SpelEvaluationException: EL1030E:(pos 0): The operator 'ADD' is not supported between objects of type 'null' and 'null'
Because when I don't specify key as fllows.
@Cacheable(value = "oauth2token")
public OAuth2Token findOneByValueAndType(String value, String type);
I can see a crazy key generated. I need to know the key because later I need to CachePut
and update the same item.
I tried following following as well.
@Cacheable(value = "oauth2token", key="#type.contact(#value)")
public OAuth2Token findOneByValueAndType(String value, String type);
Spring complains that #type
is null that time.
What is wrong here.
You probably don't have the runtime information of each parameter name (#value is not know). A safer would be to use the #p
or #a
alias that will always work. key="#p0 + #p1"
should do the trick.
Having said that, I wouldn't do that. If you are reusing the key in another annotated method, I would implement KeyGenerator
as a bean and pass the reference of that bean in the methods that need to work on those two values. That way you can share (and test) that code and avoid the duplication.