Java Code Examples for com.google.common.cache.Cache#asMap()
The following examples show how to use
com.google.common.cache.Cache#asMap() .
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: DateIndexHelper.java From datawave with Apache License 2.0 | 6 votes |
private void showMeDaCache(String when) { log.trace("from applicationContext:" + applicationContext); if (this.applicationContext != null) { CacheManager cacheManager = applicationContext.getBean("dateIndexHelperCacheManager", CacheManager.class); log.trace("beans are " + Arrays.toString(applicationContext.getBeanDefinitionNames())); if (cacheManager != null) { for (String cacheName : cacheManager.getCacheNames()) { log.trace(when + " got " + cacheName); Object nativeCache = cacheManager.getCache(cacheName).getNativeCache(); log.trace("nativeCache is a " + nativeCache); Cache cache = (Cache) nativeCache; Map map = cache.asMap(); log.trace("cache map is " + map); log.trace("cache map size is " + map.size()); for (Object key : map.keySet()) { log.trace("value for " + key + " is :" + map.get(key)); } } } else { log.trace(when + "CacheManager is " + cacheManager); } } }
Example 2
Source File: DoTestRuleFilterFactory.java From uavstack with Apache License 2.0 | 6 votes |
@Test public void guavaCache() throws InterruptedException { TesTicker ticker = new TesTicker(); Cache<String, Pojo> collection = CacheBuilder.newBuilder().expireAfterAccess(5, TimeUnit.SECONDS).ticker(ticker) .<String, Pojo> build(); Pojo p1 = new Pojo("p1name", "p1val"); Pojo p2 = new Pojo("p2name", "p2val"); collection.put("p1", p1); collection.put("p2", p2); ticker.advance(3, TimeUnit.SECONDS); Map<String, Pojo> map = collection.asMap(); assertTrue(map.containsKey("p1")); // map.get("p1"); ticker.advance(3, TimeUnit.SECONDS); assertEquals(2, collection.size()); assertFalse(map.containsKey("p1"));// 有清除过期操作 assertEquals(1, collection.size()); assertNull(collection.getIfPresent("p2")); assertNull(collection.getIfPresent("p1"));// 有清除过期操作 assertEquals(0, collection.size()); }
Example 3
Source File: PrometheusSink.java From incubator-heron with Apache License 2.0 | 6 votes |
@Override public void processRecord(MetricsRecord record) { final String[] sources = MetricsUtil.splitRecordSource(record); if (sources.length > 2) { final String source = String.format("%s/%s/%s", getTopologyName(), sources[1], sources[2]); Map<String, Double> sourceCache = metricsCache.getIfPresent(source); if (sourceCache == null) { final Cache<String, Double> newSourceCache = createCache(); sourceCache = newSourceCache.asMap(); } sourceCache.putAll(processMetrics(record.getMetrics())); metricsCache.put(source, sourceCache); } else { LOG.log(Level.SEVERE, "Unexpected metrics source: " + record.getSource()); } }
Example 4
Source File: UserContextStoreImpl.java From cellery-security with Apache License 2.0 | 5 votes |
public UserContextStoreImpl() { log.info("User Context expiry set to {} seconds.", getUserContextExpiry()); Cache<String, String> cache = CacheBuilder.newBuilder() .expireAfterWrite(getUserContextExpiry(), TimeUnit.SECONDS) .removalListener(removalNotification -> { log.debug("Stored user context was removed: " + removalNotification); }) .build(); userContextMap = cache.asMap(); }
Example 5
Source File: TimelineMetricsHolder.java From ambari-metrics with Apache License 2.0 | 5 votes |
/** * 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 6
Source File: GuavaConcurrentMapFactory.java From icure-backend with GNU General Public License v2.0 | 5 votes |
@Override public <K, V> ConcurrentMap<K, V> getMap(final ConcurrentMapListener<K, V> listener) { // Create cache builder CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); // Set expireAfterWrite if (expireDuration != null && expireUnit != null) { cacheBuilder = cacheBuilder.expireAfterWrite(expireDuration, expireUnit); } // Configure listener if (listener != null) { cacheBuilder.removalListener((RemovalListener<K, V>) notification -> { K key = notification.getKey(); V value = notification.getValue(); switch (notification.getCause()) { case REPLACED: listener.entryUpdated(key, value); break; case EXPLICIT: listener.entryRemoved(key, value); break; case COLLECTED: case EXPIRED: case SIZE: listener.entryEvicted(key, value); break; } }); } // Build cache Cache<K, V> cache = cacheBuilder.build(); return cache.asMap(); }
Example 7
Source File: PageCacheBuilder.java From hermes with Apache License 2.0 | 5 votes |
@SuppressWarnings("rawtypes") private void copyCacheDatas(Cache<Long, Page<T>> from, Cache<Long, Page<T>> to) { ConcurrentMap<Long, Page<T>> asMap = from.asMap(); Object[] segments = (Object[]) Reflects.forField().getDeclaredFieldValue(asMap, "segments"); Object segment = segments[0]; Queue accessQueue = (Queue) Reflects.forField().getDeclaredFieldValue(segment, "accessQueue"); ReentrantLock lock = ((ReentrantLock) segment); lock.lock(); try { for (Object item : accessQueue) { try { Method m1 = Reflects.forMethod().getMethod(item.getClass(), "getKey"); m1.setAccessible(true); Long key = (Long) m1.invoke(item); Method m2 = Reflects.forMethod().getMethod(item.getClass(), "getValueReference"); m2.setAccessible(true); Object valueRef = m2.invoke(item); Page<T> value = Reflects.forMethod().invokeDeclaredMethod(valueRef, "get"); to.put(key, value); } catch (Exception e) { log.error("Exception occurred while copying data from old cache to new cache.", e); } } } finally { lock.unlock(); } }
Example 8
Source File: GuavaLocalCache.java From DBus with Apache License 2.0 | 5 votes |
@Override public Map<String, Object> asMap(String cacheName) { Cache<String, Object> cache = container.get(cacheName); if (cache == null) { return Collections.emptyMap(); } Map<String, Object> map = cache.asMap(); if (map != null) { return ImmutableMap.copyOf(map); } return Collections.emptyMap(); }
Example 9
Source File: LockStoreImpl.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
private static ConcurrentMap<NodeRef, LockState> createMap(long expiry, TimeUnit timeUnit) { Cache<NodeRef, LockState> cache = CacheBuilder.newBuilder() .concurrencyLevel(32) .expireAfterWrite(expiry, timeUnit) .build(); return cache.asMap(); }
Example 10
Source File: WebSink.java From incubator-heron with Apache License 2.0 | 5 votes |
@Override public void processRecord(MetricsRecord record) { final String[] sources = MetricsUtil.splitRecordSource(record); final String source; if (sources.length > 2) { source = shouldIncludeTopologyName ? String.format("%s/%s/%s", getTopologyName(), sources[1], sources[2]) : String.format("/%s/%s", sources[1], sources[2]); } else { source = shouldIncludeTopologyName ? String.format("%s/%s", getTopologyName(), record.getSource()) : String.format("/%s", record.getSource()); } if (flattenMetrics) { metricsCache.putAll(processMetrics(source + "/", record.getMetrics())); } else { Map<String, Double> sourceCache; Object sourceObj = metricsCache.getIfPresent(source); if (sourceObj instanceof Map) { @SuppressWarnings("unchecked") Map<String, Double> castObj = (Map<String, Double>) sourceObj; sourceCache = castObj; } else { final Cache<String, Double> newSourceCache = createCache(); sourceCache = newSourceCache.asMap(); } sourceCache.putAll(processMetrics("", record.getMetrics())); metricsCache.put(source, sourceCache); } }
Example 11
Source File: ElasticsearchSuggestModule.java From heroic with Apache License 2.0 | 5 votes |
@Provides @ElasticsearchScope public RateLimitedCache<Pair<String, HashCode>> writeCache(final HeroicReporter reporter) { final Cache<Pair<String, HashCode>, Boolean> cache = CacheBuilder .newBuilder() .concurrencyLevel(writeCacheConcurrency) .maximumSize(writeCacheMaxSize) .expireAfterWrite(writeCacheDurationMinutes, MINUTES) .build(); reporter.registerCacheSize("elasticsearch-suggest-write-through", cache::size); if (writesPerSecond <= 0d) { return new DisabledRateLimitedCache<>(cache.asMap()); } if (distributedCacheSrvRecord.length() > 0) { return new DistributedRateLimitedCache<>( cache.asMap(), RateLimiter.create(writesPerSecond, rateLimitSlowStartSeconds, SECONDS), MemcachedConnection.create(distributedCacheSrvRecord), toIntExact(Duration.of(writeCacheDurationMinutes, MINUTES).convert(SECONDS)), reporter.newMemcachedReporter("suggest") ); } return new DefaultRateLimitedCache<>(cache.asMap(), RateLimiter.create(writesPerSecond, rateLimitSlowStartSeconds, SECONDS)); }
Example 12
Source File: ElasticsearchMetadataModule.java From heroic with Apache License 2.0 | 5 votes |
@Provides @ElasticsearchScope public RateLimitedCache<Pair<String, HashCode>> writeCache(HeroicReporter reporter) { final Cache<Pair<String, HashCode>, Boolean> cache = CacheBuilder .newBuilder() .concurrencyLevel(writeCacheConcurrency) .maximumSize(writeCacheMaxSize) .expireAfterWrite(writeCacheDurationMinutes, TimeUnit.MINUTES) .build(); reporter.registerCacheSize("elasticsearch-metadata-write-through", cache::size); if (writesPerSecond <= 0d) { return new DisabledRateLimitedCache<>(cache.asMap()); } if (distributedCacheSrvRecord.length() > 0) { return new DistributedRateLimitedCache<>( cache.asMap(), RateLimiter.create(writesPerSecond, rateLimitSlowStartSeconds, SECONDS), MemcachedConnection.create(distributedCacheSrvRecord), toIntExact(Duration.of(writeCacheDurationMinutes, MINUTES).convert(SECONDS)), reporter.newMemcachedReporter("metadata") ); } return new DefaultRateLimitedCache<>(cache.asMap(), RateLimiter.create(writesPerSecond, rateLimitSlowStartSeconds, TimeUnit.SECONDS)); }
Example 13
Source File: DefaultActiveTraceRepository.java From pinpoint with Apache License 2.0 | 5 votes |
private ConcurrentMap<ActiveTraceHandle, ActiveTrace> createCache(int maxActiveTraceSize) { final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); cacheBuilder.concurrencyLevel(64); cacheBuilder.initialCapacity(maxActiveTraceSize); cacheBuilder.maximumSize(maxActiveTraceSize); final Cache<ActiveTraceHandle, ActiveTrace> localCache = cacheBuilder.build(); return localCache.asMap(); }
Example 14
Source File: SimpleCache.java From pinpoint with Apache License 2.0 | 5 votes |
private ConcurrentMap<T, Result> createCache(int maxCacheSize) { final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); cacheBuilder.concurrencyLevel(64); cacheBuilder.initialCapacity(maxCacheSize); cacheBuilder.maximumSize(maxCacheSize); Cache<T, Result> localCache = cacheBuilder.build(); ConcurrentMap<T, Result> cache = localCache.asMap(); return cache; }
Example 15
Source File: LRUCache.java From pinpoint with Apache License 2.0 | 5 votes |
public LRUCache(int maxCacheSize) { final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); cacheBuilder.concurrencyLevel(32); cacheBuilder.initialCapacity(maxCacheSize); cacheBuilder.maximumSize(maxCacheSize); Cache<T, Object> localCache = cacheBuilder.build(); this.cache = localCache.asMap(); }
Example 16
Source File: SchemaIdVersionTest.java From registry with Apache License 2.0 | 4 votes |
@Test public void testEqualityWithCache() throws Exception { Cache<SchemaIdVersion, String> cache = CacheBuilder.newBuilder().maximumSize(10).build(); ConcurrentMap<SchemaIdVersion, String> map = cache.asMap(); doTestSchemaVersionIdEquality(map); }