Search in sources :

Example 96 with Cache

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

the class AbstractJCacheAnnotationTests method earlyRemoveAllWithExceptionVetoRemove.

@Test
public void earlyRemoveAllWithExceptionVetoRemove() {
    Cache cache = getCache(DEFAULT_CACHE);
    Object key = createKey(this.keyItem);
    cache.put(key, new Object());
    assertThatNullPointerException().isThrownBy(() -> service.earlyRemoveAllWithException(false));
    // This will be remove anyway as the earlyRemove has removed the cache before
    assertThat(isEmpty(cache)).isTrue();
}
Also used : Cache(cn.taketoday.cache.Cache) Test(org.junit.jupiter.api.Test)

Example 97 with Cache

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

the class CacheTestUtils method assertCacheHit.

/**
 * Assert the following key has a matching value within the specified cache(s).
 */
public static void assertCacheHit(Object key, Object value, Cache... caches) {
    for (Cache cache : caches) {
        Cache.ValueWrapper wrapper = cache.get(key);
        assertThat(wrapper).as("An entry in " + cache + " should have been found with key " + key).isNotNull();
        assertThat(wrapper.get()).as("Wrong value in " + cache + " for entry with key " + key).isEqualTo(value);
    }
}
Also used : ConcurrentMapCache(cn.taketoday.cache.concurrent.ConcurrentMapCache) Cache(cn.taketoday.cache.Cache)

Example 98 with Cache

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

the class AbstractCacheAnnotationTests method testMultiEvict.

protected void testMultiEvict(CacheableService<?> service) {
    Object o1 = new Object();
    Object o2 = o1.toString() + "A";
    Object r1 = service.multiCache(o1);
    Object r2 = service.multiCache(o1);
    Cache primary = this.cm.getCache("primary");
    Cache secondary = this.cm.getCache("secondary");
    primary.put(o2, o2);
    assertThat(r2).isSameAs(r1);
    assertThat(primary.get(o1).get()).isSameAs(r1);
    assertThat(secondary.get(o1).get()).isSameAs(r1);
    service.multiEvict(o1);
    assertThat(primary.get(o1)).isNull();
    assertThat(secondary.get(o1)).isNull();
    assertThat(primary.get(o2)).isNull();
    Object r3 = service.multiCache(o1);
    Object r4 = service.multiCache(o1);
    assertThat(r3).isNotSameAs(r1);
    assertThat(r4).isSameAs(r3);
    assertThat(primary.get(o1).get()).isSameAs(r3);
    assertThat(secondary.get(o1).get()).isSameAs(r4);
}
Also used : Cache(cn.taketoday.cache.Cache)

Example 99 with Cache

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

the class AbstractCacheAnnotationTests method testMultiPut.

protected void testMultiPut(CacheableService<?> service) {
    Object o = 1;
    Cache primary = this.cm.getCache("primary");
    Cache secondary = this.cm.getCache("secondary");
    assertThat(primary.get(o)).isNull();
    assertThat(secondary.get(o)).isNull();
    Object r1 = service.multiUpdate(o);
    assertThat(primary.get(o).get()).isSameAs(r1);
    assertThat(secondary.get(o).get()).isSameAs(r1);
    o = 2;
    assertThat(primary.get(o)).isNull();
    assertThat(secondary.get(o)).isNull();
    Object r2 = service.multiUpdate(o);
    assertThat(primary.get(o).get()).isSameAs(r2);
    assertThat(secondary.get(o).get()).isSameAs(r2);
}
Also used : Cache(cn.taketoday.cache.Cache)

Example 100 with Cache

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

the class AbstractCacheAnnotationTests method testMultiCacheAndEvict.

protected void testMultiCacheAndEvict(CacheableService<?> service) {
    String methodName = "multiCacheAndEvict";
    Cache primary = this.cm.getCache("primary");
    Cache secondary = this.cm.getCache("secondary");
    Object key = 1;
    secondary.put(key, key);
    assertThat(secondary.get(methodName)).isNull();
    assertThat(secondary.get(key).get()).isSameAs(key);
    Object r1 = service.multiCacheAndEvict(key);
    assertThat(service.multiCacheAndEvict(key)).isSameAs(r1);
    // assert the method name is used
    assertThat(primary.get(methodName).get()).isSameAs(r1);
    assertThat(secondary.get(methodName)).isNull();
    assertThat(secondary.get(key)).isNull();
}
Also used : Cache(cn.taketoday.cache.Cache)

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