Search in sources :

Example 1 with CaffeineCacheManager

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

the class CaffeineCacheManagerTests method changeCacheLoaderRecreateCache.

@Test
public void changeCacheLoaderRecreateCache() {
    CaffeineCacheManager cm = new CaffeineCacheManager("c1");
    Cache cache1 = cm.getCache("c1");
    @SuppressWarnings("unchecked") CacheLoader<Object, Object> loader = mock(CacheLoader.class);
    cm.setCacheLoader(loader);
    Cache cache1x = cm.getCache("c1");
    assertThat(cache1x != cache1).isTrue();
    // Set same instance
    cm.setCacheLoader(loader);
    Cache cache1xx = cm.getCache("c1");
    assertThat(cache1xx).isSameAs(cache1x);
}
Also used : CaffeineCacheManager(cn.taketoday.cache.support.CaffeineCacheManager) CaffeineCache(cn.taketoday.cache.support.CaffeineCache) Cache(cn.taketoday.cache.Cache) Test(org.junit.jupiter.api.Test)

Example 2 with CaffeineCacheManager

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

the class CaffeineCacheManagerTests method setCacheNameNullRestoreDynamicMode.

@Test
public void setCacheNameNullRestoreDynamicMode() {
    CaffeineCacheManager cm = new CaffeineCacheManager("c1");
    assertThat(cm.getCache("someCache")).isNull();
    cm.setCacheNames(null);
    assertThat(cm.getCache("someCache")).isNotNull();
}
Also used : CaffeineCacheManager(cn.taketoday.cache.support.CaffeineCacheManager) Test(org.junit.jupiter.api.Test)

Example 3 with CaffeineCacheManager

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

the class CaffeineCacheManagerTests method customCacheRegistration.

@Test
public void customCacheRegistration() {
    CaffeineCacheManager cm = new CaffeineCacheManager("c1");
    com.github.benmanes.caffeine.cache.Cache<Object, Object> nc = Caffeine.newBuilder().build();
    cm.registerCustomCache("c2", nc);
    Cache cache1 = cm.getCache("c1");
    Cache cache2 = cm.getCache("c2");
    assertThat(nc == cache2.getNativeCache()).isTrue();
    cm.setCaffeine(Caffeine.newBuilder().maximumSize(10));
    assertThat(cm.getCache("c1") != cache1).isTrue();
    assertThat(cm.getCache("c2") == cache2).isTrue();
}
Also used : CaffeineCacheManager(cn.taketoday.cache.support.CaffeineCacheManager) CaffeineCache(cn.taketoday.cache.support.CaffeineCache) Cache(cn.taketoday.cache.Cache) Test(org.junit.jupiter.api.Test)

Example 4 with CaffeineCacheManager

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

the class CaffeineCacheManagerTests method changeCacheSpecificationRecreateCache.

@Test
public void changeCacheSpecificationRecreateCache() {
    CaffeineCacheManager cm = new CaffeineCacheManager("c1");
    Cache cache1 = cm.getCache("c1");
    cm.setCacheSpecification("maximumSize=10");
    Cache cache1x = cm.getCache("c1");
    assertThat(cache1x != cache1).isTrue();
}
Also used : CaffeineCacheManager(cn.taketoday.cache.support.CaffeineCacheManager) CaffeineCache(cn.taketoday.cache.support.CaffeineCache) Cache(cn.taketoday.cache.Cache) Test(org.junit.jupiter.api.Test)

Example 5 with CaffeineCacheManager

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

the class CaffeineCacheManagerTests method cacheLoaderUseLoadingCache.

@Test
public void cacheLoaderUseLoadingCache() {
    CaffeineCacheManager cm = new CaffeineCacheManager("c1");
    cm.setCacheLoader(key -> {
        if ("ping".equals(key)) {
            return "pong";
        }
        throw new IllegalArgumentException("I only know ping");
    });
    Cache cache1 = cm.getCache("c1");
    Cache.ValueWrapper value = cache1.get("ping");
    assertThat(value).isNotNull();
    assertThat(value.get()).isEqualTo("pong");
    assertThatIllegalArgumentException().isThrownBy(() -> assertThat(cache1.get("foo")).isNull()).withMessageContaining("I only know ping");
}
Also used : CaffeineCacheManager(cn.taketoday.cache.support.CaffeineCacheManager) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) CaffeineCache(cn.taketoday.cache.support.CaffeineCache) Cache(cn.taketoday.cache.Cache) Test(org.junit.jupiter.api.Test)

Aggregations

CaffeineCacheManager (cn.taketoday.cache.support.CaffeineCacheManager)19 Test (org.junit.jupiter.api.Test)19 Cache (cn.taketoday.cache.Cache)17 CaffeineCache (cn.taketoday.cache.support.CaffeineCache)17 CacheManager (cn.taketoday.cache.CacheManager)2 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)2 NoSuchCacheException (cn.taketoday.cache.NoSuchCacheException)1 CacheConfiguration (cn.taketoday.cache.annotation.CacheConfiguration)1 Method (java.lang.reflect.Method)1