Search in sources :

Example 16 with CacheManager

use of org.springframework.cache.CacheManager in project spring-framework by spring-projects.

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(org.springframework.cache.CacheManager) Cache(org.springframework.cache.Cache) Test(org.junit.jupiter.api.Test)

Example 17 with CacheManager

use of org.springframework.cache.CacheManager in project spring-framework by spring-projects.

the class CacheResolverCustomizationTests method setup.

@BeforeEach
public void setup() {
    ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
    this.cacheManager = context.getBean("cacheManager", CacheManager.class);
    this.anotherCacheManager = context.getBean("anotherCacheManager", CacheManager.class);
    this.simpleService = context.getBean(SimpleService.class);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) CacheManager(org.springframework.cache.CacheManager) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 18 with CacheManager

use of org.springframework.cache.CacheManager in project spring-framework by spring-projects.

the class CacheResolverCustomizationTests method setUp.

@Before
public void setUp() {
    ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
    this.cacheManager = context.getBean("cacheManager", CacheManager.class);
    this.anotherCacheManager = context.getBean("anotherCacheManager", CacheManager.class);
    this.simpleService = context.getBean(SimpleService.class);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) CacheManager(org.springframework.cache.CacheManager) Before(org.junit.Before)

Example 19 with CacheManager

use of org.springframework.cache.CacheManager in project spring-boot by spring-projects.

the class CacheAutoConfigurationTests method validateCacheManager.

private <T extends CacheManager> T validateCacheManager(Class<T> type) {
    CacheManager cacheManager = this.context.getBean(CacheManager.class);
    assertThat(cacheManager).as("Wrong cache manager type").isInstanceOf(type);
    return type.cast(cacheManager);
}
Also used : CaffeineCacheManager(org.springframework.cache.caffeine.CaffeineCacheManager) SimpleCacheManager(org.springframework.cache.support.SimpleCacheManager) ConcurrentMapCacheManager(org.springframework.cache.concurrent.ConcurrentMapCacheManager) EhCacheCacheManager(org.springframework.cache.ehcache.EhCacheCacheManager) CouchbaseCacheManager(com.couchbase.client.spring.cache.CouchbaseCacheManager) SpringEmbeddedCacheManager(org.infinispan.spring.provider.SpringEmbeddedCacheManager) NoOpCacheManager(org.springframework.cache.support.NoOpCacheManager) HazelcastCacheManager(com.hazelcast.spring.cache.HazelcastCacheManager) CacheManager(org.springframework.cache.CacheManager) RedisCacheManager(org.springframework.data.redis.cache.RedisCacheManager) JCacheCacheManager(org.springframework.cache.jcache.JCacheCacheManager)

Example 20 with CacheManager

use of org.springframework.cache.CacheManager in project spring-framework by spring-projects.

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);
    assertSame(r1, this.cs.customCacheManager(key));
    Cache cache = customCm.getCache("testCache");
    assertNotNull(cache.get(key));
}
Also used : CacheManager(org.springframework.cache.CacheManager) Cache(org.springframework.cache.Cache) Test(org.junit.Test)

Aggregations

CacheManager (org.springframework.cache.CacheManager)28 Test (org.junit.jupiter.api.Test)12 ConcurrentMapCacheManager (org.springframework.cache.concurrent.ConcurrentMapCacheManager)10 SimpleCacheManager (org.springframework.cache.support.SimpleCacheManager)10 Cache (org.springframework.cache.Cache)8 LinkedHashMap (java.util.LinkedHashMap)5 HazelcastCacheManager (com.hazelcast.spring.cache.HazelcastCacheManager)4 CaffeineCacheManager (org.springframework.cache.caffeine.CaffeineCacheManager)4 EhCacheCacheManager (org.springframework.cache.ehcache.EhCacheCacheManager)4 NoOpCacheManager (org.springframework.cache.support.NoOpCacheManager)4 RedisCacheManager (org.springframework.data.redis.cache.RedisCacheManager)4 JCacheCacheManager (org.springframework.cache.jcache.JCacheCacheManager)3 CouchbaseCacheManager (com.couchbase.client.spring.cache.CouchbaseCacheManager)2 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 SpringEmbeddedCacheManager (org.infinispan.spring.provider.SpringEmbeddedCacheManager)2 CacheEntry (org.springframework.boot.actuate.cache.CachesEndpoint.CacheEntry)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2