Search in sources :

Example 1 with CacheInvalidateAnnoConfig

use of com.alicp.jetcache.anno.support.CacheInvalidateAnnoConfig 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 2 with CacheInvalidateAnnoConfig

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

the class CacheHandlerInvalidateTest method testInvalidates.

@Test
public void testInvalidates() throws Throwable {
    CacheInvalidateAnnoConfig anno1 = new CacheInvalidateAnnoConfig();
    anno1.setDefineMethod(method);
    anno1.setCondition(CacheConsts.UNDEFINED_STRING);
    anno1.setKey("args[0] + 1");
    CacheInvalidateAnnoConfig anno2 = new CacheInvalidateAnnoConfig();
    anno2.setDefineMethod(method);
    anno2.setCondition(CacheConsts.UNDEFINED_STRING);
    anno2.setKey("args[0] + 2");
    cacheInvokeConfig.setInvalidateAnnoConfigs(Arrays.asList(anno1, anno2));
    cache.put("KEY1", "V");
    cache.put("KEY2", "V");
    CacheHandler.invoke(cacheInvokeContext);
    assertNull(cache.get("KEY1"));
    assertNull(cache.get("KEY2"));
}
Also used : CacheInvalidateAnnoConfig(com.alicp.jetcache.anno.support.CacheInvalidateAnnoConfig) Test(org.junit.jupiter.api.Test)

Example 3 with CacheInvalidateAnnoConfig

use of com.alicp.jetcache.anno.support.CacheInvalidateAnnoConfig 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 4 with CacheInvalidateAnnoConfig

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

the class CacheConfigUtil method createCacheInvalidateAnnoConfig.

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

Aggregations

CacheInvalidateAnnoConfig (com.alicp.jetcache.anno.support.CacheInvalidateAnnoConfig)4 CacheConfigException (com.alicp.jetcache.CacheConfigException)2 CacheUpdateAnnoConfig (com.alicp.jetcache.anno.support.CacheUpdateAnnoConfig)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 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1