Package org.springframework.cache

Examples of org.springframework.cache.Cache


public class ConcurrentMapCacheManagerTests {

  @Test
  public void testDynamicMode() {
    CacheManager cm = new ConcurrentMapCacheManager();
    Cache cache1 = cm.getCache("c1");
    assertTrue(cache1 instanceof ConcurrentMapCache);
    Cache cache1again = cm.getCache("c1");
    assertSame(cache1again, cache1);
    Cache cache2 = cm.getCache("c2");
    assertTrue(cache2 instanceof ConcurrentMapCache);
    Cache cache2again = cm.getCache("c2");
    assertSame(cache2again, cache2);
    Cache cache3 = cm.getCache("c3");
    assertTrue(cache3 instanceof ConcurrentMapCache);
    Cache cache3again = cm.getCache("c3");
    assertSame(cache3again, cache3);

    cache1.put("key1", "value1");
    assertEquals("value1", cache1.get("key1").get());
    cache1.put("key2", 2);
View Full Code Here


  }

  @Test
  public void testStaticMode() {
    ConcurrentMapCacheManager cm = new ConcurrentMapCacheManager("c1", "c2");
    Cache cache1 = cm.getCache("c1");
    assertTrue(cache1 instanceof ConcurrentMapCache);
    Cache cache1again = cm.getCache("c1");
    assertSame(cache1again, cache1);
    Cache cache2 = cm.getCache("c2");
    assertTrue(cache2 instanceof ConcurrentMapCache);
    Cache cache2again = cm.getCache("c2");
    assertSame(cache2again, cache2);
    Cache cache3 = cm.getCache("c3");
    assertNull(cache3);

    cache1.put("key1", "value1");
    assertEquals("value1", cache1.get("key1").get());
    cache1.put("key2", 2);
    assertEquals(2, cache1.get("key2").get());
    cache1.put("key3", null);
    assertNull(cache1.get("key3").get());
    cache1.evict("key3");
    assertNull(cache1.get("key3"));

    cm.setAllowNullValues(false);
    Cache cache1x = cm.getCache("c1");
    assertTrue(cache1x instanceof ConcurrentMapCache);
    assertTrue(cache1x != cache1);
    Cache cache2x = cm.getCache("c2");
    assertTrue(cache2x instanceof ConcurrentMapCache);
    assertTrue(cache2x != cache2);
    Cache cache3x = cm.getCache("c3");
    assertNull(cache3x);

    cache1x.put("key1", "value1");
    assertEquals("value1", cache1x.get("key1").get());
    cache1x.put("key2", 2);
    assertEquals(2, cache1x.get("key2").get());
    try {
      cache1x.put("key3", null);
      fail("Should have thrown NullPointerException");
    }
    catch (NullPointerException ex) {
      // expected
    }

    cm.setAllowNullValues(true);
    Cache cache1y = cm.getCache("c1");

    cache1y.put("key3", null);
    assertNull(cache1y.get("key3").get());
    cache1y.evict("key3");
    assertNull(cache1y.get("key3"));
  }
View Full Code Here

    this.simpleService = context.getBean(SimpleService.class);
  }

  @Test
  public void noCustomization() {
    Cache cache = cacheManager.getCache("default");

    Object key = new Object();
    assertCacheMiss(key, cache);

    Object value = simpleService.getSimple(key);
View Full Code Here

    assertCacheHit(key, value, cache);
  }

  @Test
  public void customCacheResolver() {
    Cache cache = cacheManager.getCache("primary");

    Object key = new Object();
    assertCacheMiss(key, cache);

    Object value = simpleService.getWithCustomCacheResolver(key);
View Full Code Here

    assertCacheHit(key, value, cache);
  }

  @Test
  public void customCacheManager() {
    Cache cache = anotherCacheManager.getCache("default");

    Object key = new Object();
    assertCacheMiss(key, cache);

    Object value = simpleService.getWithCustomCacheManager(key);
View Full Code Here

    assertCacheHit(key, value, cache);
  }

  @Test
  public void runtimeResolution() {
    Cache defaultCache = cacheManager.getCache("default");
    Cache primaryCache = cacheManager.getCache("primary");

    Object key = new Object();
    assertCacheMiss(key, defaultCache, primaryCache);
    Object value = simpleService.getWithRuntimeCacheResolution(key, "default");
    assertCacheHit(key, value, defaultCache);
View Full Code Here

    assertCacheMiss(key2, defaultCache);
  }

  @Test
  public void namedResolution() {
    Cache cache = cacheManager.getCache("secondary");

    Object key = new Object();
    assertCacheMiss(key, cache);

    Object value = simpleService.getWithNamedCacheResolution(key);
View Full Code Here

  }

  private void fooGetSimple(ApplicationContext context, FooService service) {
    CacheManager cacheManager = context.getBean(CacheManager.class);

    Cache cache = cacheManager.getCache("testCache");

    Object key = new Object();
    assertCacheMiss(key, cache);

    Object value = service.getSimple(key);
View Full Code Here

      return cacheManager;
    }

    @Bean
    public Cache mockCache() {
      Cache cache = mock(Cache.class);
      given(cache.getName()).willReturn("test");
      return cache;
    }
View Full Code Here

    return Collections.unmodifiableSet(this.cacheMap.keySet());
  }

  @Override
  public Cache getCache(String name) {
    Cache cache = this.cacheMap.get(name);
    if (cache == null && this.dynamic) {
      synchronized (this.cacheMap) {
        cache = this.cacheMap.get(name);
        if (cache == null) {
          cache = createConcurrentMapCache(name);
View Full Code Here

TOP

Related Classes of org.springframework.cache.Cache

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.