Search in sources :

Example 11 with CacheManager

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

the class ConcurrentMapCacheManagerTests method testDynamicMode.

@Test
public void testDynamicMode() {
    CacheManager cm = new ConcurrentMapCacheManager();
    Cache cache1 = cm.getCache("c1");
    assertThat(cache1 instanceof ConcurrentMapCache).isTrue();
    Cache cache1again = cm.getCache("c1");
    assertThat(cache1).isSameAs(cache1again);
    Cache cache2 = cm.getCache("c2");
    assertThat(cache2 instanceof ConcurrentMapCache).isTrue();
    Cache cache2again = cm.getCache("c2");
    assertThat(cache2).isSameAs(cache2again);
    Cache cache3 = cm.getCache("c3");
    assertThat(cache3 instanceof ConcurrentMapCache).isTrue();
    Cache cache3again = cm.getCache("c3");
    assertThat(cache3).isSameAs(cache3again);
    cache1.put("key1", "value1");
    assertThat(cache1.get("key1").get()).isEqualTo("value1");
    cache1.put("key2", 2);
    assertThat(cache1.get("key2").get()).isEqualTo(2);
    cache1.put("key3", null);
    assertThat(cache1.get("key3").get()).isNull();
    cache1.put("key3", null);
    assertThat(cache1.get("key3").get()).isNull();
    cache1.evict("key3");
    assertThat(cache1.get("key3")).isNull();
    assertThat(cache1.putIfAbsent("key1", "value1x").get()).isEqualTo("value1");
    assertThat(cache1.get("key1").get()).isEqualTo("value1");
    assertThat(cache1.putIfAbsent("key2", 2.1).get()).isEqualTo(2);
    assertThat(cache1.putIfAbsent("key3", null)).isNull();
    assertThat(cache1.get("key3").get()).isNull();
    assertThat(cache1.putIfAbsent("key3", null).get()).isNull();
    assertThat(cache1.get("key3").get()).isNull();
    cache1.evict("key3");
    assertThat(cache1.get("key3")).isNull();
}
Also used : CacheManager(cn.taketoday.cache.CacheManager) Cache(cn.taketoday.cache.Cache) Test(org.junit.jupiter.api.Test)

Example 12 with CacheManager

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

the class AbstractCacheResolver method resolveCaches.

@Override
public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) {
    Collection<String> cacheNames = getCacheNames(context);
    if (cacheNames == null) {
        return Collections.emptyList();
    }
    ArrayList<Cache> result = new ArrayList<>(cacheNames.size());
    CacheManager cacheManager = getCacheManager();
    for (String cacheName : cacheNames) {
        Cache cache = cacheManager.getCache(cacheName);
        if (cache == null) {
            throw new IllegalArgumentException("Cannot find cache named '" + cacheName + "' for " + context.getOperation());
        }
        result.add(cache);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) CacheManager(cn.taketoday.cache.CacheManager) Cache(cn.taketoday.cache.Cache)

Example 13 with CacheManager

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

the class ConcurrentMapCacheManagerTests method testDynamicMode.

@Test
public void testDynamicMode() {
    CacheManager cm = new ConcurrentMapCacheManager();
    Cache cache1 = cm.getCache("c1");
    assertThat(cache1 instanceof ConcurrentMapCache).isTrue();
    Cache cache1again = cm.getCache("c1");
    assertThat(cache1).isSameAs(cache1again);
    Cache cache2 = cm.getCache("c2");
    assertThat(cache2 instanceof ConcurrentMapCache).isTrue();
    Cache cache2again = cm.getCache("c2");
    assertThat(cache2).isSameAs(cache2again);
    Cache cache3 = cm.getCache("c3");
    assertThat(cache3 instanceof ConcurrentMapCache).isTrue();
    Cache cache3again = cm.getCache("c3");
    assertThat(cache3).isSameAs(cache3again);
    cache1.put("key1", "value1");
    assertThat(cache1.get("key1").get()).isEqualTo("value1");
    cache1.put("key2", 2);
    assertThat(cache1.get("key2").get()).isEqualTo(2);
    cache1.put("key3", null);
    assertThat(cache1.get("key3").get()).isNull();
    cache1.put("key3", null);
    assertThat(cache1.get("key3").get()).isNull();
    cache1.evict("key3");
    assertThat(cache1.get("key3")).isNull();
    assertThat(cache1.putIfAbsent("key1", "value1x").get()).isEqualTo("value1");
    assertThat(cache1.get("key1").get()).isEqualTo("value1");
    assertThat(cache1.putIfAbsent("key2", 2.1).get()).isEqualTo(2);
    assertThat(cache1.putIfAbsent("key3", null)).isNull();
    assertThat(cache1.get("key3").get()).isNull();
    assertThat(cache1.putIfAbsent("key3", null).get()).isNull();
    assertThat(cache1.get("key3").get()).isNull();
    cache1.evict("key3");
    assertThat(cache1.get("key3")).isNull();
}
Also used : CacheManager(cn.taketoday.cache.CacheManager) Cache(cn.taketoday.cache.Cache) Test(org.junit.jupiter.api.Test)

Example 14 with CacheManager

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

the class CaffeineCacheManagerTests method testDynamicMode.

@Test
public void testDynamicMode() {
    CacheManager cm = new CaffeineCacheManager();
    Cache cache1 = cm.getCache("c1");
    boolean condition2 = cache1 instanceof CaffeineCache;
    assertThat(condition2).isTrue();
    Cache cache1again = cm.getCache("c1");
    assertThat(cache1).isSameAs(cache1again);
    Cache cache2 = cm.getCache("c2");
    boolean condition1 = cache2 instanceof CaffeineCache;
    assertThat(condition1).isTrue();
    Cache cache2again = cm.getCache("c2");
    assertThat(cache2).isSameAs(cache2again);
    Cache cache3 = cm.getCache("c3");
    boolean condition = cache3 instanceof CaffeineCache;
    assertThat(condition).isTrue();
    Cache cache3again = cm.getCache("c3");
    assertThat(cache3).isSameAs(cache3again);
    cache1.put("key1", "value1");
    assertThat(cache1.get("key1").get()).isEqualTo("value1");
    cache1.put("key2", 2);
    assertThat(cache1.get("key2").get()).isEqualTo(2);
    cache1.put("key3", null);
    assertThat(cache1.get("key3").get()).isNull();
    cache1.evict("key3");
    assertThat(cache1.get("key3")).isNull();
}
Also used : CaffeineCacheManager(cn.taketoday.cache.support.CaffeineCacheManager) CaffeineCache(cn.taketoday.cache.support.CaffeineCache) CaffeineCacheManager(cn.taketoday.cache.support.CaffeineCacheManager) CacheManager(cn.taketoday.cache.CacheManager) CaffeineCache(cn.taketoday.cache.support.CaffeineCache) Cache(cn.taketoday.cache.Cache) Test(org.junit.jupiter.api.Test)

Aggregations

CacheManager (cn.taketoday.cache.CacheManager)14 Cache (cn.taketoday.cache.Cache)12 Test (org.junit.jupiter.api.Test)10 CaffeineCache (cn.taketoday.cache.support.CaffeineCache)2 CaffeineCacheManager (cn.taketoday.cache.support.CaffeineCacheManager)2 ApplicationContext (cn.taketoday.context.ApplicationContext)2 AnnotationConfigApplicationContext (cn.taketoday.context.annotation.AnnotationConfigApplicationContext)2 ArrayList (java.util.ArrayList)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2