Example usage for com.google.common.cache Cache asMap

List of usage examples for com.google.common.cache Cache asMap

Introduction

In this page you can find the example usage for com.google.common.cache Cache asMap.

Prototype

ConcurrentMap<K, V> asMap();

Source Link

Document

Returns a view of the entries stored in this cache as a thread-safe map.

Usage

From source file:org.alfresco.repo.lock.mem.LockStoreImpl.java

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();
}

From source file:org.datacleaner.util.CollectionUtils2.java

/**
 * //  w  ww .jav  a2s. c  o  m
 * @return
 * 
 * @deprecated use Google Guava's {@link CacheBuilder},
 *             {@link #createCache(int, long)} or something similar if
 *             needed.
 */
@Deprecated
public static <K, V> ConcurrentMap<K, V> createCacheMap() {
    Cache<K, V> cache = CacheBuilder.newBuilder().maximumSize(10000).build();
    return cache.asMap();
}

From source file:com.facebook.presto.hive.metastore.RecordingHiveMetastore.java

private static <K, V> List<Pair<K, V>> toPairs(Cache<K, V> cache) {
    return cache.asMap().entrySet().stream().map(entry -> new Pair<>(entry.getKey(), entry.getValue()))
            .collect(toImmutableList());
}

From source file:com.baidu.oped.apm.profiler.metadata.LRUCache.java

public LRUCache(int maxCacheSize) {
    final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    cacheBuilder.concurrencyLevel(32);/*from  w w w. j av a  2s .co  m*/
    cacheBuilder.initialCapacity(maxCacheSize);
    cacheBuilder.maximumSize(maxCacheSize);
    Cache<T, Object> localCache = cacheBuilder.build();
    this.cache = localCache.asMap();
}

From source file:com.navercorp.pinpoint.profiler.metadata.SimpleCache.java

private ConcurrentMap<T, Result> createCache(int maxCacheSize) {
    final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    cacheBuilder.concurrencyLevel(64);/* w  w w . j ava 2 s .c  o m*/
    cacheBuilder.initialCapacity(maxCacheSize);
    cacheBuilder.maximumSize(maxCacheSize);
    Cache<T, Result> localCache = cacheBuilder.build();
    ConcurrentMap<T, Result> cache = localCache.asMap();
    return cache;
}

From source file:com.navercorp.pinpoint.profiler.context.active.ActiveTraceRepository.java

private ConcurrentMap<Long, ActiveTrace> createCache(int maxActiveTraceSize) {
    final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder();
    cacheBuilder.concurrencyLevel(64);/*from   www  . jav  a  2  s .  co m*/
    cacheBuilder.initialCapacity(maxActiveTraceSize);
    cacheBuilder.maximumSize(maxActiveTraceSize);
    // OOM defense
    cacheBuilder.weakValues();

    final Cache<Long, ActiveTrace> localCache = cacheBuilder.build();
    return localCache.asMap();
}

From source file:org.elasticsearch.index.cache.field.data.support.AbstractConcurrentMapFieldDataCache.java

@Override
public long sizeInBytes() {
    // the overhead of the map is not really relevant...
    long sizeInBytes = 0;
    for (Cache<String, FieldData> map : cache.values()) {
        for (FieldData fieldData : map.asMap().values()) {
            sizeInBytes += fieldData.sizeInBytes();
        }// w  ww  . ja  v a2  s.  co  m
    }
    return sizeInBytes;
}

From source file:com.twitter.heron.metricsmgr.sink.WebSink.java

@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 {//from   w  w  w.j av  a 2  s  .co  m
        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);
    }
}

From source file:org.apache.pulsar.functions.metrics.sink.PrometheusSink.java

@Override
public void processRecord(MetricsData record, Function.FunctionDetails functionDetails) {
    final String source = FunctionDetailsUtils.getFullyQualifiedName(functionDetails);

    Map<String, Double> sourceCache = metricsCache.getIfPresent(source);
    if (sourceCache == null) {
        final Cache<String, Double> newSourceCache = createCache();
        sourceCache = newSourceCache.asMap();
    }//from w ww .  j a va2  s  . c  om

    sourceCache.putAll(processMetrics(record));
    metricsCache.put(source, sourceCache);
}

From source file:io.airlift.sample.PersonStore.java

@Inject
public PersonStore(StoreConfig config, EventClient eventClient) {
    Preconditions.checkNotNull(config, "config must not be null");
    Preconditions.checkNotNull(eventClient, "eventClient is null");

    Cache<String, Person> personCache = CacheBuilder.newBuilder()
            .expireAfterWrite(config.getTtl().toMillis(), TimeUnit.MILLISECONDS).build();
    persons = personCache.asMap();
    stats = new PersonStoreStats(eventClient);
}