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

The following examples show how to use org.springframework.cache.Cache#evict() . 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: TestJ2CacheSpringCacheManageAdapter.java    From J2Cache with Apache License 2.0 6 votes vote down vote up
@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 2
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 3
Source File: TransactionAwareCacheDecoratorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 4
Source File: CaffeineCacheManagerTests.java    From spring-analysis-note 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 5
Source File: CacheAdminServiceImpl.java    From rice with Educational Community License v2.0 6 votes vote down vote up
@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 6
Source File: TransactionAwareCacheDecoratorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 7
Source File: SeoMappingCacheWrapper.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 8
Source File: AbstractGenericCacheWrapper.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void releaseObjects(Cache cache, List<String> keysToRelease) {
    if (null != keysToRelease) {
        for (String code : keysToRelease) {
            cache.evict(this.getCacheKeyPrefix() + code);
        }
    }
}
 
Example 9
Source File: AbstractCacheInvoker.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * 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
Source File: PageManagerCacheWrapper.java    From entando-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 11
Source File: TransactionAwareCacheDecoratorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 12
Source File: CacheManagerUtil.java    From mall4j with GNU Affero General Public License v3.0 4 votes vote down vote up
public void evictCache(String cacheName,String key) {
    Cache cache = cacheManager.getCache(cacheName);
    if (cache != null) {
        cache.evict(key);
    }
}
 
Example 13
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 14
Source File: ConcurrentMapCacheManagerTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@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 15
Source File: RolloutStatusCache.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
@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 16
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 17
Source File: ConcurrentMapCacheManagerTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@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 18
Source File: KeyGeneratorManagerCacheWrapper.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void releaseCachedObjects(Cache cache) {
	cache.evict(IKeyGeneratorManagerCacheWrapper.CURRENT_KEY);
}
 
Example 19
Source File: CacheInfoManager.java    From entando-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
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();
}
 
Example 20
Source File: RolloutStatusCache.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
@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());
}