Java Code Examples for org.springframework.cache.CacheManager#getCache()

The following examples show how to use org.springframework.cache.CacheManager#getCache() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void testDynamicMode() {
	CacheManager cm = new GuavaCacheManager();
	Cache cache1 = cm.getCache("c1");
	assertTrue(cache1 instanceof GuavaCache);
	Cache cache1again = cm.getCache("c1");
	assertSame(cache1again, cache1);
	Cache cache2 = cm.getCache("c2");
	assertTrue(cache2 instanceof GuavaCache);
	Cache cache2again = cm.getCache("c2");
	assertSame(cache2again, cache2);
	Cache cache3 = cm.getCache("c3");
	assertTrue(cache3 instanceof GuavaCache);
	Cache cache3again = cm.getCache("c3");
	assertSame(cache3again, 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"));
}
 
Example 2
Source Project: yes-cart   File: BookmarkServiceImpl.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Construct bookmark service.
 *
 * @param categoryService category service
 * @param contentService  content service
 * @param productService  product service
 * @param cacheManager    cache manager to use
 */
public BookmarkServiceImpl(final CategoryService categoryService,
                           final ContentService contentService,
                           final ProductService productService,
                           final CacheManager cacheManager) {

    this.categoryService = categoryService;
    this.productService = productService;
    this.contentService = contentService;

    CATEGORY_CACHE = cacheManager.getCache("web.bookmarkService-seoCategory");
    CONTENT_CACHE = cacheManager.getCache("web.bookmarkService-seoContent");
    SKU_CACHE = cacheManager.getCache("web.bookmarkService-seoSku");
    PRODUCT_CACHE = cacheManager.getCache("web.bookmarkService-seoProduct");

}
 
Example 3
protected KimAttribute getAttributeDefinitionByName( String attributeName ) {
    CacheManager cm = CoreImplServiceLocator.getCacheManagerRegistry().getCacheManagerByCacheName(KimAttribute.Cache.NAME);
    Cache cache = cm.getCache(KimAttribute.Cache.NAME);
    String cacheKey = "name=" + attributeName;
    ValueWrapper valueWrapper = cache.get( cacheKey );
    
    if ( valueWrapper != null ) {
        return (KimAttribute) valueWrapper.get();
    }

    List<KimAttributeBo> attributeImpls = KradDataServiceLocator.getDataObjectService().findMatching( KimAttributeBo.class, QueryByCriteria.Builder.forAttribute(KRADPropertyConstants.ATTRIBUTE_NAME, attributeName).build()).getResults();
    KimAttribute attribute = null;
    if ( !attributeImpls.isEmpty() ) {
        attribute = KimAttributeBo.to(attributeImpls.get(0)); 
    }
    
    cache.put( cacheKey, attribute );

    return attribute;
}
 
Example 4
protected KimAttribute getAttributeDefinitionById( String id ) {
      CacheManager cm = CoreImplServiceLocator.getCacheManagerRegistry().getCacheManagerByCacheName(KimAttribute.Cache.NAME);
      Cache cache = cm.getCache(KimAttribute.Cache.NAME);
      String cacheKey = "id=" + id;
      ValueWrapper valueWrapper = cache.get( cacheKey );
      
      if ( valueWrapper != null ) {
          return (KimAttribute) valueWrapper.get();
      }

KimAttributeBo attributeImpl = KradDataServiceLocator.getDataObjectService().find( KimAttributeBo.class, id );
KimAttribute attribute = KimAttributeBo.to(attributeImpl);
cache.put( cacheKey, attribute );

  	return attribute;
  }
 
Example 5
@Test
public void testDynamicMode() {
	CacheManager cm = new CaffeineCacheManager();
	Cache cache1 = cm.getCache("c1");
	assertTrue(cache1 instanceof CaffeineCache);
	Cache cache1again = cm.getCache("c1");
	assertSame(cache1again, cache1);
	Cache cache2 = cm.getCache("c2");
	assertTrue(cache2 instanceof CaffeineCache);
	Cache cache2again = cm.getCache("c2");
	assertSame(cache2again, cache2);
	Cache cache3 = cm.getCache("c3");
	assertTrue(cache3 instanceof CaffeineCache);
	Cache cache3again = cm.getCache("c3");
	assertSame(cache3again, 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"));
}
 
Example 6
@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));
}
 
Example 7
public CachingResourceResolver(CacheManager cacheManager, String cacheName) {
	Cache cache = cacheManager.getCache(cacheName);
	if (cache == null) {
		throw new IllegalArgumentException("Cache '" + cacheName + "' not found");
	}
	this.cache = cache;
}
 
Example 8
public CachingResourceTransformer(CacheManager cacheManager, String cacheName) {
	Cache cache = cacheManager.getCache(cacheName);
	if (cache == null) {
		throw new IllegalArgumentException("Cache '" + cacheName + "' not found");
	}
	this.cache = cache;
}
 
Example 9
public CachingResourceResolver(CacheManager cacheManager, String cacheName) {
	Cache cache = cacheManager.getCache(cacheName);
	if (cache == null) {
		throw new IllegalArgumentException("Cache '" + cacheName + "' not found");
	}
	this.cache = cache;
}
 
Example 10
public CachingResourceTransformer(CacheManager cacheManager, String cacheName) {
	Cache cache = cacheManager.getCache(cacheName);
	if (cache == null) {
		throw new IllegalArgumentException("Cache '" + cacheName + "' not found");
	}
	this.cache = cache;
}
 
Example 11
@Test
@Ignore
public void testWriteBehindPutEvictSync() throws InterruptedException {
    CacheManager cacheManager = StaticSpringContext.getBean(CacheManager.class);
    Cache cache = cacheManager.getCache("writeBehindCache");
    net.sf.ehcache.Cache nativeCache = (net.sf.ehcache.Cache) cache.getNativeCache();
    System.out.println("cache put:" + "test");
    nativeCache.putWithWriter(new Element(1, "test"));
    System.out.println("cache get:" + cache.get(1).get());
    System.out.println("cache evict:" + 1);
    nativeCache.removeWithWriter(1);
    System.out.println("sleep:" + 2 + "s");
    Thread.sleep(2000);
}
 
Example 12
@Override
public Cache getCache(String name) {
	for (CacheManager cacheManager : this.cacheManagers) {
		Cache cache = cacheManager.getCache(name);
		if (cache != null) {
			return cache;
		}
	}
	return null;
}
 
Example 13
public CachingResourceResolver(CacheManager cacheManager, String cacheName) {
	this(cacheManager.getCache(cacheName));
}
 
Example 14
protected AbstractFileServiceImpl(final CacheManager cacheManager) {
    FILE_URI_CACHE = cacheManager.getCache("web.fileService-fileURI");
    OBJECT_FILES_CACHE = cacheManager.getCache("web.fileService-objectFiles");
}
 
Example 15
public CachingResourceTransformer(CacheManager cacheManager, String cacheName) {
	this(cacheManager.getCache(cacheName));
}
 
Example 16
public AttributeToCodeValueConverter(final GenericDAO<Object, Long> genericDAO,
                                     final CacheManager cacheManager) {
    this.genericDAO = genericDAO;
    this.attributeCache = cacheManager.getCache("attributeService-byAttributeCode");
}
 
Example 17
public CachingResourceResolver(CacheManager cacheManager, String cacheName) {
	this(cacheManager.getCache(cacheName));
}
 
Example 18
public GroovyGStringTemplateSupportImpl(final CacheManager cacheManager) throws ClassNotFoundException {

        TEMPLATE_CACHE = cacheManager.getCache("contentService-templateSupport");

        final ClassLoader classLoader = this.getClass().getClassLoader();
        classLoader.loadClass(DecimalFormat.class.getName());
        this.engine = new GStringTemplateEngine(classLoader);

    }
 
Example 19
public ThymeleafTemplateSupportImpl(final CacheManager cacheManager) throws ClassNotFoundException {

        TEMPLATE_CACHE = cacheManager.getCache("contentService-templateSupport");

        final SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setCacheManager(new ThymeleafCacheManager());
        this.engine = engine;

    }
 
Example 20
public GroovySimpleTemplateSupportImpl(final CacheManager cacheManager) {

        TEMPLATE_CACHE = cacheManager.getCache("contentService-templateSupport");

    }