Search in sources :

Example 6 with Cache

use of cn.taketoday.cache.Cache in project today-infrastructure by TAKETODAY.

the class AbstractCacheAnnotationTests method testMethodName.

protected void testMethodName(CacheableService<?> service, String keyName) {
    Object key = new Object();
    Object r1 = service.name(key);
    assertThat(service.name(key)).isSameAs(r1);
    Cache cache = this.cm.getCache("testCache");
    // assert the method name is used
    assertThat(cache.get(keyName)).isNotNull();
}
Also used : Cache(cn.taketoday.cache.Cache)

Example 7 with Cache

use of cn.taketoday.cache.Cache in project today-infrastructure by TAKETODAY.

the class AbstractCacheAnnotationTests method testMultiConditionalCacheAndEvict.

protected void testMultiConditionalCacheAndEvict(CacheableService<?> service) {
    Cache primary = this.cm.getCache("primary");
    Cache secondary = this.cm.getCache("secondary");
    Object key = 1;
    secondary.put(key, key);
    assertThat(primary.get(key)).isNull();
    assertThat(secondary.get(key).get()).isSameAs(key);
    Object r1 = service.multiConditionalCacheAndEvict(key);
    Object r3 = service.multiConditionalCacheAndEvict(key);
    assertThat(!r1.equals(r3)).isTrue();
    assertThat(primary.get(key)).isNull();
    Object key2 = 3;
    Object r2 = service.multiConditionalCacheAndEvict(key2);
    assertThat(service.multiConditionalCacheAndEvict(key2)).isSameAs(r2);
    // assert the method name is used
    assertThat(primary.get(key2).get()).isSameAs(r2);
    assertThat(secondary.get(key2)).isNull();
}
Also used : Cache(cn.taketoday.cache.Cache)

Example 8 with Cache

use of cn.taketoday.cache.Cache in project today-infrastructure by TAKETODAY.

the class AbstractCacheAnnotationTests method testUnlessExpression.

protected void testUnlessExpression(CacheableService<?> service) {
    Cache cache = this.cm.getCache("testCache");
    cache.clear();
    service.unless(10);
    service.unless(11);
    assertThat(cache.get(10).get()).isEqualTo(10L);
    assertThat(cache.get(11)).isNull();
}
Also used : Cache(cn.taketoday.cache.Cache)

Example 9 with Cache

use of cn.taketoday.cache.Cache in project today-infrastructure by TAKETODAY.

the class AbstractJCacheAnnotationTests method putWithExceptionVetoPut.

@Test
public void putWithExceptionVetoPut() {
    Cache cache = getCache(DEFAULT_CACHE);
    Object key = createKey(this.keyItem);
    Object value = new Object();
    assertThat(cache.get(key)).isNull();
    assertThatNullPointerException().isThrownBy(() -> service.putWithException(this.keyItem, value, false));
    assertThat(cache.get(key)).isNull();
}
Also used : Cache(cn.taketoday.cache.Cache) Test(org.junit.jupiter.api.Test)

Example 10 with Cache

use of cn.taketoday.cache.Cache in project today-infrastructure by TAKETODAY.

the class AbstractJCacheAnnotationTests method earlyPutWithException.

@Test
public void earlyPutWithException() {
    Cache cache = getCache(DEFAULT_CACHE);
    Object key = createKey(this.keyItem);
    Object value = new Object();
    assertThat(cache.get(key)).isNull();
    assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> service.earlyPutWithException(this.keyItem, value, true));
    Cache.ValueWrapper result = cache.get(key);
    assertThat(result).isNotNull();
    assertThat(result.get()).isEqualTo(value);
}
Also used : Cache(cn.taketoday.cache.Cache) Test(org.junit.jupiter.api.Test)

Aggregations

Cache (cn.taketoday.cache.Cache)316 Test (org.junit.jupiter.api.Test)180 ConcurrentMapCache (cn.taketoday.cache.concurrent.ConcurrentMapCache)38 CaffeineCache (cn.taketoday.cache.support.CaffeineCache)20 CaffeineCacheManager (cn.taketoday.cache.support.CaffeineCacheManager)17 CacheManager (cn.taketoday.cache.CacheManager)12 Nullable (cn.taketoday.lang.Nullable)8 ArrayList (java.util.ArrayList)8 NoSuchBeanDefinitionException (cn.taketoday.beans.factory.NoSuchBeanDefinitionException)6 SimpleCacheManager (cn.taketoday.cache.support.SimpleCacheManager)6 AnnotationConfigApplicationContext (cn.taketoday.context.annotation.AnnotationConfigApplicationContext)6 Assertions.assertThatIOException (org.assertj.core.api.Assertions.assertThatIOException)6 CacheConfiguration (cn.taketoday.cache.annotation.CacheConfiguration)5 Method (java.lang.reflect.Method)5 LinkedHashSet (java.util.LinkedHashSet)4 StandardApplicationContext (cn.taketoday.context.support.StandardApplicationContext)3 Date (java.util.Date)3 User (test.demo.config.User)3 ValueWrapper (cn.taketoday.cache.Cache.ValueWrapper)2 NoSuchCacheException (cn.taketoday.cache.NoSuchCacheException)2