static void closeQueryRequest(String project, int maxConcurrentQuery) { if (maxConcurrentQuery == 0) { return; } AtomicInteger nRunningQueries = runningStats.getIfPresent(project); if (nRunningQueries != null) { nRunningQueries.decrementAndGet(); } }
public static Integer getCurrentRunningQuery(String project) { AtomicInteger nRunningQueries = runningStats.getIfPresent(project); if (nRunningQueries != null) { return nRunningQueries.get(); } else { return null; } }
public NetworkLocation get(HostAddress host) { NetworkLocation location = cache.getIfPresent(host); if ((location == null) && (negativeCache.getIfPresent(host) == null)) { // Store a value in the cache, so that refresh() is done asynchronously cache.put(host, ROOT_LOCATION); cache.refresh(host); } // Return the root location for anything we couldn't locate return location == null ? ROOT_LOCATION : location; }
@Override public int complete(String buffer, int cursor, List<CharSequence> candidates) { if (cursor <= 0) { return cursor; } int blankPos = findLastBlank(buffer.substring(0, cursor)); String prefix = buffer.substring(blankPos + 1, cursor); String schemaName = queryRunner.getSession().getSchema(); if (schemaName != null) { List<String> functionNames = functionCache.getIfPresent(schemaName); List<String> tableNames = tableCache.getIfPresent(schemaName); SortedSet<String> sortedCandidates = new TreeSet<>(); if (functionNames != null) { sortedCandidates.addAll(filterResults(functionNames, prefix)); } if (tableNames != null) { sortedCandidates.addAll(filterResults(tableNames, prefix)); } candidates.addAll(sortedCandidates); } return blankPos + 1; }
@Override public void remove(final String name, final String[] tags, final String[] values) { final Collector collector = CACHE.getIfPresent(new SimpleCollectorKey(name, tags)); if (collector == null) return; CollectorRegistry.defaultRegistry.unregister(collector); }
private ByteBuf getKeyAndDecryptData(MessageMetadata msgMetadata, ByteBuf payload) { ByteBuf decryptedData = null; List<EncryptionKeys> encKeys = msgMetadata.getEncryptionKeysList(); // Go through all keys to retrieve data key from cache for (int i = 0; i < encKeys.size(); i++) { byte[] msgDataKey = encKeys.get(i).getValue().toByteArray(); byte[] keyDigest = digest.digest(msgDataKey); SecretKey storedSecretKey = dataKeyCache.getIfPresent(ByteBuffer.wrap(keyDigest)); if (storedSecretKey != null) { // Taking a small performance hit here if the hash collides. When it // retruns a different key, decryption fails. At this point, we would // call decryptDataKey to refresh the cache and come here again to decrypt. decryptedData = decryptData(storedSecretKey, msgMetadata, payload); // If decryption succeeded, data is non null if (decryptedData != null) { break; } } else { // First time, entry won't be present in cache log.debug("{} Failed to decrypt data or data key is not in cache. Will attempt to refresh", logCtx); } } return decryptedData; }
/** * Asynchronously look up a players hiscore from a specified endpoint * * @param username Players username * @param endpoint Hiscore endpoint * @return HiscoreResult or null */ public HiscoreResult lookupAsync(String username, HiscoreEndpoint endpoint) { HiscoreKey hiscoreKey = new HiscoreKey(username, endpoint); HiscoreResult hiscoreResult = hiscoreCache.getIfPresent(hiscoreKey); if (hiscoreResult != null && hiscoreResult != EMPTY) { return hiscoreResult == NONE ? null : hiscoreResult; } hiscoreCache.refresh(hiscoreKey); return null; } }
/** * Return the status of the ASG whether is enabled or disabled for service. * The value is picked up from the cache except the very first time. * * @param instanceInfo the instanceInfo for the lookup * @return true if enabled, false otherwise */ public boolean isASGEnabled(InstanceInfo instanceInfo) { CacheKey cacheKey = new CacheKey(getAccountId(instanceInfo, accountId), instanceInfo.getASGName()); Boolean result = asgCache.getIfPresent(cacheKey); if (result != null) { return result; } else { if (!serverConfig.shouldUseAwsAsgApi()) { // Disabled, cached values (if any) are still being returned if the caller makes // a decision to call the disabled client during some sort of transitioning // period, but no new values will be fetched while disabled. logger.info(("'{}' is not cached at the moment and won't be fetched because querying AWS ASGs " + "has been disabled via the config, returning the fallback value."), cacheKey); return true; } logger.info("Cache value for asg {} does not exist yet, async refreshing.", cacheKey.asgName); // Only do an async refresh if it does not yet exist. Do this to refrain from calling aws api too much asgCache.refresh(cacheKey); return true; } }
public void testWriteThroughEntry() { for (LoadingCache<Object, Object> cache : caches()) { cache.getUnchecked(1); Entry<Object, Object> entry = Iterables.getOnlyElement(cache.asMap().entrySet()); cache.invalidate(1); assertEquals(0, cache.size()); entry.setValue(3); assertEquals(1, cache.size()); assertEquals(3, cache.getIfPresent(1)); checkValidState(cache); try { entry.setValue(null); fail(); } catch (NullPointerException expected) { } checkValidState(cache); } }
/** * Synchronously look up a players hiscore from a specified endpoint * * @param username Players username * @param endpoint Hiscore endpoint * @return HiscoreResult or null * @throws IOException Upon error in fetching hiscore */ public HiscoreResult lookup(String username, HiscoreEndpoint endpoint) throws IOException { HiscoreKey hiscoreKey = new HiscoreKey(username, endpoint); HiscoreResult hiscoreResult = hiscoreCache.getIfPresent(hiscoreKey); if (hiscoreResult != null && hiscoreResult != EMPTY) { return hiscoreResult == NONE ? null : hiscoreResult; } hiscoreResult = hiscoreClient.lookup(username, endpoint); if (hiscoreResult == null) { hiscoreCache.put(hiscoreKey, NONE); return null; } hiscoreCache.put(hiscoreKey, hiscoreResult); return hiscoreResult; }
public void testWriteThroughEntry() { for (LoadingCache<Object, Object> cache : caches()) { cache.getUnchecked(1); Entry<Object, Object> entry = Iterables.getOnlyElement(cache.asMap().entrySet()); cache.invalidate(1); assertEquals(0, cache.size()); entry.setValue(3); assertEquals(1, cache.size()); assertEquals(3, cache.getIfPresent(1)); checkValidState(cache); try { entry.setValue(null); fail(); } catch (NullPointerException expected) { } checkValidState(cache); } }
public Optional<Long> loadIfCached(long startTs) { return Optional.ofNullable(cache.getIfPresent(startTs)); }
assertSame(one, cache.getIfPresent(key)); stats = cache.stats(); assertEquals(1, stats.missCount()); assertSame(two, cache.getIfPresent(key)); stats = cache.stats(); assertEquals(1, stats.missCount()); assertSame(two, cache.getIfPresent(key)); stats = cache.stats(); assertEquals(1, stats.missCount());
@Override public GuidePostsInfo getTableStats(GuidePostsKey key) { GuidePostsInfo info = guidePostsCache.getCache().getIfPresent(key); if (null == info) { return GuidePostsInfo.NO_GUIDEPOST; } return info; }
assertSame(one, cache.getIfPresent(key)); stats = cache.stats(); assertEquals(1, stats.missCount()); assertSame(one, cache.getIfPresent(key)); stats = cache.stats(); assertEquals(1, stats.missCount()); assertSame(two, cache.getIfPresent(key)); stats = cache.stats(); assertEquals(1, stats.missCount());
/** * Creates or recycles a {@link CCMAccess} instance and returns it. * * <p>Caller MUST call {@link CCMAccess#close()} when done with the cluster, to ensure that * resources will be properly freed. */ public static CCMAccess get(CCMBridge.Builder key) { CachedCCMAccess ccm = CACHE.getIfPresent(key); if (ccm != null) { ccm.refCount.incrementAndGet(); } else { try { ccm = CACHE.get(key); } catch (ExecutionException e) { throw Throwables.propagate(e); } } logCache(); return ccm; }
TypeCodec<?> existing = cache.getIfPresent(key); if (existing != null) { logger.warn(
@Override @Nullable public Session getIfPresent(LocalPlayer player) { return sessions.getIfPresent(player); }
@Override public Serializable get(String key) { if (StrUtil.isBlank(key)) { return null; } Serializable ret = loadingCache.getIfPresent(key); if (ret == null) { ret = temporaryLoadingCache.getIfPresent(key); } return ret; }
@Override public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException { if (location.isOutputLocation()) { return inMemoryFileObjects.getIfPresent(uriForJavaFileObject(location, className, kind)); } else { return super.getJavaFileForInput(location, className, kind); } }