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

The following examples show how to use org.springframework.cache.Cache#evict() . 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
@Override
public void flush(Collection<CacheTarget> cacheTargets) throws RiceIllegalArgumentException {
    if (CollectionUtils.isNotEmpty(cacheTargets)) {
        logCacheFlush(cacheTargets);
        for (CacheTarget cacheTarget : cacheTargets) {
            if (cacheTarget == null) {
                throw new RiceIllegalArgumentException("cacheTarget is null");
            }
            final Cache c = getCache(cacheTarget.getCache());
            if (c != null) {
                if (cacheTarget.containsKey()) {
                    c.evict(cacheTarget.getKey());
                } else {
                    c.clear();
                }
            }
        }
    }
}
 
Example 2
@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 3
@Test
public void evictTransactional() {
	Cache target = new ConcurrentMapCache("testCache");
	Cache cache = new TransactionAwareCacheDecorator(target);
	Object key = new Object();
	cache.put(key, "123");


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

	assertNull(target.get(key));
}
 
Example 4
@Test
public void evictTransactional() {
	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.evict(key);
	assertEquals("123", target.get(key, String.class));
	this.txManager.commit(status);

	assertNull(target.get(key));
}
 
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 testCache() throws IOException {

    SerializationUtils.init("fst", null);

    J2CacheConfig config = J2CacheConfig.initFromConfig("/j2cache.properties");
    J2CacheBuilder j2CacheBuilder = J2CacheBuilder.init(config);

    J2CacheSpringCacheManageAdapter cacheManageAdapter = new J2CacheSpringCacheManageAdapter(j2CacheBuilder, true);
    String cacheName = "ddd";
    Cache cache = cacheManageAdapter.getCache(cacheName);

    String key = "dddd";
    Cache.ValueWrapper valueWrapper = cache.get(key);

    Assert.assertNull(valueWrapper); // 第一次取  是null

    cache.put(key, null); // 存 null


    valueWrapper = cache.get(key);
    Assert.assertNull(valueWrapper); //允许存 null, 存 null 之后再取  是 null
    cache.evict(key); // 失效
    valueWrapper = cache.get(key);
    Assert.assertNull(valueWrapper); // 失效后再取  是null
}
 
Example 7
@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));
}
 
Example 8
protected void releaseCachedObjects(Cache cache) {
    List<String> codes = (List<String>) this.get(cache, DRAFT_PAGE_CODES_CACHE_NAME, List.class);
    if (null != codes) {
        for (int i = 0; i < codes.size(); i++) {
            String code = codes.get(i);
            cache.evict(DRAFT_PAGE_CACHE_NAME_PREFIX + code);
            cache.evict(ONLINE_PAGE_CACHE_NAME_PREFIX + code);
        }
        cache.evict(DRAFT_PAGE_CODES_CACHE_NAME);
        cache.evict(ONLINE_PAGE_CODES_CACHE_NAME);
    }
}
 
Example 9
/**
 * Execute {@link Cache#evict(Object)} on the specified {@link Cache} and
 * invoke the error handler if an exception occurs.
 */
protected void doEvict(Cache cache, Object key) {
	try {
		cache.evict(key);
	}
	catch (RuntimeException ex) {
		getErrorHandler().handleCacheEvictError(ex, cache, key);
	}
}
 
Example 10
protected void releaseCachedObjects(Cache cache, String listKey, String prefixKey) {
	List<String> codes = (List<String>) this.get(cache, listKey, List.class);
	if (null != codes) {
		for (String code : codes) {
			cache.evict(prefixKey + code);
		}
		cache.evict(listKey);
	}
}
 
Example 11
private void releaseObjects(Cache cache, List<String> keysToRelease) {
    if (null != keysToRelease) {
        for (String code : keysToRelease) {
            cache.evict(this.getCacheKeyPrefix() + code);
        }
    }
}
 
Example 12
@EventListener(classes = RolloutDeletedEvent.class)
void invalidateCachedTotalTargetCountOnRolloutDelete(final RolloutDeletedEvent event) {
    final Cache cache = tenantAware.runAsTenant(event.getTenant(), () -> cacheManager.getCache(CACHE_RO_NAME));
    cache.evict(event.getEntityId());
}
 
Example 13
@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());

	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 14
@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
public void evictCache(String cacheName,String key) {
    Cache cache = cacheManager.getCache(cacheName);
    if (cache != null) {
        cache.evict(key);
    }
}
 
Example 16
@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());

	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 17
@EventListener(classes = RolloutGroupDeletedEvent.class)
void invalidateCachedTotalTargetCountOnRolloutGroupDelete(final RolloutGroupDeletedEvent event) {
    final Cache cache = tenantAware.runAsTenant(event.getTenant(), () -> cacheManager.getCache(CACHE_GR_NAME));
    cache.evict(event.getEntityId());
}
 
Example 18
@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 19
private void releaseCachedObjects(Cache cache) {
	cache.evict(IKeyGeneratorManagerCacheWrapper.CURRENT_KEY);
}
 
Example 20
public void flushAll(String cacheName) {
    Cache cacheOfGroup = this.getCache(CACHE_INFO_MANAGER_CACHE_NAME);
    cacheOfGroup.evict(GROUP_CACHE_NAME_PREFIX + cacheName);
    Cache cache = this.getCache(cacheName);
    cache.clear();
}