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

The following examples show how to use org.springframework.cache.CacheManager#getCache() . 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
Source File: GuavaCacheManagerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 File: BookmarkServiceImpl.java    From yes-cart with 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
Source File: AttributeValidationHelper.java    From rice with Educational Community License v2.0 6 votes vote down vote up
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
Source File: AttributeValidationHelper.java    From rice with Educational Community License v2.0 6 votes vote down vote up
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
Source File: CaffeineCacheManagerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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
Source File: CompositeCacheManager.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Cache getCache(String name) {
	for (CacheManager cacheManager : this.cacheManagers) {
		Cache cache = cacheManager.getCache(name);
		if (cache != null) {
			return cache;
		}
	}
	return null;
}
 
Example 7
Source File: AbstractCacheAnnotationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 8
Source File: TwoLevelCacheTemplateTest.java    From n2o-framework with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: CachingResourceResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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
Source File: CachingResourceTransformer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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
Source File: CachingResourceResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 12
Source File: CachingResourceTransformer.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 13
Source File: CachingResourceResolver.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public CachingResourceResolver(CacheManager cacheManager, String cacheName) {
	this(cacheManager.getCache(cacheName));
}
 
Example 14
Source File: AttributeToCodeValueConverter.java    From yes-cart with Apache License 2.0 4 votes vote down vote up
public AttributeToCodeValueConverter(final GenericDAO<Object, Long> genericDAO,
                                     final CacheManager cacheManager) {
    this.genericDAO = genericDAO;
    this.attributeCache = cacheManager.getCache("attributeService-byAttributeCode");
}
 
Example 15
Source File: CachingResourceTransformer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public CachingResourceTransformer(CacheManager cacheManager, String cacheName) {
	this(cacheManager.getCache(cacheName));
}
 
Example 16
Source File: AbstractFileServiceImpl.java    From yes-cart with Apache License 2.0 4 votes vote down vote up
protected AbstractFileServiceImpl(final CacheManager cacheManager) {
    FILE_URI_CACHE = cacheManager.getCache("web.fileService-fileURI");
    OBJECT_FILES_CACHE = cacheManager.getCache("web.fileService-objectFiles");
}
 
Example 17
Source File: CachingResourceResolver.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public CachingResourceResolver(CacheManager cacheManager, String cacheName) {
	this(cacheManager.getCache(cacheName));
}
 
Example 18
Source File: ThymeleafTemplateSupportImpl.java    From yes-cart with Apache License 2.0 3 votes vote down vote up
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 19
Source File: GroovyGStringTemplateSupportImpl.java    From yes-cart with Apache License 2.0 3 votes vote down vote up
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 20
Source File: GroovySimpleTemplateSupportImpl.java    From yes-cart with Apache License 2.0 2 votes vote down vote up
public GroovySimpleTemplateSupportImpl(final CacheManager cacheManager) {

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

    }