下面列出了怎么用com.google.common.cache.RemovalListener的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Initialize container reports cache that sent from datanodes.
*/
@SuppressWarnings("UnstableApiUsage")
private Cache<String, ContainerStat> buildContainerReportCache() {
return
CacheBuilder.newBuilder()
.expireAfterAccess(Long.MAX_VALUE, TimeUnit.MILLISECONDS)
.maximumSize(Integer.MAX_VALUE)
.removalListener((
RemovalListener<String, ContainerStat>) removalNotification -> {
synchronized (containerReportCache) {
ContainerStat stat = removalNotification.getValue();
if (stat != null) {
// TODO: Are we doing the right thing here?
// remove invalid container report
metrics.decrContainerStat(stat);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Remove expired container stat entry for " +
"datanode: {}.", removalNotification.getKey());
}
}
})
.build();
}
private SourceManager(KylinConfig config) {
this.systemConfig = config;
this.sourceMap = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.DAYS)
.removalListener(new RemovalListener<String, ISource>() {
@Override
public void onRemoval(RemovalNotification<String, ISource> entry) {
ISource s = entry.getValue();
if (s != null) {
try {
s.close();
} catch (Throwable e) {
logger.error("Failed to close ISource: {}", s.getClass().getName(), e);
}
}
}
}).build();
}
private SnapshotManager(KylinConfig config) {
this.config = config;
this.snapshotCache = CacheBuilder.newBuilder().removalListener(new RemovalListener<String, SnapshotTable>() {
@Override
public void onRemoval(RemovalNotification<String, SnapshotTable> notification) {
SnapshotManager.logger.info("Snapshot with resource path {} is removed due to {}",
notification.getKey(), notification.getCause());
}
}).maximumSize(config.getCachedSnapshotMaxEntrySize())//
.expireAfterWrite(1, TimeUnit.DAYS).build(new CacheLoader<String, SnapshotTable>() {
@Override
public SnapshotTable load(String key) throws Exception {
SnapshotTable snapshotTable = SnapshotManager.this.load(key, true);
return snapshotTable;
}
});
}
private ExtTableSnapshotInfoManager(KylinConfig config) {
this.config = config;
this.snapshotCache = CacheBuilder.newBuilder().removalListener(new RemovalListener<String, ExtTableSnapshotInfo>() {
@Override
public void onRemoval(RemovalNotification<String, ExtTableSnapshotInfo> notification) {
ExtTableSnapshotInfoManager.logger.info("Snapshot with resource path " + notification.getKey()
+ " is removed due to " + notification.getCause());
}
}).maximumSize(1000)//
.expireAfterWrite(1, TimeUnit.DAYS).build(new CacheLoader<String, ExtTableSnapshotInfo>() {
@Override
public ExtTableSnapshotInfo load(String key) throws Exception {
ExtTableSnapshotInfo snapshot = ExtTableSnapshotInfoManager.this.load(key);
return snapshot;
}
});
}
private void init() {
this.basePath = getCacheBasePath(config);
this.maxCacheSizeInKB = (long) (config.getExtTableSnapshotLocalCacheMaxSizeGB() * 1024 * 1024);
this.tablesCache = CacheBuilder.newBuilder().removalListener(new RemovalListener<String, CachedTableInfo>() {
@Override
public void onRemoval(RemovalNotification<String, CachedTableInfo> notification) {
logger.warn(notification.getValue() + " is removed " + "because of " + notification.getCause());
notification.getValue().cleanStorage();
}
}).maximumWeight(maxCacheSizeInKB).weigher(new Weigher<String, CachedTableInfo>() {
@Override
public int weigh(String key, CachedTableInfo value) {
return value.getSizeInKB();
}
}).build();
restoreCacheState();
cacheStateChecker = new CacheStateChecker();
initExecutors();
}
private DictionaryManager(KylinConfig config) {
this.config = config;
this.dictCache = CacheBuilder.newBuilder()//
.softValues()//
.removalListener(new RemovalListener<String, DictionaryInfo>() {
@Override
public void onRemoval(RemovalNotification<String, DictionaryInfo> notification) {
DictionaryManager.logger.info("Dict with resource path {} is removed due to {}",
notification.getKey(), notification.getCause());
}
})//
.maximumSize(config.getCachedDictMaxEntrySize())//
.expireAfterWrite(1, TimeUnit.DAYS).build(new CacheLoader<String, DictionaryInfo>() {
@Override
public DictionaryInfo load(String key) throws Exception {
DictionaryInfo dictInfo = DictionaryManager.this.load(key, true);
if (dictInfo == null) {
return NONE_INDICATOR;
} else {
return dictInfo;
}
}
});
}
@Inject
public PlatformAlarmIncidentService(
SubsystemRegistry registry,
AlarmIncidentDAO incidentDao,
AlarmIncidentHistoryListener historyListener,
PlatformMessageBus platformBus,
PlacePopulationCacheManager populationCacheMgr
) {
super(registry, incidentDao, historyListener, platformBus, populationCacheMgr);
this.platformBus = platformBus;
this.cancelCache = CacheBuilder.newBuilder()
.recordStats()
.concurrencyLevel(cancelTimeoutConcurrency)
.expireAfterWrite(cancelTimeoutSeconds, TimeUnit.SECONDS)
.removalListener((RemovalListener<String, SettableFuture<Void>>) notification -> {
if(notification.wasEvicted() && notification.getValue() != null) {
notification.getValue().setException(new TimeoutException());
}
})
.build();
cleanUpScheduler.scheduleAtFixedRate(cancelCache::cleanUp, 0, cancelCleanupSecs, TimeUnit.SECONDS);
}
/**
*
*/
public FileWatcherRegistry() {
this.watchers =
CacheBuilder
.newBuilder()
.removalListener(new RemovalListener<Path, FileWatcherRegistry.Watcher>() {
@Override
public void onRemoval(RemovalNotification<Path, Watcher> notification) {
notification.getValue().stop();
}
})
.build(new CacheLoader<Path, Watcher>() {
@Override
public FileWatcherRegistry.Watcher load(Path key) throws Exception {
return watch(key);
}
});
this.executor =
new ThreadPoolBuilder()
.withNameFormat("resource-watcher-%d")
.withMetrics("resource.watcher")
.build();
}
@Override
protected void validate() throws Exception {
clientGroupCache = CacheBuilder.newBuilder()
.expireAfterAccess(config.getChannelMaxIdleTime(), TimeUnit.MILLISECONDS)
.removalListener(new RemovalListener<BrokerNode, ClientGroup>() {
@Override
public void onRemoval(RemovalNotification<BrokerNode, ClientGroup> removalNotification) {
try {
removalNotification.getValue().stop();
} catch (Exception e) {
logger.error("close client exception, address: {}, error: {}", removalNotification.getKey().getHost(), e.getMessage());
logger.debug("close client exception, address: {}", removalNotification.getKey().getHost(), e);
}
}
})
.build();
}
private void buildChannelCache() {
this.channelCache = CacheBuilder.newBuilder()
.maximumSize(rqdCacheSize)
.expireAfterAccess(rqdCacheExpiration, TimeUnit.MINUTES)
.removalListener(new RemovalListener<String, ManagedChannel>() {
@Override
public void onRemoval(RemovalNotification<String, ManagedChannel> removal){
ManagedChannel conn = removal.getValue();
conn.shutdown();
}
})
.build(
new CacheLoader<String, ManagedChannel>() {
@Override
public ManagedChannel load(String host) throws Exception {
ManagedChannelBuilder channelBuilder = ManagedChannelBuilder.forAddress(
host, rqdServerPort).usePlaintext();
return channelBuilder.build();
}
});
}
public static void main(String[] args) throws InterruptedException {
RemovalListener<String, String> listener = new RemovalListener<String, String>() {
public void onRemoval(RemovalNotification<String, String> notification) {
System.out.println("[" + notification.getKey() + ":" + notification.getValue() + "] is removed!");
}
};
Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(3).removalListener(listener).build();
Object value = new Object();
cache.put("key1", "value1");
cache.put("key2", "value2");
cache.put("key3", "value3");
cache.put("key4", "value3");
cache.put("key5", "value3");
cache.put("key6", "value3");
cache.put("key7", "value3");
cache.put("key8", "value3");
}
public void getNameLoadingCache(String name) throws Exception{
LoadingCache<String, String> cache = CacheBuilder.newBuilder()
//设置大小,条目数
.maximumSize(20)
//设置失效时间,创建时间
.expireAfterWrite(20, TimeUnit.SECONDS)
//设置时效时间,最后一次被访问
.expireAfterAccess(20, TimeUnit.HOURS)
//移除缓存的监听器
.removalListener(new RemovalListener<String, String>() {
public void onRemoval(RemovalNotification<String, String> notification) {
System.out.println("有缓存数据被移除了");
}})
//缓存构建的回调
.build(new CacheLoader<String, String>(){//加载缓存
@Override
public String load(String key) throws Exception {
return key + "-" + "iamzhongyong";
}
});
System.out.println(cache.get(name));
cache.invalidateAll();
}
/**
* 不需要延迟处理(泛型的方式封装)
* @return
*/
public <K , V> LoadingCache<K , V> cached(CacheLoader<K , V> cacheLoader) {
LoadingCache<K , V> cache = CacheBuilder
.newBuilder()
.maximumSize(2)
.weakKeys()
.softValues()
.refreshAfterWrite(120, TimeUnit.SECONDS)
.expireAfterWrite(10, TimeUnit.MINUTES)
.removalListener(new RemovalListener<K, V>(){
@Override
public void onRemoval(RemovalNotification<K, V> rn) {
System.out.println(rn.getKey()+"被移除");
}})
.build(cacheLoader);
return cache;
}
private LoadingCache<Long, Page<T>> buildCache(int size) {
return CacheBuilder.newBuilder().concurrencyLevel(1).initialCapacity(size).maximumSize(size)
.removalListener(new RemovalListener<Long, Page<T>>() {
@Override
public void onRemoval(RemovalNotification<Long, Page<T>> notification) {
m_recentlyExpiredPagesCache.get().put(notification.getKey(), true);
}
}).build(new CacheLoader<Long, Page<T>>() {
@Override
public Page<T> load(Long pageNo) throws Exception {
return new Page<>(pageNo, m_pageSize, m_pageLoadIntervalMillis);
}
});
}
public KeyProviderCache(long expiryMs) {
cache = CacheBuilder.newBuilder()
.expireAfterAccess(expiryMs, TimeUnit.MILLISECONDS)
.removalListener(new RemovalListener<URI, KeyProvider>() {
@Override
public void onRemoval(
RemovalNotification<URI, KeyProvider> notification) {
try {
notification.getValue().close();
} catch (Throwable e) {
LOG.error(
"Error closing KeyProvider with uri ["
+ notification.getKey() + "]", e);
;
}
}
})
.build();
}
/**
* Create a new KMSAudit.
*
* @param windowMs Duplicate events within the aggregation window are quashed
* to reduce log traffic. A single message for aggregated
* events is printed at the end of the window, along with a
* count of the number of aggregated events.
*/
KMSAudit(long windowMs) {
cache = CacheBuilder.newBuilder()
.expireAfterWrite(windowMs, TimeUnit.MILLISECONDS)
.removalListener(
new RemovalListener<String, AuditEvent>() {
@Override
public void onRemoval(
RemovalNotification<String, AuditEvent> entry) {
AuditEvent event = entry.getValue();
if (event.getAccessCount().get() > 0) {
KMSAudit.this.logEvent(event);
event.getAccessCount().set(0);
KMSAudit.this.cache.put(entry.getKey(), event);
}
}
}).build();
executor = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder()
.setDaemon(true).setNameFormat(KMS_LOGGER_NAME + "_thread").build());
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
cache.cleanUp();
}
}, windowMs / 10, windowMs / 10, TimeUnit.MILLISECONDS);
}
public static LoadingCache create(DigestUtil digestUtil) {
return CacheBuilder.newBuilder()
.expireAfterAccess(10, TimeUnit.MINUTES)
.removalListener(
new RemovalListener<String, Instance>() {
@Override
public void onRemoval(RemovalNotification<String, Instance> notification) {
stopInstance(notification.getValue());
}
})
.build(
new CacheLoader<String, Instance>() {
@Override
public Instance load(String worker) {
return newStubInstance(worker, digestUtil);
}
});
}
public VertexCache(long maxCacheSize, int concurrencyLevel, int initialDirtySize) {
volatileVertices = new ConcurrentHashMap<>(initialDirtySize);
cache = CacheBuilder.newBuilder()
.maximumSize(maxCacheSize)
.concurrencyLevel(concurrencyLevel)
.removalListener((RemovalListener<Long, InternalVertex>) notification -> {
if (notification.getCause() == RemovalCause.EXPLICIT) { //Due to invalidation at the end
return;
}
//We get here if the entry is evicted because of size constraint or replaced through add
//i.e. RemovalCause.SIZE or RemovalCause.REPLACED
InternalVertex v = notification.getValue();
if (((AbstractVertex) v).isTxOpen() && (v.isModified() || v.isRemoved())) { //move vertex to volatile map if we cannot lose track of it
volatileVertices.putIfAbsent(notification.getKey(), v);
}
})
.build();
}
/**
* Constructs ZKDiscoveryService using the provided zookeeper client for storing service registry under namespace.
* @param zkClient of zookeeper quorum
* @param namespace under which the service registered would be stored in zookeeper.
* If namespace is {@code null}, no namespace will be used.
*/
public ZKDiscoveryService(ZKClient zkClient, String namespace) {
this.closed = new AtomicBoolean();
this.discoverables = HashMultimap.create();
this.lock = new ReentrantLock();
this.retryExecutor = Executors.newSingleThreadScheduledExecutor(
Threads.createDaemonThreadFactory("zk-discovery-retry"));
this.zkClient = namespace == null ? zkClient : ZKClients.namespace(zkClient, namespace);
this.services = CacheBuilder.newBuilder()
.removalListener(new RemovalListener<String, ServiceDiscoveredCacheEntry>() {
@Override
public void onRemoval(RemovalNotification<String, ServiceDiscoveredCacheEntry> notification) {
ServiceDiscoveredCacheEntry entry = notification.getValue();
if (entry != null) {
entry.cancel();
}
}
})
.build(createServiceLoader());
this.watcherCancellable = this.zkClient.addConnectionWatcher(createConnectionWatcher());
}
public KeyProviderCache(long expiryMs) {
cache = CacheBuilder.newBuilder()
.expireAfterAccess(expiryMs, TimeUnit.MILLISECONDS)
.removalListener(new RemovalListener<URI, KeyProvider>() {
@Override
public void onRemoval(
RemovalNotification<URI, KeyProvider> notification) {
try {
notification.getValue().close();
} catch (Throwable e) {
LOG.error(
"Error closing KeyProvider with uri ["
+ notification.getKey() + "]", e);
;
}
}
})
.build();
}
/**
* Create a new KMSAudit.
*
* @param windowMs Duplicate events within the aggregation window are quashed
* to reduce log traffic. A single message for aggregated
* events is printed at the end of the window, along with a
* count of the number of aggregated events.
*/
KMSAudit(long windowMs) {
cache = CacheBuilder.newBuilder()
.expireAfterWrite(windowMs, TimeUnit.MILLISECONDS)
.removalListener(
new RemovalListener<String, AuditEvent>() {
@Override
public void onRemoval(
RemovalNotification<String, AuditEvent> entry) {
AuditEvent event = entry.getValue();
if (event.getAccessCount().get() > 0) {
KMSAudit.this.logEvent(event);
event.getAccessCount().set(0);
KMSAudit.this.cache.put(entry.getKey(), event);
}
}
}).build();
executor = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder()
.setDaemon(true).setNameFormat(KMS_LOGGER_NAME + "_thread").build());
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
cache.cleanUp();
}
}, windowMs / 10, windowMs / 10, TimeUnit.MILLISECONDS);
}
SegmentStoreConnectionManager(final ConnectionFactory clientCF) {
this.cache = CacheBuilder.newBuilder()
.maximumSize(Config.HOST_STORE_CONTAINER_COUNT)
// if a host is not accessed for 5 minutes, remove it from the cache
.expireAfterAccess(5, TimeUnit.MINUTES)
.removalListener((RemovalListener<PravegaNodeUri, SegmentStoreConnectionPool>) removalNotification -> {
// Whenever a connection manager is evicted from the cache call shutdown on it.
removalNotification.getValue().shutdown();
})
.build(new CacheLoader<PravegaNodeUri, SegmentStoreConnectionPool>() {
@Override
@ParametersAreNonnullByDefault
public SegmentStoreConnectionPool load(PravegaNodeUri nodeUri) {
return new SegmentStoreConnectionPool(nodeUri, clientCF);
}
});
}
@VisibleForTesting
public PeriodicWatermarking(StreamMetadataStore streamMetadataStore, BucketStore bucketStore,
Function<Stream, WatermarkClient> watermarkClientSupplier, ScheduledExecutorService executor) {
this.streamMetadataStore = streamMetadataStore;
this.bucketStore = bucketStore;
this.executor = executor;
this.watermarkClientCache = CacheBuilder.newBuilder()
.maximumSize(MAX_CACHE_SIZE)
.expireAfterAccess(10, TimeUnit.MINUTES)
.removalListener((RemovalListener<Stream, WatermarkClient>) notification -> {
notification.getValue().client.close();
})
.build(new CacheLoader<Stream, WatermarkClient>() {
@ParametersAreNonnullByDefault
@Override
public WatermarkClient load(final Stream stream) {
return watermarkClientSupplier.apply(stream);
}
});
}
public GrpcClient(final Endpoint address, final SharedResources sharedResources, final ISettings settings) {
this.address = address;
this.settings = settings;
this.grpcExecutor = sharedResources.getClientChannelExecutor();
this.backgroundExecutor = sharedResources.getBackgroundExecutor();
this.eventLoopGroup = settings.getUseInProcessTransport() ? null : sharedResources.getEventLoopGroup();
final RemovalListener<Endpoint, Channel> removalListener =
removal -> shutdownChannel((ManagedChannel) removal.getValue());
this.channelMap = CacheBuilder.newBuilder()
.expireAfterAccess(30, TimeUnit.SECONDS)
.removalListener(removalListener)
.build(new CacheLoader<Endpoint, Channel>() {
@Override
public Channel load(final Endpoint endpoint) {
return getChannel(endpoint);
}
});
}
@Inject
public NetflowV9CodecAggregator() {
// TODO customize
this.templateCache = CacheBuilder.newBuilder()
.maximumSize(5000)
.removalListener(notification -> LOG.debug("Removed {} from template cache for reason {}", notification.getKey(), notification.getCause()))
.recordStats()
.build();
this.packetCache = CacheBuilder.newBuilder()
.expireAfterWrite(1, TimeUnit.MINUTES)
.maximumWeight(Size.megabytes(1).toBytes())
.removalListener((RemovalListener<TemplateKey, Queue<PacketBytes>>) notification -> LOG.debug("Removed {} from packet cache for reason {}", notification.getKey(), notification.getCause()))
.weigher((key, value) -> value.stream().map(PacketBytes::readableBytes).reduce(0, Integer::sum))
.recordStats()
.build();
}
private SnapshotManager(KylinConfig config) {
this.config = config;
this.snapshotCache = CacheBuilder.newBuilder().removalListener(new RemovalListener<String, SnapshotTable>() {
@Override
public void onRemoval(RemovalNotification<String, SnapshotTable> notification) {
SnapshotManager.logger.info("Snapshot with resource path {} is removed due to {}",
notification.getKey(), notification.getCause());
}
}).maximumSize(config.getCachedSnapshotMaxEntrySize())//
.expireAfterWrite(1, TimeUnit.DAYS).build(new CacheLoader<String, SnapshotTable>() {
@Override
public SnapshotTable load(String key) throws Exception {
SnapshotTable snapshotTable = SnapshotManager.this.load(key, true);
return snapshotTable;
}
});
}
private ExtTableSnapshotInfoManager(KylinConfig config) {
this.config = config;
this.snapshotCache = CacheBuilder.newBuilder().removalListener(new RemovalListener<String, ExtTableSnapshotInfo>() {
@Override
public void onRemoval(RemovalNotification<String, ExtTableSnapshotInfo> notification) {
ExtTableSnapshotInfoManager.logger.info("Snapshot with resource path " + notification.getKey()
+ " is removed due to " + notification.getCause());
}
}).maximumSize(1000)//
.expireAfterWrite(1, TimeUnit.DAYS).build(new CacheLoader<String, ExtTableSnapshotInfo>() {
@Override
public ExtTableSnapshotInfo load(String key) throws Exception {
ExtTableSnapshotInfo snapshot = ExtTableSnapshotInfoManager.this.load(key);
return snapshot;
}
});
}
private DictionaryManager(KylinConfig config) {
this.config = config;
this.dictCache = CacheBuilder.newBuilder()//
.softValues()//
.removalListener(new RemovalListener<String, DictionaryInfo>() {
@Override
public void onRemoval(RemovalNotification<String, DictionaryInfo> notification) {
DictionaryManager.logger.info("Dict with resource path {} is removed due to {}",
notification.getKey(), notification.getCause());
}
})//
.maximumSize(config.getCachedDictMaxEntrySize())//
.expireAfterWrite(1, TimeUnit.DAYS).build(new CacheLoader<String, DictionaryInfo>() {
@Override
public DictionaryInfo load(String key) throws Exception {
DictionaryInfo dictInfo = DictionaryManager.this.load(key, true);
if (dictInfo == null) {
return NONE_INDICATOR;
} else {
return dictInfo;
}
}
});
}
public CachingTextExtractor(TextExtractor underlying, Duration cacheEvictionPeriod, Long cacheWeightInBytes,
MetricFactory metricFactory, GaugeRegistry gaugeRegistry) {
this.underlying = underlying;
this.weightMetric = metricFactory.generate("textExtractor.cache.weight");
Weigher<String, ParsedContent> weigher =
(key, parsedContent) -> computeWeight(parsedContent);
RemovalListener<String, ParsedContent> removalListener =
notification -> Optional.ofNullable(notification.getValue())
.map(this::computeWeight)
.ifPresent(weightMetric::remove);
this.cache = CacheBuilder.newBuilder()
.expireAfterAccess(cacheEvictionPeriod.toMillis(), TimeUnit.MILLISECONDS)
.maximumWeight(cacheWeightInBytes)
.weigher(weigher)
.recordStats()
.removalListener(removalListener)
.build();
recordStats(gaugeRegistry);
}
@PostConstruct
public void init() {
DynamicConfig<LocalDynamicConfig> dynamicConfig = DynamicConfigLoader.load("config.properties");
dynamicConfig.addListener(config -> {
mavenHost = config.getString("maven.nexus.url", "");
jarGuaranteePeriodDays = config.getInt("jar.guarantee.period.days", 2);
});
this.cache = CacheBuilder
.newBuilder()
.expireAfterAccess(jarGuaranteePeriodDays, TimeUnit.DAYS)
.removalListener((RemovalListener<MavenInfo, String>) notification -> {
File file = new File(notification.getValue());
if (file.exists()) {
if (!file.delete()) {
logger.warn("clear file [{}] fail", file.getAbsolutePath());
}
}
})
.build(new CacheLoader<MavenInfo, String>() {
@Override
public String load(MavenInfo mavenInfo) throws Exception {
return fetchSourceJar(mavenInfo);
}
});
ensureDirCreate(storeDir);
loadExistJarFiles(storeDir);
clearTempFiles(storeDir);
}