Search in sources :

Example 1 with CountClass

use of com.alicp.jetcache.testsupport.CountClass in project jetcache by alibaba.

the class CacheHandlerTest method testStaticInvokeUnlessAndNull.

@Test
public void testStaticInvokeUnlessAndNull() throws Throwable {
    Method method = CountClass.class.getMethod("countNull");
    cacheAnnoConfig.setCacheNullValue(false);
    cacheAnnoConfig.setUnless("mvel{result==0}");
    cacheInvokeConfig.init();
    //null, not cached
    Assert.assertNull(invoke(method, null));
    //0, not cached
    Assert.assertEquals(0, invoke(method, null).longValue());
    //1, cache
    Assert.assertEquals(1, invoke(method, null).longValue());
    //cache hit
    Assert.assertEquals(1, invoke(method, null).longValue());
    cacheAnnoConfig.setUnless("mvel{result==1}");
    cacheInvokeConfig.init();
    Assert.assertEquals(2, invoke(method, null).longValue());
    Assert.assertEquals(2, invoke(method, null).longValue());
    count = new CountClass();
    cacheAnnoConfig.setUnless("mvel{result==2}");
    cacheInvokeConfig.init();
    Assert.assertNull(invoke(method, null));
    //0, cached
    Assert.assertEquals(0, invoke(method, null).longValue());
    //cache hit
    Assert.assertEquals(0, invoke(method, null).longValue());
    cacheAnnoConfig.setCacheNullValue(true);
    count = new CountClass();
    cacheAnnoConfig.setUnless("mvel{result==0}");
    cacheInvokeConfig.init();
    Assert.assertNull(invoke(method, null));
    Assert.assertNull(invoke(method, null));
}
Also used : CountClass(com.alicp.jetcache.testsupport.CountClass) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 2 with CountClass

use of com.alicp.jetcache.testsupport.CountClass in project jetcache by alibaba.

the class ProxyUtilTest method testGetProxy.

@Test
public void testGetProxy() {
    CacheAnnoConfig cac = new CacheAnnoConfig();
    cac.setArea(CacheConsts.DEFAULT_AREA);
    cac.setCacheType(CacheType.REMOTE);
    cac.setCondition(CacheConsts.UNDEFINED_STRING);
    cac.setUnless(CacheConsts.UNDEFINED_STRING);
    cac.setEnabled(true);
    cac.setTimeUnit(TimeUnit.SECONDS);
    cac.setExpire(100);
    cac.setName(CacheConsts.UNDEFINED_STRING);
    cac.setSerialPolicy(CacheConsts.DEFAULT_SERIAL_POLICY);
    cac.setKeyConvertor(KeyConvertor.FASTJSON);
    Count c1 = new CountClass();
    Count c2 = ProxyUtil.getProxy(c1, cac, globalCacheConfig);
    Assert.assertNotEquals(c1.count(), c1.count());
    Assert.assertEquals(c2.count(), c2.count());
    Assert.assertEquals(c2.count(100), c2.count(100));
    Assert.assertEquals(c2.count("S", 100), c2.count("S", 100));
    Assert.assertEquals(c2.count(new DynamicQuery(), 100), c2.count(new DynamicQuery(), 100));
    Assert.assertNotEquals(c2.count(200), c2.count(100));
}
Also used : DynamicQuery(com.alicp.jetcache.test.support.DynamicQuery) CountClass(com.alicp.jetcache.testsupport.CountClass) Count(com.alicp.jetcache.testsupport.Count) CacheAnnoConfig(com.alicp.jetcache.anno.support.CacheAnnoConfig) Test(org.junit.Test)

Example 3 with CountClass

use of com.alicp.jetcache.testsupport.CountClass in project jetcache by alibaba.

the class CacheHandlerTest method setup.

@BeforeEach
public void setup() {
    GlobalCacheConfig globalCacheConfig = new GlobalCacheConfig();
    configProvider = new ConfigProvider();
    configProvider.setGlobalCacheConfig(globalCacheConfig);
    configProvider.init();
    cache = LinkedHashMapCacheBuilder.createLinkedHashMapCacheBuilder().keyConvertor(FastjsonKeyConvertor.INSTANCE).buildCache();
    cachedAnnoConfig = new CachedAnnoConfig();
    cachedAnnoConfig.setArea(CacheConsts.DEFAULT_AREA);
    cachedAnnoConfig.setName("myCacheName");
    cachedAnnoConfig.setEnabled(CacheConsts.DEFAULT_ENABLED);
    cachedAnnoConfig.setTimeUnit(TimeUnit.SECONDS);
    cachedAnnoConfig.setExpire(100);
    cachedAnnoConfig.setCacheType(CacheType.REMOTE);
    cachedAnnoConfig.setLocalLimit(CacheConsts.DEFAULT_LOCAL_LIMIT);
    cachedAnnoConfig.setCacheNullValue(CacheConsts.DEFAULT_CACHE_NULL_VALUE);
    cachedAnnoConfig.setCondition(CacheConsts.UNDEFINED_STRING);
    cachedAnnoConfig.setPostCondition(CacheConsts.UNDEFINED_STRING);
    cachedAnnoConfig.setSerialPolicy(CacheConsts.DEFAULT_SERIAL_POLICY);
    cachedAnnoConfig.setKeyConvertor(KeyConvertor.FASTJSON);
    cachedAnnoConfig.setKey(CacheConsts.UNDEFINED_STRING);
    cacheInvokeConfig = new CacheInvokeConfig();
    cacheInvokeConfig.setCachedAnnoConfig(cachedAnnoConfig);
    configMap = new ConfigMap();
    count = new CountClass();
}
Also used : CountClass(com.alicp.jetcache.testsupport.CountClass) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with CountClass

use of com.alicp.jetcache.testsupport.CountClass 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 5 with CountClass

use of com.alicp.jetcache.testsupport.CountClass 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

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