Spring Caching密钥生成器

use*_*481 2 spring caching spring-cache

我刚刚开始研究Spring缓存.

我的服务方法是......

@Override
@Cacheable(value="profile", **key**="personId" )
public String cpuService2(Long personId, String personAddress){
    return "CachedMessage";
}
Run Code Online (Sandbox Code Playgroud)

没有key子句,不抛出异常并假设两个参数都自动生成缓存但是使用键,当我调用此方法时,抛出异常为...

Exception in thread "main" org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'personId' cannot be found on object of type 'org.springframework.cache.interceptor.CacheExpressionRootObject'
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:246)
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:112)
        at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:107)
        at org.springframework.expression.spel.ast.OpGT.getValueInternal(OpGT.java:37)
        at org.springframework.expression.spel.ast.OpGT.getValueInternal(OpGT.java:29)
        at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:102)
        at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:98)
        at org.springframework.cache.interceptor.ExpressionEvaluator.condition(ExpressionEvaluator.java:99)
        at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.isConditionPassing(CacheAspectSupport.java:462)
        at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.isConditionPassing(CacheAspectSupport.java:456)
        at org.springframework.cache.interceptor.CacheAspectSupport.inspectCacheables(CacheAspectSupport.java:292)
        at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:199)
        at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
        at $Proxy5.cpuService3(Unknown Source)
        at pack100_cache.pack020CacheKey.TestSimpleBean.main(TestSimpleBean.java:34)
Run Code Online (Sandbox Code Playgroud)

搜索文档但没有任何线索.希望有人能解决这个问题.

Ste*_*oll 8

你错过了前面的# personId

@Override
@Cacheable(value="profile", key="#personId" )
public String cpuService2(Long personId, String personAddress){
    return "CachedMessage";
}
Run Code Online (Sandbox Code Playgroud)

缓存抽象章有你想要做什么的例子不胜枚举.


Mar*_*ged 5

当事情变得更加复杂时,在容易理解用于生成密钥的算法时,SpEL可能不是理想的解决方案。您可能考虑使用Java生成密钥:

@Component
public class FooGenerator implements KeyGenerator {
    @Override
    public Object generate(Object target, Method method, Object... params) {
        String param = "";

        if (params.length == 2) {
            if (params[0] instanceof String) {
                param = (String) params[0];

        String key = "FooKeyEquatesTo" + param;
        return key;
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以在需要的地方插入此密钥生成器:

@Cacheable(value = "nameofmycachearea", keyGenerator="fooGenerator")
Run Code Online (Sandbox Code Playgroud)