Search in sources :

Example 1 with Cache

use of cn.taketoday.cache.Cache in project today-infrastructure 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 2 with Cache

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

the class CacheTestUtils method createSimpleCacheManager.

/**
 * Create a {@link SimpleCacheManager} with the specified cache(s).
 *
 * @param cacheNames the names of the caches to create
 */
public static CacheManager createSimpleCacheManager(String... cacheNames) {
    SimpleCacheManager result = new SimpleCacheManager();
    List<Cache> caches = new ArrayList<>();
    for (String cacheName : cacheNames) {
        caches.add(new ConcurrentMapCache(cacheName));
    }
    result.setCaches(caches);
    result.afterPropertiesSet();
    return result;
}
Also used : ConcurrentMapCache(cn.taketoday.cache.concurrent.ConcurrentMapCache) ArrayList(java.util.ArrayList) SimpleCacheManager(cn.taketoday.cache.support.SimpleCacheManager) ConcurrentMapCache(cn.taketoday.cache.concurrent.ConcurrentMapCache) Cache(cn.taketoday.cache.Cache)

Example 3 with Cache

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

the class AbstractCacheAnnotationTests method testEvictAll.

protected void testEvictAll(CacheableService<?> service, boolean successExpected) {
    Cache cache = this.cm.getCache("testCache");
    Object o1 = new Object();
    Object o2 = new Object();
    cache.putIfAbsent(o1, -1L);
    cache.putIfAbsent(o2, -2L);
    Object r1 = service.cache(o1);
    Object r2 = service.cache(o2);
    assertThat(r2).isNotSameAs(r1);
    service.evictAll(new Object());
    if (successExpected) {
        assertThat(cache.get(o1)).isNull();
        assertThat(cache.get(o2)).isNull();
    } else {
        assertThat(cache.get(o1)).isNotNull();
        assertThat(cache.get(o2)).isNotNull();
    }
    Object r3 = service.cache(o1);
    Object r4 = service.cache(o2);
    if (successExpected) {
        assertThat(r3).isNotSameAs(r1);
        assertThat(r4).isNotSameAs(r2);
    } else {
        assertThat(r3).isSameAs(r1);
        assertThat(r4).isSameAs(r2);
    }
}
Also used : Cache(cn.taketoday.cache.Cache)

Example 4 with Cache

use of cn.taketoday.cache.Cache in project today-infrastructure 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 5 with Cache

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

the class AbstractCacheAnnotationTests method testCustomCacheManager.

@Test
public void testCustomCacheManager() {
    CacheManager customCm = this.ctx.getBean("customCacheManager", CacheManager.class);
    Object key = new Object();
    Object r1 = this.cs.customCacheManager(key);
    assertThat(this.cs.customCacheManager(key)).isSameAs(r1);
    Cache cache = customCm.getCache("testCache");
    assertThat(cache.get(key)).isNotNull();
}
Also used : CacheManager(cn.taketoday.cache.CacheManager) 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