Java Code Examples for org.springframework.cache.Cache#put()

The following examples show how to use org.springframework.cache.Cache#put() . 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: AbstractJCacheAnnotationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void earlyRemoveAllWithException() {
	Cache cache = getCache(DEFAULT_CACHE);

	Object key = createKey(name.getMethodName());
	cache.put(key, new Object());

	try {
		service.earlyRemoveAllWithException(true);
		fail("Should have thrown an exception");
	}
	catch (UnsupportedOperationException e) {
		// This is what we expect
	}
	assertTrue(isEmpty(cache));
}
 
Example 2
Source File: AbstractJCacheAnnotationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void earlyRemoveAllWithExceptionVetoRemove() {
	Cache cache = getCache(DEFAULT_CACHE);

	Object key = createKey(name.getMethodName());
	cache.put(key, new Object());

	try {
		service.earlyRemoveAllWithException(false);
		fail("Should have thrown an exception");
	}
	catch (NullPointerException e) {
		// This is what we expect
	}
	// This will be remove anyway as the earlyRemove has removed the cache before
	assertTrue(isEmpty(cache));
}
 
Example 3
Source File: CachedRestTemplate.java    From jandy with GNU General Public License v3.0 6 votes vote down vote up
public <T> HttpCacheEntry<T> getForCacheEntry(Cache cache, URI url, Class<T> responseType) throws RestClientException {
  HttpCacheEntry<T> entry = cache.get(url.toString(), HttpCacheEntry.class);

  HttpHeaders headers = new HttpHeaders();
  if (accept != null)
    headers.set(HttpHeaders.ACCEPT, accept);
  if (entry != null && entry.getEtag() != null)
    headers.set(HttpHeaders.IF_NONE_MATCH, entry.getEtag());

  RequestCallback requestCallback = httpEntityCallback(new HttpEntity(headers), responseType);
  ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);

  ResponseEntity<T> responseEntity = execute(url, HttpMethod.GET, requestCallback, responseExtractor);

  if (entry == null || !HttpStatus.NOT_MODIFIED.equals(responseEntity.getStatusCode())) {
    entry = new HttpCacheEntry<>();
    entry.setEtag(responseEntity.getHeaders().getETag());
    entry.setBody(responseEntity.getBody());
    entry.setLink(responseEntity.getHeaders().get("Link"));
    cache.put(responseEntity, entry);
  }

  return entry;
}
 
Example 4
Source File: TransactionAwareCacheDecoratorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void clearTransactional() {
	Cache target = new ConcurrentMapCache("testCache");
	Cache cache = new TransactionAwareCacheDecorator(target);
	Object key = new Object();
	cache.put(key, "123");


	TransactionStatus status = this.txManager.getTransaction(
			new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED));
	cache.clear();
	assertEquals("123", target.get(key, String.class));
	this.txManager.commit(status);

	assertNull(target.get(key));
}
 
Example 5
Source File: AbstractJCacheAnnotationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void removeAllWithExceptionVetoRemove() {
	Cache cache = getCache(DEFAULT_CACHE);

	Object key = createKey(name.getMethodName());
	cache.put(key, new Object());

	try {
		service.removeAllWithException(false);
		fail("Should have thrown an exception");
	}
	catch (NullPointerException e) {
		// This is what we expect
	}
	assertNotNull(cache.get(key));
}
 
Example 6
Source File: CacheResultInterceptor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected void cacheException(Cache exceptionCache, ExceptionTypeFilter filter, Object cacheKey, Throwable ex) {
	if (exceptionCache == null) {
		return;
	}
	if (filter.match(ex.getClass())) {
		exceptionCache.put(cacheKey, ex);
	}
}
 
Example 7
Source File: PageManagerCacheWrapper.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void upgradePositionForSisterDeletion(Cache cache, String code, boolean online) {
    IPage page = (online) ? this.getOnlinePage(code) : this.getDraftPage(code);
    if (null == page) {
        return;
    }
    ((Page) page).setPosition(page.getPosition() - 1);
    if (online) {
        cache.put(ONLINE_PAGE_CACHE_NAME_PREFIX + page.getCode(), page);
    } else {
        cache.put(DRAFT_PAGE_CACHE_NAME_PREFIX + page.getCode(), page);
    }
}
 
Example 8
Source File: DictServiceImpl.java    From albedo with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void init() {
	Cache cache = cacheManager.getCache(CacheNameConstants.DICT_DETAILS);
	List<Dict> dictList = findAllOrderBySort();
	if (ObjectUtil.isNotEmpty(dictList)) {
		cache.put(CacheNameConstants.DICT_ALL, dictList);
	}
}
 
Example 9
Source File: SeoMappingCacheWrapper.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void insertVoObjectsOnCache(Cache cache, 
           Map<String, ?> objects, String listKey, String prefixKey) {
	List<String> codes = new ArrayList<>();
	Iterator<String> iter = objects.keySet().iterator();
	while (iter.hasNext()) {
		String key = iter.next();
		cache.put(prefixKey + key, objects.get(key));
		codes.add(key);
	}
	cache.put(listKey, codes);
}
 
Example 10
Source File: TransactionAwareCacheDecoratorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void putNonTransactional() {
	Cache target = new ConcurrentMapCache("testCache");
	Cache cache = new TransactionAwareCacheDecorator(target);

	Object key = new Object();
	cache.put(key, "123");
	assertEquals("123", target.get(key, String.class));
}
 
Example 11
Source File: TransactionAwareCacheDecoratorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void putTransactional() {
	Cache target = new ConcurrentMapCache("testCache");
	Cache cache = new TransactionAwareCacheDecorator(target);

	TransactionStatus status = this.txManager.getTransaction(
			new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED));

	Object key = new Object();
	cache.put(key, "123");
	assertNull(target.get(key));
	this.txManager.commit(status);

	assertEquals("123", target.get(key, String.class));
}
 
Example 12
Source File: CaffeineCacheManagerTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testStaticMode() {
	CaffeineCacheManager cm = new CaffeineCacheManager("c1", "c2");
	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");
	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 CaffeineCache);
	assertTrue(cache1x != cache1);
	Cache cache2x = cm.getCache("c2");
	assertTrue(cache2x instanceof CaffeineCache);
	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());

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

	cache1y.put("key3", null);
	assertNull(cache1y.get("key3").get());
	cache1y.evict("key3");
	assertNull(cache1y.get("key3"));
}
 
Example 13
Source File: EhcacheDao.java    From flash-waimai with MIT License 4 votes vote down vote up
@Override
public void hset(Serializable key, Serializable k, Object val) {
    Cache cache = cacheManager.getCache(String.valueOf(key));
    cache.put(k,val);
}
 
Example 14
Source File: CaffeineCacheManagerTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testStaticMode() {
	CaffeineCacheManager cm = new CaffeineCacheManager("c1", "c2");
	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");
	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 CaffeineCache);
	assertTrue(cache1x != cache1);
	Cache cache2x = cm.getCache("c2");
	assertTrue(cache2x instanceof CaffeineCache);
	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());

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

	cache1y.put("key3", null);
	assertNull(cache1y.get("key3").get());
	cache1y.evict("key3");
	assertNull(cache1y.get("key3"));
}
 
Example 15
Source File: RolloutStatusCache.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
private void putIntoCache(final Long id, final List<TotalTargetCountActionStatus> status, final Cache cache) {
    cache.put(id, new CachedTotalTargetCountActionStatus(id, status));
}
 
Example 16
Source File: EhcacheDao.java    From flash-waimai with MIT License 4 votes vote down vote up
@Override
public void set(Serializable key, Object val) {
    Cache cache = cacheManager.getCache(CONSTANT);
    cache.put(key,val);

}
 
Example 17
Source File: DictServiceImpl.java    From cola-cloud with MIT License 4 votes vote down vote up
/**
 * 缓存所有的数据字典
 */
@PostConstruct
public void cacheDict() {
    EntityWrapper wrapper = this.newEntityWrapper();
    wrapper.eq("enable", CommonConstant.COMMON_YES);
    List<Dict> dicts = this.selectList(wrapper);
    Map<String, List<DictVO>> dictMap = new HashMap<>();
    for (Dict dict : dicts) {
        if (StringUtils.isEmpty(dict.getParent())) {
            continue;
        }
        //找到目录
        List<DictVO> subDictMap = dictMap.get(dict.getParent());
        if (subDictMap == null) {
            subDictMap = new ArrayList<>();
            dictMap.put(dict.getParent(), subDictMap);
        }
        DictVO vo = new DictVO();
        BeanUtils.copyProperties(dict, vo);
        subDictMap.add(vo);
    }

    Cache cache = cacheManager.getCache(CacheConstants.DICT_ITEM_CACHE_NAME);
    for (Map.Entry<String, List<DictVO>> entry : dictMap.entrySet()) {
        List<DictVO> children = entry.getValue();
        //排序处理
        children.sort((o1, o2) -> {
            if (o1.getOrderNo() == null) {
                return 1;
            }
            if (o2.getOrderNo() == null) {
                return -1;
            }
            return o1.getOrderNo() - o2.getOrderNo();
        });

        //添加到Mapper中
        HashMap<String,DictVO> subMap = new LinkedHashMap<>();
        for (DictVO dictVO : children) {
            subMap.put(dictVO.getCode(),dictVO);
        }

        cache.put(entry.getKey(), subMap);
    }
}
 
Example 18
Source File: CategoryManagerCacheWrapper.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void updateCategory(Category category) {
    Cache cache = this.getCache();
    cache.put(CATEGORY_CACHE_NAME_PREFIX + category.getCode(), category);
    this.checkRootModification(category, cache);
}
 
Example 19
Source File: CategoryManagerCacheWrapper.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void checkRootModification(Category category, Cache cache) {
    if (category.isRoot()) {
        cache.put(CATEGORY_ROOT_CACHE_NAME, category);
    }
}
 
Example 20
Source File: CacheInfoManager.java    From entando-core with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Put an object on the given cache.
 *
 * @param targetCache The cache name
 * @param key The key
 * @param obj The object to put into cache.
 */
public void putInCache(String targetCache, String key, Object obj) {
    Cache cache = this.getCache(targetCache);
    cache.put(key, obj);
}