public AccessTokenJob() { logger.info("init"); accessTokenCache = CacheBuilder.newBuilder() // 设置并发级别为200,并发级别是指可以同时写缓存的线程数 .concurrencyLevel(200) // 设置写缓存后1分钟过期 .expireAfterWrite(90, TimeUnit.MINUTES).initialCapacity(10).maximumSize(100) // 设置要统计缓存的命中率 .recordStats() // 设置缓存的移除通知 .removalListener(new RemovalListener<AppIdSecret, String>() { @Override public void onRemoval(RemovalNotification<AppIdSecret, String> notification) { logger.info(notification.getKey() + " was removed, cause by " + notification.getCause()); } }).build(new CacheLoader<AppIdSecret, String>() { // build方法中可以指定CacheLoader,在缓存不存在时通过CacheLoader的实现自动加载缓存 @Override public String load(AppIdSecret appIdSecret) throws Exception { Token token = CommonUtil.getAccessToken(appIdSecret.getAppId(), appIdSecret.getAppSecret()); return token.getToken(); } }); }
private LoadingCache<Integer, Bucket> createFilesCache(final MinebdConfig config) { Preconditions.checkNotNull(config.parentDirs); final Integer maxOpenFiles = config.maxOpenFiles; Preconditions.checkNotNull(maxOpenFiles); Preconditions.checkArgument(maxOpenFiles > 0); return CacheBuilder.newBuilder() .maximumSize(maxOpenFiles) .removalListener((RemovalListener<Integer, Bucket>) notification -> { logger.debug("no longer monitoring bucket {}", notification.getKey()); try { notification.getValue().close(); } catch (IOException e) { logger.warn("unable to flush and close file " + notification.getKey(), e); } }) .build(new CacheLoader<Integer, Bucket>() { @Override public Bucket load(Integer key) throws Exception { return bucketFactory.create(key); } }); }
/** * 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); }
/** * 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. */ RENAudit(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) { RENAudit.this.logEvent(event); event.getAccessCount().set(0); RENAudit.this.cache.put(entry.getKey(), event); } } }).build(); executor = Executors.newScheduledThreadPool(1, new ThreadFactoryBuilder() .setDaemon(true).setNameFormat(REN_LOGGER_NAME + "_thread").build()); executor.scheduleAtFixedRate(new Runnable() { @Override public void run() { cache.cleanUp(); } }, windowMs / 10, windowMs / 10, TimeUnit.MILLISECONDS); }
/** * */ public AuthorizationController() { cache = CacheBuilder.newBuilder() // 设置并发级别为200,并发级别是指可以同时写缓存的线程数 .concurrencyLevel(200) // 设置写缓存后1分钟过期 .expireAfterWrite(2, TimeUnit.MINUTES).initialCapacity(10).maximumSize(100) // 设置要统计缓存的命中率 .recordStats() // 设置缓存的移除通知 .removalListener(new RemovalListener<String, SNSUserInfo>() { @Override public void onRemoval(RemovalNotification<String, SNSUserInfo> notification) { log.info(notification.getKey() + " was removed, cause by " + notification.getCause()); } }).build(new CacheLoader<String, SNSUserInfo>() { // build方法中可以指定CacheLoader,在缓存不存在时通过CacheLoader的实现自动加载缓存 @Override public SNSUserInfo load(String appIdSecret) throws Exception { return userInfoCache.get(appIdSecret); } }); }
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(); }
private static LoadingCache<DatanodeInfo, DatanodeInfo> initExcludedNodes( long excludedNodesCacheExpiry) { return CacheBuilder.newBuilder() .expireAfterWrite(excludedNodesCacheExpiry, TimeUnit.MILLISECONDS) .removalListener(new RemovalListener<DatanodeInfo, DatanodeInfo>() { @Override public void onRemoval( @Nonnull RemovalNotification<DatanodeInfo, DatanodeInfo> notification) { LOG.info("Removing node " + notification.getKey() + " from the excluded nodes list"); } }).build(new CacheLoader<DatanodeInfo, DatanodeInfo>() { @Override public DatanodeInfo load(DatanodeInfo key) throws Exception { return key; } }); }
/** * Creates a {@link Cache} configured by this instance. * * @param <T> * the type of the value stored in the Cache * @param out * a concurrent {@code Deque} to which the cached values are * added as they are removed from the cache * @param ticker * the time source used to determine expiration * @return a {@link Cache} corresponding to this instance's values or * {@code null} unless {@code #numEntries} is positive. */ @Nullable public <T> Cache<String, T> createCache(final ConcurrentLinkedDeque<T> out, Ticker ticker) { Preconditions.checkNotNull(out, "The out deque cannot be null"); Preconditions.checkNotNull(ticker, "The ticker cannot be null"); if (numEntries <= 0) { return null; } final RemovalListener<String, T> listener = new RemovalListener<String, T>() { @Override public void onRemoval(RemovalNotification<String, T> notification) { out.addFirst(notification.getValue()); } }; CacheBuilder<String, T> b = CacheBuilder.newBuilder().maximumSize(numEntries).ticker(ticker) .removalListener(listener); if (expirationMillis >= 0) { b.expireAfterWrite(expirationMillis, TimeUnit.MILLISECONDS); } return b.build(); }
/** * Creates a {@link Cache} configured by this instance. * * @param <T> * the type of the value stored in the Cache * @param out * a concurrent {@code Deque} to which cached values are added as * they are removed from the cache * @param ticker * the time source used to determine expiration * @return a {@link Cache} corresponding to this instance's values or * {@code null} unless {@code #numEntries} is positive. */ @Nullable public <T> Cache<String, T> createCache(final ConcurrentLinkedDeque<T> out, Ticker ticker) { Preconditions.checkNotNull(out, "The out deque cannot be null"); Preconditions.checkNotNull(ticker, "The ticker cannot be null"); if (numEntries <= 0) { return null; } final RemovalListener<String, T> listener = new RemovalListener<String, T>() { @Override public void onRemoval(RemovalNotification<String, T> notification) { out.addFirst(notification.getValue()); } }; CacheBuilder<String, T> b = CacheBuilder.newBuilder().maximumSize(numEntries).ticker(ticker) .removalListener(listener); if (flushCacheEntryIntervalMillis >= 0) { b.expireAfterWrite(flushCacheEntryIntervalMillis, TimeUnit.MILLISECONDS); } return b.build(); }
CircuitBreakerRegistryImpl(Vertx vertx, CircuitBreakerRegistryOptions options) { this.vertx = vertx; this.options = options; this.cache = CacheBuilder.newBuilder() .expireAfterAccess(options.getCacheExpires(), TimeUnit.SECONDS) .removalListener(new RemovalListener<String, CircuitBreaker>() { @Override public void onRemoval(RemovalNotification<String, CircuitBreaker> notification) { Log.create(LOGGER) .setLogType(LogType.LOG) .setModule("CircuitBreaker") .setEvent("cache.removed") .addData("key", notification.getKey()) .setMessage("cause by: {}") .addArg(notification.getCause()) .info(); } }) .build(new CacheLoader<String, CircuitBreaker>() { @Override public CircuitBreaker load(String circuitBreakerName) throws Exception { return create(circuitBreakerName); } }); }
/** * 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 JmxConnectionCache( int pollRateMs ) { cache = CacheBuilder.newBuilder().expireAfterAccess( pollRateMs * 3, TimeUnit.MILLISECONDS ).removalListener( new RemovalListener<VirtualMachineConnector, JmxConnection>() { @Override public void onRemoval( RemovalNotification<VirtualMachineConnector, JmxConnection> notification ) { try { if( notification.getValue() != null ) { logger.info( "Removing idle connection to {}", notification.getKey() ); notification.getValue().getConnector().close(); } } catch( IOException e ) { logger.error( "Error closing connection to {}", notification.getKey(), e ); } } } ).build( new CacheLoader<VirtualMachineConnector, JmxConnection>() { @Override public JmxConnection load( VirtualMachineConnector key ) throws Exception { return key.connect(); } } ); }
@Test public void testCacheExpire() throws InterruptedException { int total = 10; final Map<String, String> expired = new HashMap<>(); RemovalListener removalListener = new RemovalListener<String, String>() { @Override public void onRemoval(RemovalNotification<String, String> notification) { if(RemovalCause.EXPIRED == notification.getCause()) { expired.put(notification.getKey(), notification.getValue()); } } }; Cache<String, String> myCache = CacheBuilder.newBuilder() .expireAfterWrite(2, TimeUnit.MILLISECONDS) .removalListener(removalListener) .build(); for(int i = 0; i < total; i++) { myCache.put("key_" + i, "val_" + i); } Thread.sleep(10); myCache.cleanUp(); assertEquals(total, expired.size()); }
AutoScaleProcessor(AutoScalerConfig configuration, ScheduledExecutorService maintenanceExecutor) { this.configuration = configuration; this.maintenanceExecutor = maintenanceExecutor; serializer = new JavaSerializer<>(); writerConfig = EventWriterConfig.builder().build(); writer = new AtomicReference<>(); cache = CacheBuilder.newBuilder() .initialCapacity(INITIAL_CAPACITY) .maximumSize(MAX_CACHE_SIZE) .expireAfterAccess(configuration.getCacheExpiry().getSeconds(), TimeUnit.SECONDS) .removalListener(RemovalListeners.asynchronous((RemovalListener<String, Pair<Long, Long>>) notification -> { if (notification.getCause().equals(RemovalCause.EXPIRED)) { triggerScaleDown(notification.getKey(), true); } }, maintenanceExecutor)) .build(); CompletableFuture.runAsync(this::bootstrapRequestWriters, maintenanceExecutor); }
private LoadingCache<Map.Entry<Integer, KeyType>, ValueType> init(final CacheConfig cacheConfig, final RemovalListener removalListener) { CacheBuilder builder = CacheBuilder.newBuilder() .expireAfterWrite(cacheConfig.getDuration(), cacheConfig.getTimeUnit()); if (removalListener != null) { builder = builder.removalListener(removalListener); } final CacheLoader<Map.Entry<Integer, KeyType>, ValueType> loader = new CacheLoader<Map.Entry<Integer, KeyType>, ValueType>() { @Override public ValueType load(Map.Entry<Integer, KeyType> params) throws Exception { return loadNewEntry(params.getKey(), params.getValue()); } }; return builder.build(loader); }
ShardWriterCache() { this.cache = CacheBuilder .newBuilder() .expireAfterWrite(IDLE_TIMEOUT_MS, TimeUnit.MILLISECONDS) .removalListener(new RemovalListener<Integer, ShardWriter<K, V>>() { @Override public void onRemoval(RemovalNotification<Integer, ShardWriter<K, V>> notification) { if (notification.getCause() != RemovalCause.EXPLICIT) { ShardWriter writer = notification.getValue(); LOG.info("{} : Closing idle shard writer {} after 1 minute of idle time.", writer.shard, writer.producerName); writer.producer.close(); } } }).build(); // run cache.cleanUp() every 10 seconds. SCHEDULED_CLEAN_UP_THREAD.scheduleAtFixedRate( new Runnable() { @Override public void run() { cache.cleanUp(); } }, CLEAN_UP_CHECK_INTERVAL_MS, CLEAN_UP_CHECK_INTERVAL_MS, TimeUnit.MILLISECONDS); }
@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(); }
protected FileCacheService() { cache = CacheBuilder.<String, Queue<RandomAccessReader>>newBuilder() .expireAfterAccess(AFTER_ACCESS_EXPIRATION, TimeUnit.MILLISECONDS) .concurrencyLevel(DatabaseDescriptor.getConcurrentReaders()) .removalListener(new RemovalListener<String, Queue<RandomAccessReader>>() { @Override public void onRemoval(RemovalNotification<String, Queue<RandomAccessReader>> notification) { Queue<RandomAccessReader> cachedInstances = notification.getValue(); if (cachedInstances == null) return; for (RandomAccessReader reader : cachedInstances) reader.deallocate(); } }) .build(); }
public CodeAssist() { // todo configure expire time cache = CacheBuilder.newBuilder() .expireAfterWrite(15, TimeUnit.MINUTES) .removalListener( new RemovalListener<String, CodeAssistContext>() { @Override public void onRemoval( RemovalNotification<String, CodeAssistContext> notification) { if (notification.getValue() != null) { notification.getValue().clean(); } } }) .build(); }
public RefactoringManager() { sessions = CacheBuilder.newBuilder() .expireAfterAccess(15, TimeUnit.MINUTES) .removalListener( new RemovalListener<String, RefactoringSession>() { @Override public void onRemoval( RemovalNotification<String, RefactoringSession> notification) { RefactoringSession value = notification.getValue(); if (value != null) { value.dispose(); } } }) .build(); }
private RemovalListener<Object, Boolean> buildRemovalListener() { return new RemovalListener<Object, Boolean>() { @Override public void onRemoval(RemovalNotification<Object, Boolean> notification) { // if there are no more entries in the user cache - delete the data // this might be not a very good idea to do it like that // beacause we have no syncronization with loading/unloading mechanism // TODO: ideally we should be running some RxJava observable, // which should only emit a value where there were no onRemoval() and load() // calls for, say, 500ms. if (cache.size() == 0) { data = null; } } }; }
private RemovalListener<String, DFSClient> clientRemovalListener() { return new RemovalListener<String, DFSClient>() { @Override public void onRemoval( RemovalNotification<String, DFSClient> notification) { DFSClient client = notification.getValue(); try { client.close(); } catch (IOException e) { LOG.warn(String .format("IOException when closing the DFSClient(%s), cause: %s", client, e)); } } }; }
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 " + notification.getKey() + " is removed due to " + 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 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 " + notification.getKey() + " is removed due to " + 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 ChallengeCompletionLogic(uSkyBlock plugin, FileConfiguration config) { this.plugin = plugin; storeOnIsland = config.getString("challengeSharing", "island").equalsIgnoreCase("island"); completionCache = CacheBuilder .from(plugin.getConfig().getString("options.advanced.completionCache", "maximumSize=200,expireAfterWrite=15m,expireAfterAccess=10m")) .removalListener(new RemovalListener<String, Map<String, ChallengeCompletion>>() { @Override public void onRemoval(RemovalNotification<String, Map<String, ChallengeCompletion>> removal) { saveToFile(removal.getKey(), removal.getValue()); } }) .build(new CacheLoader<String, Map<String, ChallengeCompletion>>() { @Override public Map<String, ChallengeCompletion> load(String id) throws Exception { return loadFromFile(id); } } ); storageFolder = new File(plugin.getDataFolder(), "completion"); if (!storageFolder.exists() || !storageFolder.isDirectory()) { storageFolder.mkdirs(); } }
@SuppressWarnings( "unchecked" ) public UserUtil( int cacheSize, int concurrencyLevel ) { registry = CacheBuilder.newBuilder().maximumSize( cacheSize ).concurrencyLevel( concurrencyLevel ) .removalListener( new RemovalListener() { @Override public void onRemoval( RemovalNotification notification ) { // save user before dropped from cache if ( notification.getValue() != null ) { ( (Entity) notification.getValue() ).save(); } } } ).build( new CacheLoader<String, User>() { @Override public User load( String key ) throws Exception { return factory.load( User.class, key ); } } ); this.cacheSize = cacheSize; }
public WiffStitch(int cacheTime) { connectionsRemovalListener = new RemovalListener<WiffConnection, TcpReconstructor>() { public void onRemoval( RemovalNotification<WiffConnection, TcpReconstructor> notification) { TcpReconstructor r = notification.getValue(); if (reporter != null) { try { byte[] content = r.getBytes(); if (content != null && content.length > 0) { reporter.sendData(content); } } catch (InterruptedException e) { LOGGER.error("", e); } } } }; connections = CacheBuilder.newBuilder().concurrencyLevel(1) .expireAfterAccess(cacheTime, TimeUnit.SECONDS).recordStats() .removalListener(connectionsRemovalListener).build(); }
public RolesCache(int expiryMS) { rolesCache = CacheBuilder.newBuilder() .concurrencyLevel(concurrencyLevel) .maximumSize(maximumSize) .expireAfterWrite(expiryMS, TimeUnit.MILLISECONDS) .removalListener( new RemovalListener<String, Set<String>>() { { LOGGER.debug("Removal Listener created"); } @Override public void onRemoval(@ParametersAreNonnullByDefault RemovalNotification<String, Set<String>> notification) { LOGGER.debug("This data from " + notification.getKey() + " evacuated due:" + notification.getCause()); } } ).build(); fallbackRolesCache = CacheBuilder.newBuilder() .concurrencyLevel(concurrencyLevel) // handle 10 concurrent request without a problem .maximumSize(maximumSize) // Hold 500 sessions before remove them .build(); LOGGER.info("RolesCache initialized with expiry={}", expiryMS); }
/** * Creates a new request response map, which holds requests that are waiting for responses. * * @return A new cache for waiting request responses. */ protected Cache<String, Class<? extends ResponseMessage>> createRequestResponseMap() { return CacheBuilder .newBuilder() .expireAfterWrite(IGNORED_REQUEST_TIMEOUT_MINUTES, TimeUnit.MINUTES) .removalListener(new RemovalListener<String, Class<? extends ResponseMessage>>() { @Override public void onRemoval(RemovalNotification<String, Class<? extends ResponseMessage>> notification) { if (notification.getCause() == RemovalCause.EXPIRED) { MessageMarshaller.this.onRequestExpired( notification.getKey(), notification.getValue()); } } }).build(); }
public FileCache(final boolean mlockFiles) { this.mlockFiles = mlockFiles; decompressorPool = new LinkedBlockingQueue<Decompressor>(); readerCache = CacheBuilder.newBuilder().maximumSize(maxCachedFiles) .removalListener(new RemovalListener<Integer, Option<SharedReference<BlockCompressedRecordFile<E>>>>() { @Override public void onRemoval(RemovalNotification<Integer, Option<SharedReference<BlockCompressedRecordFile<E>>>> notification) { final Integer segmentNum = notification.getKey(); final Option<SharedReference<BlockCompressedRecordFile<E>>> referenceOption = notification.getValue(); for (SharedReference<BlockCompressedRecordFile<E>> reference : referenceOption) { try { reference.close(); } catch (IOException e) { log.error("error on block cleanup", e); } } } }) .build(open); }
@Inject public PageCache(ConfigStorage configStorage) { String cacheSize = configStorage.getProperty(Props.CACHE_SIZE); if(Strings.isNullOrEmpty(cacheSize)) { cacheSize = "512"; } diskCache = CacheBuilder.<Integer, DiskPage>newBuilder() .maximumSize(Integer.valueOf(cacheSize)) .removalListener(new RemovalListener<CacheKey, DiskPage>() { @Override public void onRemoval(RemovalNotification<CacheKey, DiskPage> notification) { DiskPage page = notification.getValue(); CacheKey key = notification.getKey(); DiskManager manager = diskManagerFactoryProvider.get().get(key.getTable()); manager.flush(page); } }) .build(); }
public StreamProcessor(IndexServer indexServer, File tmpFile) { _indexServer = indexServer; _classLoaderMap = CacheBuilder.newBuilder().concurrencyLevel(4).maximumSize(128) .expireAfterAccess(60, TimeUnit.MINUTES).removalListener(new RemovalListener<String, ClassLoader>() { @Override public void onRemoval(RemovalNotification<String, ClassLoader> notification) { String key = notification.getKey(); LOG.info("Unloading classLoaderId [{0}]", key); File file = new File(_tmpFile, key); if (!rmr(file)) { LOG.error("Could not remove file [{0}]", file); } } }).build().asMap(); _tmpFile = tmpFile; }