Search in sources :

Example 1 with ConfigProvider

use of com.alicp.jetcache.anno.support.ConfigProvider in project jetcache by alibaba.

the class TestUtil method createConfigProvider.

public static ConfigProvider createConfigProvider() {
    ConfigProvider configProvider = new ConfigProvider();
    configProvider.setGlobalCacheConfig(createGloableConfig());
    return configProvider;
}
Also used : SpringConfigProvider(com.alicp.jetcache.anno.support.SpringConfigProvider) ConfigProvider(com.alicp.jetcache.anno.support.ConfigProvider)

Example 2 with ConfigProvider

use of com.alicp.jetcache.anno.support.ConfigProvider in project jetcache by alibaba.

the class CacheHandlerInvalidateTest method setup.

@BeforeEach
public void setup() throws Exception {
    GlobalCacheConfig globalCacheConfig = new GlobalCacheConfig();
    configProvider = new ConfigProvider();
    configProvider.setGlobalCacheConfig(globalCacheConfig);
    configProvider.init();
    cache = LinkedHashMapCacheBuilder.createLinkedHashMapCacheBuilder().keyConvertor(FastjsonKeyConvertor.INSTANCE).buildCache();
    cacheInvokeConfig = new CacheInvokeConfig();
    configMap = new ConfigMap();
    count = new CountClass();
    method = CountClass.class.getMethod("update", String.class, int.class);
    cacheInvokeContext = configProvider.getCacheContext().createCacheInvokeContext(configMap);
    cacheInvokeContext.setCacheInvokeConfig(cacheInvokeConfig);
    invalidateAnnoConfig = new CacheInvalidateAnnoConfig();
    invalidateAnnoConfig.setDefineMethod(method);
    invalidateAnnoConfig.setCondition(CacheConsts.UNDEFINED_STRING);
    cacheInvokeConfig.setInvalidateAnnoConfigs(Collections.singletonList(invalidateAnnoConfig));
    invalidateAnnoConfig.setKey("args[0]");
    cacheInvokeConfig.setCachedAnnoConfig(null);
    cacheInvokeContext.setMethod(method);
    cacheInvokeContext.setArgs(new Object[] { "KEY", 1000 });
    cacheInvokeContext.setInvoker(() -> method.invoke(count, cacheInvokeContext.getArgs()));
    cacheInvokeContext.setCacheFunction((a, b) -> cache);
}
Also used : GlobalCacheConfig(com.alicp.jetcache.anno.support.GlobalCacheConfig) CacheInvalidateAnnoConfig(com.alicp.jetcache.anno.support.CacheInvalidateAnnoConfig) ConfigMap(com.alicp.jetcache.anno.support.ConfigMap) ConfigProvider(com.alicp.jetcache.anno.support.ConfigProvider) CountClass(com.alicp.jetcache.testsupport.CountClass) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with ConfigProvider

use of com.alicp.jetcache.anno.support.ConfigProvider in project jetcache by alibaba.

the class CacheHandlerUpdateTest method setup.

@BeforeEach
public void setup() throws Exception {
    GlobalCacheConfig globalCacheConfig = new GlobalCacheConfig();
    configProvider = new ConfigProvider();
    configProvider.setGlobalCacheConfig(globalCacheConfig);
    configProvider.init();
    cache = LinkedHashMapCacheBuilder.createLinkedHashMapCacheBuilder().keyConvertor(FastjsonKeyConvertor.INSTANCE).buildCache();
    cacheInvokeConfig = new CacheInvokeConfig();
    configMap = new ConfigMap();
    count = new CountClass();
    Method method = CountClass.class.getMethod("update", String.class, int.class);
    cacheInvokeContext = configProvider.getCacheContext().createCacheInvokeContext(configMap);
    cacheInvokeContext.setCacheInvokeConfig(cacheInvokeConfig);
    updateAnnoConfig = new CacheUpdateAnnoConfig();
    updateAnnoConfig.setCondition(CacheConsts.UNDEFINED_STRING);
    updateAnnoConfig.setDefineMethod(method);
    cacheInvokeConfig.setUpdateAnnoConfig(updateAnnoConfig);
    updateAnnoConfig.setKey("args[0]");
    updateAnnoConfig.setValue("args[1]");
    cacheInvokeContext.setMethod(method);
    cacheInvokeContext.setArgs(new Object[] { "K1", 1000 });
    cacheInvokeContext.setInvoker(() -> cacheInvokeContext.getMethod().invoke(count, cacheInvokeContext.getArgs()));
    cacheInvokeContext.setCacheFunction((a, b) -> cache);
}
Also used : GlobalCacheConfig(com.alicp.jetcache.anno.support.GlobalCacheConfig) ConfigMap(com.alicp.jetcache.anno.support.ConfigMap) ConfigProvider(com.alicp.jetcache.anno.support.ConfigProvider) CacheUpdateAnnoConfig(com.alicp.jetcache.anno.support.CacheUpdateAnnoConfig) CountClass(com.alicp.jetcache.testsupport.CountClass) Method(java.lang.reflect.Method) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with ConfigProvider

use of com.alicp.jetcache.anno.support.ConfigProvider in project jetcache by alibaba.

the class JetCacheInterceptor method invoke.

@Override
public Object invoke(final MethodInvocation invocation) throws Throwable {
    if (configProvider == null) {
        configProvider = applicationContext.getBean(ConfigProvider.class);
    }
    if (configProvider != null && globalCacheConfig == null) {
        globalCacheConfig = configProvider.getGlobalCacheConfig();
    }
    if (globalCacheConfig == null || !globalCacheConfig.isEnableMethodCache()) {
        return invocation.proceed();
    }
    Method method = invocation.getMethod();
    Object obj = invocation.getThis();
    CacheInvokeConfig cac = null;
    if (obj != null) {
        String key = CachePointcut.getKey(method, obj.getClass());
        cac = cacheConfigMap.getByMethodInfo(key);
    }
    if (cac == null || cac == CacheInvokeConfig.getNoCacheInvokeConfigInstance()) {
        return invocation.proceed();
    }
    CacheInvokeContext context = configProvider.getCacheContext().createCacheInvokeContext(cacheConfigMap);
    context.setTargetObject(invocation.getThis());
    context.setInvoker(invocation::proceed);
    context.setMethod(method);
    context.setArgs(invocation.getArguments());
    context.setCacheInvokeConfig(cac);
    context.setHiddenPackages(globalCacheConfig.getHiddenPackages());
    return CacheHandler.invoke(context);
}
Also used : ConfigProvider(com.alicp.jetcache.anno.support.ConfigProvider) CacheInvokeConfig(com.alicp.jetcache.anno.method.CacheInvokeConfig) CacheInvokeContext(com.alicp.jetcache.anno.method.CacheInvokeContext) Method(java.lang.reflect.Method)

Aggregations

ConfigProvider (com.alicp.jetcache.anno.support.ConfigProvider)4 ConfigMap (com.alicp.jetcache.anno.support.ConfigMap)2 GlobalCacheConfig (com.alicp.jetcache.anno.support.GlobalCacheConfig)2 CountClass (com.alicp.jetcache.testsupport.CountClass)2 Method (java.lang.reflect.Method)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 CacheInvokeConfig (com.alicp.jetcache.anno.method.CacheInvokeConfig)1 CacheInvokeContext (com.alicp.jetcache.anno.method.CacheInvokeContext)1 CacheInvalidateAnnoConfig (com.alicp.jetcache.anno.support.CacheInvalidateAnnoConfig)1 CacheUpdateAnnoConfig (com.alicp.jetcache.anno.support.CacheUpdateAnnoConfig)1 SpringConfigProvider (com.alicp.jetcache.anno.support.SpringConfigProvider)1