Java Code Examples for com.google.common.cache.Cache#invalidateAll()

The following examples show how to use com.google.common.cache.Cache#invalidateAll() . 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: GuavaCache5.java    From blog with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    Cache<String,String> cache = CacheBuilder.newBuilder().build();
    Object value = new Object();
    cache.put("key1","value1");
    cache.put("key2","value2");
    cache.put("key3","value3");

    List<String> list = new ArrayList<String>();
    list.add("key1");
    list.add("key2");

    cache.invalidateAll(list);//批量清除list中全部key对应的记录
    System.out.println(cache.getIfPresent("key1"));
    System.out.println(cache.getIfPresent("key2"));
    System.out.println(cache.getIfPresent("key3"));
}
 
Example 2
Source File: RemoteDriverTest.java    From calcite-avatica with Apache License 2.0 6 votes vote down vote up
@Test public void testStatementLifecycle() throws Exception {
  ConnectionSpec.getDatabaseLock().lock();
  try (AvaticaConnection connection = (AvaticaConnection) getLocalConnection()) {
    Map<Integer, AvaticaStatement> clientMap = connection.statementMap;
    Cache<Integer, Object> serverMap = getLocalConnectionInternals()
        .getRemoteStatementMap(connection);
    // Other tests being run might leave statements in the cache.
    // The lock guards against more statements being cached during the test.
    serverMap.invalidateAll();
    assertEquals(0, clientMap.size());
    assertEquals(0, serverMap.size());
    Statement stmt = connection.createStatement();
    assertEquals(1, clientMap.size());
    assertEquals(1, serverMap.size());
    stmt.close();
    assertEquals(0, clientMap.size());
    assertEquals(0, serverMap.size());
  } finally {
    ConnectionSpec.getDatabaseLock().unlock();
  }
}
 
Example 3
Source File: TimelineMetricsHolder.java    From ambari-metrics with Apache License 2.0 5 votes vote down vote up
/**
 * Returns values from cache and clears the cache
 * @param cache
 * @param lock
 * @return
 */
private Map<String, TimelineMetrics> extractMetricsFromCacheWithLock(Cache<String, TimelineMetrics> cache, ReadWriteLock lock) {
  lock.writeLock().lock();
  Map<String, TimelineMetrics> metricsMap = new TreeMap<>(cache.asMap());
  cache.invalidateAll();
  lock.writeLock().unlock();
  return metricsMap;
}
 
Example 4
Source File: RemoteDriverTest.java    From calcite-avatica with Apache License 2.0 5 votes vote down vote up
@Test public void testConnectionIsolation() throws Exception {
  ConnectionSpec.getDatabaseLock().lock();
  try {
    Cache<String, Connection> connectionMap = getLocalConnectionInternals()
        .getRemoteConnectionMap((AvaticaConnection) getLocalConnection());
    // Other tests being run might leave connections in the cache.
    // The lock guards against more connections being cached during the test.
    connectionMap.invalidateAll();

    final String sql = "select * from (values (1, 'a'))";
    assertEquals("connection cache should start empty",
        0, connectionMap.size());
    Connection conn1 = getLocalConnection();
    Connection conn2 = getLocalConnection();
    assertEquals("we now have two connections open",
        2, connectionMap.size());
    PreparedStatement conn1stmt1 = conn1.prepareStatement(sql);
    assertEquals(
        "creating a statement does not cause new connection",
        2, connectionMap.size());
    PreparedStatement conn2stmt1 = conn2.prepareStatement(sql);
    assertEquals(
        "creating a statement does not cause new connection",
        2, connectionMap.size());
    AvaticaPreparedStatement s1 = (AvaticaPreparedStatement) conn1stmt1;
    AvaticaPreparedStatement s2 = (AvaticaPreparedStatement) conn2stmt1;
    assertFalse("connection id's should be unique",
        s1.handle.connectionId.equalsIgnoreCase(s2.handle.connectionId));
    conn2.close();
    assertEquals("closing a connection closes the server-side connection",
        1, connectionMap.size());
    conn1.close();
    assertEquals("closing a connection closes the server-side connection",
        0, connectionMap.size());
  } finally {
    ConnectionSpec.getDatabaseLock().unlock();
  }
}
 
Example 5
Source File: MCRCache.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Changes the capacity of this cache. This is the maximum number of objects that will be cached at a time. If the
 * new capacity is smaller than the current number of objects in the cache, the least recently used objects will be
 * removed from the cache.
 *
 * @param capacity
 *            the maximum number of objects this cache will hold
 */
public synchronized void setCapacity(long capacity) {
    this.capacity = capacity;
    Cache<K, MCRCacheEntry<V>> newCache = CacheBuilder.newBuilder().recordStats().maximumSize(capacity).build();
    newCache.putAll(backingCache.asMap());
    Cache<K, MCRCacheEntry<V>> oldCache = backingCache;
    backingCache = newCache;
    oldCache.invalidateAll();
}
 
Example 6
Source File: AbstractConfig.java    From apollo with Apache License 2.0 5 votes vote down vote up
/**
 * Clear config cache
 */
protected void clearConfigCache() {
  synchronized (this) {
    for (Cache c : allCaches) {
      if (c != null) {
        c.invalidateAll();
      }
    }
    m_configVersion.incrementAndGet();
  }
}
 
Example 7
Source File: DefaultCacheRegistry.java    From emodb with Apache License 2.0 5 votes vote down vote up
private void invalidate(InvalidationEvent event) {
    // Invalidate the actual cache.
    Cache<String, ?> cache = _cache.get();
    if (cache != null) {
        if (event.hasKeys()) {
            cache.invalidateAll(event.getKeys());
        } else {
            cache.invalidateAll();
        }
    }
    notifyListeners(event);
}
 
Example 8
Source File: MetaDataEndpointImpl.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public void clearCache(RpcController controller, ClearCacheRequest request,
        RpcCallback<ClearCacheResponse> done) {
    GlobalCache cache = GlobalCache.getInstance(this.env);
    Cache<ImmutableBytesPtr, PTable> metaDataCache =
            GlobalCache.getInstance(this.env).getMetaDataCache();
    metaDataCache.invalidateAll();
    cache.clearTenantCache();
}
 
Example 9
Source File: HotKeyListener.java    From EVCache with Apache License 2.0 5 votes vote down vote up
private void setupHotKeyListener() {
    if(enableThrottleHotKeys.get()) {
        poolManager.addEVCacheEventListener(this);
    } else {
        poolManager.removeEVCacheEventListener(this);
        for(Cache<String, Integer> cache : cacheMap.values()) {
            cache.invalidateAll();
        }
    }
}
 
Example 10
Source File: GuavaLevel1CacheProvider.java    From azeroth with Apache License 2.0 4 votes vote down vote up
public void remove(String cacheName, String key) {
    Cache<String, Object> cache = getCacheHolder(cacheName);
    if (cache != null) {
        cache.invalidateAll();
    }
}
 
Example 11
Source File: GuavaLevel1CacheProvider.java    From azeroth with Apache License 2.0 4 votes vote down vote up
public void clearAll() {
    for (Cache<String, Object> cache : caches.values()) {
        cache.invalidateAll();
    }
}
 
Example 12
Source File: S3FileSystem.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private static final void invalidateCache(Cache<?, ?> cache) {
  cache.invalidateAll();
  cache.cleanUp();
}
 
Example 13
Source File: GuavaLevel1CacheProvider.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
public void remove(String cacheName,String key){
	Cache<String, Object> cache = getCacheHolder(cacheName);
	if(cache != null){
		cache.invalidateAll();
	}
}
 
Example 14
Source File: GuavaLevel1CacheProvider.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
public void clearAll(){
	for (Cache<String, Object> cache : caches.values()) {
		cache.invalidateAll();
	}
}