下面列出了com.google.common.cache.Cache#invalidateAll ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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"));
}
@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();
}
}
/**
* 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;
}
@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();
}
}
/**
* 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();
}
/**
* Clear config cache
*/
protected void clearConfigCache() {
synchronized (this) {
for (Cache c : allCaches) {
if (c != null) {
c.invalidateAll();
}
}
m_configVersion.incrementAndGet();
}
}
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);
}
@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();
}
private void setupHotKeyListener() {
if(enableThrottleHotKeys.get()) {
poolManager.addEVCacheEventListener(this);
} else {
poolManager.removeEVCacheEventListener(this);
for(Cache<String, Integer> cache : cacheMap.values()) {
cache.invalidateAll();
}
}
}
public void remove(String cacheName, String key) {
Cache<String, Object> cache = getCacheHolder(cacheName);
if (cache != null) {
cache.invalidateAll();
}
}
public void clearAll() {
for (Cache<String, Object> cache : caches.values()) {
cache.invalidateAll();
}
}
private static final void invalidateCache(Cache<?, ?> cache) {
cache.invalidateAll();
cache.cleanUp();
}
public void remove(String cacheName,String key){
Cache<String, Object> cache = getCacheHolder(cacheName);
if(cache != null){
cache.invalidateAll();
}
}
public void clearAll(){
for (Cache<String, Object> cache : caches.values()) {
cache.invalidateAll();
}
}