jetcache icon indicating copy to clipboard operation
jetcache copied to clipboard

如何通过配置文件动态启用禁用jetcache

Open shengming007 opened this issue 2 years ago • 11 comments

源码中位置 com.alicp.jetcache.anno.support.AbstractLifecycle

private boolean inited;

@PostConstruct
public final synchronized void init() {
    if (!inited) {
        doInit();
        inited = true;
    }
}

这个地方启用禁用jetcache 的代码是有用PostConstruct来实现了,目前没提供参数停止执行这个init方法,只能改源码实现吗?

shengming007 avatar May 13 '22 09:05 shengming007

目的是什么,spel里面可以引用spring bean的

bean('beanName').propertyName

areyouok avatar May 13 '22 12:05 areyouok

想要根据不同的配置环境变量 来动态启用和禁用jetcache,同一套代码在有些profile中想禁用掉jetcache

shengming007 avatar May 14 '22 03:05 shengming007

可以用bean做开关,可以动态更新。或者不想启用的话,EnableMethodCache注解不要扫对应的包就好了。

areyouok avatar May 16 '22 10:05 areyouok

我在公司框架中实现这个功能,修改的代码量有点大,如果从jetcache层面做会简单很多,关键点就是对两个@Enable注解的控制;作者可以考虑一下在配置文件中 增加一个 jetcache.enable=true 的全局控制开关,默认是开启的;

jeasonyoung avatar Jul 16 '22 15:07 jeasonyoung

GlobalCacheConfig有个enableMethodCache属性

areyouok avatar Jul 17 '22 03:07 areyouok

com.alicp.jetcache.autoconfigure.JetCacheAutoConfiguration.java

@Configuration
+@ConditionalOnProperty(prefix="jetcache", name="enable", havingValue = "true", matchIfMissing = true)
@ConditionalOnClass(GlobalCacheConfig.class)
@ConditionalOnMissingBean(GlobalCacheConfig.class)
@EnableConfigurationProperties(JetCacheProperties.class)

com.alicp.jetcache.anno.aop.JetCacheInterceptor.java

    @Override
    public Object invoke(final MethodInvocation invocation) throws Throwable {
        if (configProvider == null) {
+            if (applicationContext.getBeanProvider(SpringConfigProvider.class).getIfAvailable() == null) {
+               return invocation.proceed();
+           }
            configProvider = applicationContext.getBean(ConfigProvider.class);
        }
        if (configProvider != null && globalCacheConfig == null) {
            globalCacheConfig = configProvider.getGlobalCacheConfig();
        }
        if (globalCacheConfig == null || !globalCacheConfig.isEnableMethodCache()) {
            return invocation.proceed();
        }

这一段可能有点帮助,通过配置文件启用/禁用 cache组件

leechedan avatar Jul 18 '22 06:07 leechedan

GlobalCacheConfig有个enableMethodCache属性

楼主,这里能提个PR解决吗?如果系统内没有启用JetcacheAutoConfiguration的话,applicationContext.getBean(ConfigProvider.class)这个操作会报异常

leechedan avatar Jul 22 '22 06:07 leechedan

随时可以提,不过spring context里面是有这个bean的吧,否则初始化代码有问题?

看这里,不用yml做配置,是要声明一个SpringConfigProvider的:https://github.com/alibaba/jetcache/blob/master/docs/CN/GettingStarted.md

areyouok avatar Jul 22 '22 08:07 areyouok

随时可以提,不过spring context里面是有这个bean的吧,否则初始化代码有问题?

看这里,不用yml做配置,是要声明一个SpringConfigProvider的:https://github.com/alibaba/jetcache/blob/master/docs/CN/GettingStarted.md

. ConditionalOnProperty 注解在没有启用jetcache的情况下,是没有注入com.alicp.jetcache.anno.support.ConfigProvider的bean的, 所以在invoke方法中需要判断这个bean是否存在 . 不启用jetcache的时候尽量停用jetcacheAutoConfiguration的自动装配 . 这个配置启用禁用jetcache是会禁用掉整个jetcache的,包括非yml配置的缓存

如果楼主确定这个开关有需要的话,我就提PR

leechedan avatar Jul 26 '22 02:07 leechedan

我没有明白,你可以提了看看

areyouok avatar Jul 26 '22 08:07 areyouok

#691 PR for switch

leechedan avatar Jul 26 '22 10:07 leechedan