spring cache key generator为什么不使用spel键?

时间:2017-09-13 09:53:42

标签: spring spring-boot spring-cache

keyGenerator无法处理spel键,我可以为所有键自定义keyGenerator包含@Cacheable(key="#spelKey")

我找到了代码:org.springframework.cache.interceptor.CacheAspectSupport

    protected Object generateKey(Object result) {
        if (StringUtils.hasText(this.metadata.operation.getKey())) {
            EvaluationContext evaluationContext = this.createEvaluationContext(result);
            return CacheAspectSupport.this.evaluator.key(this.metadata.operation.getKey(), this.methodCacheKey, evaluationContext);
        } else {
            return this.metadata.keyGenerator.generate(this.target, this.metadata.method, this.args);
        }
    }

更改代码如:

    protected Object generateKey(Object result) {
        if (StringUtils.hasText(this.metadata.operation.getKey())) {
            EvaluationContext evaluationContext = createEvaluationContext(result);
            return this.metadata.keyGenerator.generate(this.target, this.metadata.method, evaluator.key(this.metadata.operation.getKey(), this.methodCacheKey, evaluationContext));
        }
        return this.metadata.keyGenerator.generate(this.target, this.metadata.method, this.args);
    }

1 个答案:

答案 0 :(得分:0)

我不确定你要对#spelKey做什么,但它已经相当记录了。如果您想要做一些复杂的事情,我会改为KeyGenerator,请参阅the documentation