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

The following examples show how to use org.springframework.cache.Cache#clear() . 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: KIMTestCase.java    From rice with Educational Community License v2.0 6 votes vote down vote up
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
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 3
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 4
Source File: AbstractLocalCacheLoader.java    From chronus with Apache License 2.0 6 votes vote down vote up
/**
 * 缓存刷新
 */
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
Source File: CacheAspectDefinition.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@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 File: CustomerControllerITTest.java    From POC with 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
Source File: TransactionAwareCacheDecoratorTests.java    From java-technology-stack 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 8
Source File: TransactionAwareCacheDecoratorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 9
Source File: TransactionAwareCacheDecoratorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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
Source File: AbstractCacheAnnotationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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 11
Source File: RuleServiceInternalImpl.java    From rice with Educational Community License v2.0 5 votes vote down vote up
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 12
Source File: CacheableImpexFederationFacadeImpl.java    From yes-cart with Apache License 2.0 5 votes vote down vote up
/**
 * 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 13
Source File: TransactionAwareCacheDecoratorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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
Source File: AbstractCacheInvoker.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * 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 15
Source File: UserService.java    From WeBASE-Sign with 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 16
Source File: TransactionAwareCacheDecoratorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 17
Source File: TransactionAwareCacheDecoratorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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
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 19
Source File: BaseCoreDBTestCase.java    From yes-cart with Apache License 2.0 4 votes vote down vote up
protected void clearCache() {
    for (Cache cache : getCacheMap().values()) {
        cache.clear();
    }
}
 
Example 20
Source File: CacheDirectorImpl.java    From yes-cart with 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;
}