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;
}
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"));
}
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);
}
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;
}
Aggregations