Search in sources :

Example 1 with CacheUpdateAnnoConfig

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

the class CacheConfigUtil method parseCacheUpdate.

private static CacheUpdateAnnoConfig parseCacheUpdate(Method m) {
    CacheUpdate anno = m.getAnnotation(CacheUpdate.class);
    if (anno == null) {
        return null;
    }
    CacheUpdateAnnoConfig cc = new CacheUpdateAnnoConfig();
    cc.setArea(anno.area());
    cc.setName(anno.name());
    if (cc.getName() == null || cc.getName().trim().equals("")) {
        throw new CacheConfigException("name is required for @CacheUpdate: " + m.getClass().getName() + "." + m.getName());
    }
    cc.setKey(anno.key());
    cc.setValue(anno.value());
    if (cc.getValue() == null || cc.getValue().trim().equals("")) {
        throw new CacheConfigException("value is required for @CacheUpdate: " + m.getClass().getName() + "." + m.getName());
    }
    cc.setCondition(anno.condition());
    cc.setMulti(anno.multi());
    cc.setDefineMethod(m);
    return cc;
}
Also used : CacheConfigException(com.alicp.jetcache.CacheConfigException) CacheUpdateAnnoConfig(com.alicp.jetcache.anno.support.CacheUpdateAnnoConfig)

Example 2 with CacheUpdateAnnoConfig

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

the class CacheConfigUtil method parse.

public static boolean parse(CacheInvokeConfig cac, Method method) {
    boolean hasAnnotation = false;
    CachedAnnoConfig cachedConfig = parseCached(method);
    if (cachedConfig != null) {
        cac.setCachedAnnoConfig(cachedConfig);
        hasAnnotation = true;
    }
    boolean enable = parseEnableCache(method);
    if (enable) {
        cac.setEnableCacheContext(true);
        hasAnnotation = true;
    }
    List<CacheInvalidateAnnoConfig> invalidateAnnoConfigs = parseCacheInvalidates(method);
    if (invalidateAnnoConfigs != null) {
        cac.setInvalidateAnnoConfigs(invalidateAnnoConfigs);
        hasAnnotation = true;
    }
    CacheUpdateAnnoConfig updateAnnoConfig = parseCacheUpdate(method);
    if (updateAnnoConfig != null) {
        cac.setUpdateAnnoConfig(updateAnnoConfig);
        hasAnnotation = true;
    }
    if (cachedConfig != null && (invalidateAnnoConfigs != null || updateAnnoConfig != null)) {
        throw new CacheConfigException("@Cached can't coexists with @CacheInvalidate or @CacheUpdate: " + method);
    }
    return hasAnnotation;
}
Also used : CachedAnnoConfig(com.alicp.jetcache.anno.support.CachedAnnoConfig) CacheConfigException(com.alicp.jetcache.CacheConfigException) CacheInvalidateAnnoConfig(com.alicp.jetcache.anno.support.CacheInvalidateAnnoConfig) CacheUpdateAnnoConfig(com.alicp.jetcache.anno.support.CacheUpdateAnnoConfig)

Example 3 with CacheUpdateAnnoConfig

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

the class ExpressionUtilTest method testValue1.

@Test
public void testValue1() {
    cic.setCachedAnnoConfig(null);
    CacheUpdateAnnoConfig updateAnnoConfig = new CacheUpdateAnnoConfig();
    updateAnnoConfig.setDefineMethod(method);
    updateAnnoConfig.setValue("#p2");
    context.setArgs(new Object[] { "1234", 5678 });
    assertEquals(5678, ExpressionUtil.evalValue(context, updateAnnoConfig));
}
Also used : CacheUpdateAnnoConfig(com.alicp.jetcache.anno.support.CacheUpdateAnnoConfig) Test(org.junit.jupiter.api.Test)

Example 4 with CacheUpdateAnnoConfig

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

the class ExpressionUtilTest method testValue2.

@Test
public void testValue2() {
    cic.setCachedAnnoConfig(null);
    CacheUpdateAnnoConfig updateAnnoConfig = new CacheUpdateAnnoConfig();
    updateAnnoConfig.setDefineMethod(method);
    updateAnnoConfig.setValue("#p3");
    context.setArgs(new Object[] { "1234", 5678 });
    assertNull(ExpressionUtil.evalValue(context, updateAnnoConfig));
}
Also used : CacheUpdateAnnoConfig(com.alicp.jetcache.anno.support.CacheUpdateAnnoConfig) Test(org.junit.jupiter.api.Test)

Example 5 with CacheUpdateAnnoConfig

use of com.alicp.jetcache.anno.support.CacheUpdateAnnoConfig 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)

Aggregations

CacheUpdateAnnoConfig (com.alicp.jetcache.anno.support.CacheUpdateAnnoConfig)5 CacheConfigException (com.alicp.jetcache.CacheConfigException)2 Test (org.junit.jupiter.api.Test)2 CacheInvalidateAnnoConfig (com.alicp.jetcache.anno.support.CacheInvalidateAnnoConfig)1 CachedAnnoConfig (com.alicp.jetcache.anno.support.CachedAnnoConfig)1 ConfigMap (com.alicp.jetcache.anno.support.ConfigMap)1 ConfigProvider (com.alicp.jetcache.anno.support.ConfigProvider)1 GlobalCacheConfig (com.alicp.jetcache.anno.support.GlobalCacheConfig)1 CountClass (com.alicp.jetcache.testsupport.CountClass)1 Method (java.lang.reflect.Method)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1