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

The following examples show how to use org.springframework.cache.Cache#clear() . 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
public void clearNamedCache(String cacheName) {

        try {
            CacheManager cm = CoreImplServiceLocator.getCacheManagerRegistry().getCacheManagerByCacheName(cacheName);
            if ( cm != null ) {
                Cache cache = cm.getCache(cacheName);
                if (cache != null) {
                    cache.clear();
                    if ( LOG.isDebugEnabled() ) {
                        LOG.debug( "Cleared " + cacheName + " cache." );
                    }
                } else {
                    // this is at debug level intentionally, since not all BOs have caches
                    LOG.debug( "Unable to find cache for " + cacheName + ".");
                }
            } else {
                LOG.info( "Unable to find cache manager when attempting to clear " + cacheName );
            }
        } catch (RiceIllegalArgumentException e) {
            LOG.info( "Cache manager not found when attempting to clear " + cacheName );
        }

    }
 
Example 2
@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 3
@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 4
/**
 * 缓存刷新
 */
public synchronized void update() {
    //缓存AB切换
    change();

    //加载数据
    Map<String, Object> datas = init();
    if (datas != null && !datas.isEmpty()) {
        for (Map.Entry<String, Object> item : datas.entrySet()) {
            put(item.getKey(), item.getValue());
        }
    }

    //缓存切换
    current = switchA;
    //老缓存清理
    Cache cache = ehcache.getCache(switchB);
    if (cache != null) {
        cache.clear();
    }
}
 
Example 5
@AfterReturning ("@annotation(fr.gael.dhus.spring.cache.IncrementCache)")
public void updateCache (JoinPoint joinPoint)
{
   IncrementCache annotation = ((MethodSignature) joinPoint.getSignature ())
         .getMethod ().getAnnotation (IncrementCache.class);

   Cache cache = cacheManager.getCache (annotation.name ());
   if (cache != null)
   {
      synchronized (cache)
      {
         Integer old_value = cache.get (annotation.key (), Integer.class);
         cache.clear ();
         if (old_value == null)
         {
            return;
         }
         cache.put (annotation.key (), (old_value + annotation.value ()));
      }
   }

}
 
Example 6
Source Project: POC   File: CustomerControllerITTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void test10_validateCache() {
	final Cache customersCache = this.cacheManager.getCache("customer");
	assertThat(customersCache).isNotNull();
	customersCache.clear(); // Simple test assuming the cache is empty
	final Long customerId = getCustomerIdByFirstName("Raja");
	assertThat(customersCache.get(customerId)).isNull();
	final ResponseEntity<Customer> response = userRestTemplate()
			.getForEntity(String.format("%s/%s", BASE_URL, customerId), Customer.class);
	final Customer customer = response.getBody();
	assertThat(customer).isNotNull();
	final Customer cachedCustomer = (Customer) Objects.requireNonNull(customersCache.get(customerId)).get();
	assertThat(cachedCustomer).isNotNull();
	assertThat(cachedCustomer.getFirstName()).isEqualTo(customer.getFirstName());
	assertThat(cachedCustomer.getLastName()).isEqualTo(customer.getLastName());
	assertThat(cachedCustomer.getDateOfBirth()).isEqualTo(customer.getDateOfBirth());
	assertThat(cachedCustomer.getAddress().getCounty()).isEqualTo(customer.getAddress().getCounty());
	assertThat(cachedCustomer.getOrders()).isNotEmpty().hasSize(1);
	Order order = cachedCustomer.getOrders().get(0);
	assertThat(order).isNotNull();
	assertThat(order.getOrderId()).isPositive();
	assertThat(order.getOrderNumber()).isEqualTo("ORD1");
	assertThat(order.getOrderStatus()).isEqualTo(OrderStatus.IN_PROCESS);

}
 
Example 7
@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 8
/**
 * Execute {@link Cache#clear()} on the specified {@link Cache} and
 * invoke the error handler if an exception occurs.
 */
protected void doClear(Cache cache) {
	try {
		cache.clear();
	}
	catch (RuntimeException ex) {
		getErrorHandler().handleCacheClearError(ex, cache);
	}
}
 
Example 9
@Test
public void regularOperationsOnTarget() {
	Cache target = new ConcurrentMapCache("testCache");
	Cache cache = new TransactionAwareCacheDecorator(target);
	assertEquals(target.getName(), cache.getName());
	assertEquals(target.getNativeCache(), cache.getNativeCache());

	Object key = new Object();
	target.put(key, "123");
	assertEquals("123", cache.get(key).get());
	assertEquals("123", cache.get(key, String.class));

	cache.clear();
	assertNull(target.get(key));
}
 
Example 10
@Test
public void clearNonTransactional() {
	Cache target = new ConcurrentMapCache("testCache");
	Cache cache = new TransactionAwareCacheDecorator(target);
	Object key = new Object();
	cache.put(key, "123");

	cache.clear();
	assertNull(target.get(key));
}
 
Example 11
Source Project: WeBASE-Sign   File: UserService.java    License: Apache License 2.0 5 votes vote down vote up
public Boolean deleteAllCredentialCache() {
    log.info("delete all Credential cache");

    Cache cache = cacheManager.getCache("getCredentials");
    if(cache!=null) {
        cache.clear();
    }
    return true;
}
 
Example 12
@Test
public void regularOperationsOnTarget() {
	Cache target = new ConcurrentMapCache("testCache");
	Cache cache = new TransactionAwareCacheDecorator(target);
	assertEquals(target.getName(), cache.getName());
	assertEquals(target.getNativeCache(), cache.getNativeCache());

	Object key = new Object();
	target.put(key, "123");
	assertEquals("123", cache.get(key).get());
	assertEquals("123", cache.get(key, String.class));

	cache.clear();
	assertNull(target.get(key));
}
 
Example 13
@Test
public void clearNonTransactional() {
	Cache target = new ConcurrentMapCache("testCache");
	Cache cache = new TransactionAwareCacheDecorator(target);
	Object key = new Object();
	cache.put(key, "123");

	cache.clear();
	assertNull(target.get(key));
}
 
Example 14
/**
 * Perform cache flush if this is transient entity.
 *
 * @param object object about to be persisted
 *
 * @return true if caches were flushed
 */
protected boolean flushCachesIfTransient(final Object object) {
    if (isTransientEntity(object)) {
        for (final String cacheName : cachesToFlushForTransientEntities) {
            final Cache cache = cacheManager.getCache(cacheName);
            cache.clear();
        }
        return true;
    }
    return false;
}
 
Example 15
private void clearCache(String cacheName) {
    DistributedCacheManagerDecorator distributedCacheManagerDecorator =
            GlobalResourceLoader.getService(KewImplConstants.KEW_DISTRIBUTED_CACHE_MANAGER);

    Cache cache = distributedCacheManagerDecorator.getCache(cacheName);
    if (cache != null) {
        cache.clear();
    }
}
 
Example 16
public void testUnlessExpression(CacheableService<?> service) throws Exception {
	Cache cache = cm.getCache("testCache");
	cache.clear();
	service.unless(10);
	service.unless(11);
	assertThat(cache.get(10).get(), equalTo((Object) 10L));
	assertThat(cache.get(11), nullValue());
}
 
Example 17
@Test
public void regularOperationsOnTarget() {
	Cache target = new ConcurrentMapCache("testCache");
	Cache cache = new TransactionAwareCacheDecorator(target);
	assertEquals(target.getName(), cache.getName());
	assertEquals(target.getNativeCache(), cache.getNativeCache());

	Object key = new Object();
	target.put(key, "123");
	assertEquals("123", cache.get(key).get());
	assertEquals("123", cache.get(key, String.class));

	cache.clear();
	assertNull(target.get(key));
}
 
Example 18
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 19
Source Project: yes-cart   File: BaseCoreDBTestCase.java    License: Apache License 2.0 4 votes vote down vote up
protected void clearCache() {
    for (Cache cache : getCacheMap().values()) {
        cache.clear();
    }
}
 
Example 20
Source Project: yes-cart   File: CacheDirectorImpl.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public int onCacheableBulkChange(final String entityOperation, final String entityName, final Long[] pkValues) {

    int cnt = 0;

    final Set<Pair<String, String>> cacheNames = resolveCacheNames(entityOperation, entityName);

    if (cacheNames != null) {

        final CacheManager cm = getCacheManager();

        for (Pair<String, String> cacheStrategy : cacheNames) {

            final Cache cache = cm.getCache(cacheStrategy.getFirst());

            if (cache != null) {

                if("all".equals(cacheStrategy.getSecond())) {

                    cache.clear();

                    cnt += pkValues.length;

                } else if("key".equals(cacheStrategy.getSecond())) {

                    for (final Long pkValue : pkValues) {

                        cache.evict(pkValue);

                        cnt++;

                    }

                } else {

                    LOG.warn("The [{}] cache eviction strategy not supported", cacheStrategy.getSecond());

                }

            }

        }

    }

    return cnt;
}