Search in sources :

Example 1 with ConcurrentMapCache

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

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(org.springframework.cache.concurrent.ConcurrentMapCache) ArrayList(java.util.ArrayList) SimpleCacheManager(org.springframework.cache.support.SimpleCacheManager) ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache)

Example 2 with ConcurrentMapCache

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

the class TransactionAwareCacheDecoratorTests method clearNonTransactional.

@Test
public void clearNonTransactional() {
    Cache target = new ConcurrentMapCache("testCache");
    Cache cache = new TransactionAwareCacheDecorator(target);
    Object key = new Object();
    cache.put(key, "123");
    cache.clear();
    assertNull(target.get(key));
}
Also used : ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) Cache(org.springframework.cache.Cache) Test(org.junit.Test)

Example 3 with ConcurrentMapCache

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

the class TransactionAwareCacheDecoratorTests method evictNonTransactional.

@Test
public void evictNonTransactional() {
    Cache target = new ConcurrentMapCache("testCache");
    Cache cache = new TransactionAwareCacheDecorator(target);
    Object key = new Object();
    cache.put(key, "123");
    cache.evict(key);
    assertNull(target.get(key));
}
Also used : ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) Cache(org.springframework.cache.Cache) Test(org.junit.Test)

Example 4 with ConcurrentMapCache

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

the class TransactionAwareCacheDecoratorTests method putTransactional.

@Test
public void putTransactional() {
    Cache target = new ConcurrentMapCache("testCache");
    Cache cache = new TransactionAwareCacheDecorator(target);
    TransactionStatus status = txManager.getTransaction(new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED));
    Object key = new Object();
    cache.put(key, "123");
    assertNull(target.get(key));
    txManager.commit(status);
    assertEquals("123", target.get(key, String.class));
}
Also used : ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) DefaultTransactionAttribute(org.springframework.transaction.interceptor.DefaultTransactionAttribute) TransactionStatus(org.springframework.transaction.TransactionStatus) ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) Cache(org.springframework.cache.Cache) Test(org.junit.Test)

Example 5 with ConcurrentMapCache

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

the class TransactionAwareCacheDecoratorTests method putIfAbsent.

@Test
public void putIfAbsent() {
    // no transactional support for putIfAbsent
    Cache target = new ConcurrentMapCache("testCache");
    Cache cache = new TransactionAwareCacheDecorator(target);
    Object key = new Object();
    assertNull(cache.putIfAbsent(key, "123"));
    assertEquals("123", target.get(key, String.class));
    assertEquals("123", cache.putIfAbsent(key, "456").get());
    // unchanged
    assertEquals("123", target.get(key, String.class));
}
Also used : ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) ConcurrentMapCache(org.springframework.cache.concurrent.ConcurrentMapCache) Cache(org.springframework.cache.Cache) Test(org.junit.Test)

Aggregations

ConcurrentMapCache (org.springframework.cache.concurrent.ConcurrentMapCache)16 Cache (org.springframework.cache.Cache)10 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)6 Before (org.junit.Before)4 ClassPathResource (org.springframework.core.io.ClassPathResource)4 TransactionStatus (org.springframework.transaction.TransactionStatus)3 DefaultTransactionAttribute (org.springframework.transaction.interceptor.DefaultTransactionAttribute)3 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 SimpleCacheManager (org.springframework.cache.support.SimpleCacheManager)2 AnnotatedElementKey (org.springframework.context.expression.AnnotatedElementKey)1 EvaluationContext (org.springframework.expression.EvaluationContext)1