gpt4 book ai didi

java - 如何注册一个自定义的CacheInterceptor而不是Spring中封装的CacheInterceptor?

转载 作者:太空宇宙 更新时间:2023-11-04 07:07:10 37 4
gpt4 key购买 nike

我们正在开发一个 Multi-Tenancy 应用程序,我们需要带有自定义键的@Cacheable考虑一个附加参数,即tenantId。 Spring Cache 允许自定义 keg 生成器,实现接口(interface) org.springframework.cache.interceptor.KeyGenerator

但是,当 @Cacheable 注解定义有 key 时,缓存框架不会使用该生成器(这表明 Spring Cache 不应使用 KeyGenerator,至少在 Spring 3.2 中)。这没有考虑 Multi-Tenancy 应用程序的用例,其中所有 key 都需要考虑tenantId以避免租户之间的 key 冲突。

此问题的解决方案是用在任何情况下都使用 KeyGenerator 的自定义版本替换 Spring Cache 附带的 CacheInterceptor。

代码的关键部分在CacheInterceptor的父类(super class)CacheAspectSupport中:

/**
* Computes the key for the given caching operation.
* @return generated key (null if none can be generated)
*/
protected Object generateKey() {
if (StringUtils.hasText(this.operation.getKey())) {
return evaluator.key(this.operation.getKey(), this.method, this.evalContext);
}
return keyGenerator.generate(this.target, this.method, this.args);
}

我们处理一种将该代码替换为版本的方法,该版本考虑独立于值的生成

this.operation.getKey() 

通过子类化 CacheInterceptor 并重写一些东西。但是,没有简单的方法可以替换 Spring Cache 配置中的拦截器。我们使用 xml 配置

<cache:annotation-driven /> 

标签

same question前段时间在Spring论坛上出现过,但目前还没有答案

最佳答案

我们在 this other stackoverflow question 中找到了答案这是为了展示如何替换 KeyGenerator。关键是注释掉

<cache:annotation-driven /> 

来自xml,并手动配置注释的拦截器。它就像一个魅力!

关于java - 如何注册一个自定义的CacheInterceptor而不是Spring中封装的CacheInterceptor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21186854/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com