Search in sources :

Example 1 with CaffeineCache

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

the class CaffeineCacheTests method setUp.

@BeforeEach
void setUp() {
    nativeCache = Caffeine.newBuilder().build();
    cache = new CaffeineCache(CACHE_NAME, nativeCache);
    com.github.benmanes.caffeine.cache.Cache<Object, Object> nativeCacheNoNull = Caffeine.newBuilder().build();
    cacheNoNull = new CaffeineCache(CACHE_NAME_NO_NULL, nativeCacheNoNull, false);
}
Also used : CaffeineCache(cn.taketoday.cache.support.CaffeineCache) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with CaffeineCache

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

the class CaffeineCacheManagerTests method testStaticMode.

@Test
public void testStaticMode() {
    CaffeineCacheManager cm = new CaffeineCacheManager("c1", "c2");
    Cache cache1 = cm.getCache("c1");
    boolean condition3 = cache1 instanceof CaffeineCache;
    assertThat(condition3).isTrue();
    Cache cache1again = cm.getCache("c1");
    assertThat(cache1).isSameAs(cache1again);
    Cache cache2 = cm.getCache("c2");
    boolean condition2 = cache2 instanceof CaffeineCache;
    assertThat(condition2).isTrue();
    Cache cache2again = cm.getCache("c2");
    assertThat(cache2).isSameAs(cache2again);
    Cache cache3 = cm.getCache("c3");
    assertThat(cache3).isNull();
    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();
    cm.setAllowNullValues(false);
    Cache cache1x = cm.getCache("c1");
    boolean condition1 = cache1x instanceof CaffeineCache;
    assertThat(condition1).isTrue();
    assertThat(cache1x != cache1).isTrue();
    Cache cache2x = cm.getCache("c2");
    boolean condition = cache2x instanceof CaffeineCache;
    assertThat(condition).isTrue();
    assertThat(cache2x != cache2).isTrue();
    Cache cache3x = cm.getCache("c3");
    assertThat(cache3x).isNull();
    cache1x.put("key1", "value1");
    assertThat(cache1x.get("key1").get()).isEqualTo("value1");
    cache1x.put("key2", 2);
    assertThat(cache1x.get("key2").get()).isEqualTo(2);
    cm.setAllowNullValues(true);
    Cache cache1y = cm.getCache("c1");
    cache1y.put("key3", null);
    assertThat(cache1y.get("key3").get()).isNull();
    cache1y.evict("key3");
    assertThat(cache1y.get("key3")).isNull();
}
Also used : CaffeineCacheManager(cn.taketoday.cache.support.CaffeineCacheManager) CaffeineCache(cn.taketoday.cache.support.CaffeineCache) CaffeineCache(cn.taketoday.cache.support.CaffeineCache) Cache(cn.taketoday.cache.Cache) Test(org.junit.jupiter.api.Test)

Example 3 with CaffeineCache

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

the class CaffeineCacheTests method testLoadingCacheGetWithType.

@Test
void testLoadingCacheGetWithType() {
    String value = "value";
    CaffeineCache loadingCache = new CaffeineCache(CACHE_NAME, Caffeine.newBuilder().build(key -> value));
    String valueWrapper = loadingCache.get(new Object(), String.class);
    assertThat(valueWrapper).isNotNull();
    assertThat(valueWrapper).isEqualTo(value);
}
Also used : Test(org.junit.jupiter.api.Test) BeforeEach(org.junit.jupiter.api.BeforeEach) CaffeineCache(cn.taketoday.cache.support.CaffeineCache) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) AbstractValueAdaptingCacheTests(cn.taketoday.contextsupport.testfixture.cache.AbstractValueAdaptingCacheTests) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ValueWrapper(cn.taketoday.cache.Cache.ValueWrapper) Cache(cn.taketoday.cache.Cache) CaffeineCache(cn.taketoday.cache.support.CaffeineCache) Test(org.junit.jupiter.api.Test)

Example 4 with CaffeineCache

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

the class CaffeineCacheTests method testLoadingCacheGet.

@Test
void testLoadingCacheGet() {
    Object value = new Object();
    CaffeineCache loadingCache = new CaffeineCache(CACHE_NAME, Caffeine.newBuilder().build(key -> value));
    ValueWrapper valueWrapper = loadingCache.get(new Object());
    assertThat(valueWrapper).isNotNull();
    assertThat(valueWrapper.get()).isEqualTo(value);
}
Also used : Test(org.junit.jupiter.api.Test) BeforeEach(org.junit.jupiter.api.BeforeEach) CaffeineCache(cn.taketoday.cache.support.CaffeineCache) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) AbstractValueAdaptingCacheTests(cn.taketoday.contextsupport.testfixture.cache.AbstractValueAdaptingCacheTests) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ValueWrapper(cn.taketoday.cache.Cache.ValueWrapper) Cache(cn.taketoday.cache.Cache) ValueWrapper(cn.taketoday.cache.Cache.ValueWrapper) CaffeineCache(cn.taketoday.cache.support.CaffeineCache) Test(org.junit.jupiter.api.Test)

Example 5 with CaffeineCache

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

the class CaffeineCacheTests method setUp.

@BeforeEach
void setUp() {
    nativeCache = Caffeine.newBuilder().build();
    cache = new CaffeineCache(CACHE_NAME, nativeCache);
    com.github.benmanes.caffeine.cache.Cache<Object, Object> nativeCacheNoNull = Caffeine.newBuilder().build();
    cacheNoNull = new CaffeineCache(CACHE_NAME_NO_NULL, nativeCacheNoNull, false);
}
Also used : CaffeineCache(cn.taketoday.cache.support.CaffeineCache) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

CaffeineCache (cn.taketoday.cache.support.CaffeineCache)14 Cache (cn.taketoday.cache.Cache)12 Test (org.junit.jupiter.api.Test)12 ValueWrapper (cn.taketoday.cache.Cache.ValueWrapper)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 AbstractValueAdaptingCacheTests (cn.taketoday.contextsupport.testfixture.cache.AbstractValueAdaptingCacheTests)6 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)6 CaffeineCacheManager (cn.taketoday.cache.support.CaffeineCacheManager)4 CacheManager (cn.taketoday.cache.CacheManager)2