Example usage for com.google.common.cache LoadingCache put

List of usage examples for com.google.common.cache LoadingCache put

Introduction

In this page you can find the example usage for com.google.common.cache LoadingCache put.

Prototype

void put(K key, V value);

Source Link

Document

Associates value with key in this cache.

Usage

From source file:com.palantir.atlasdb.shell.AtlasShellConnection.java

public static AtlasShellConnection createAtlasShellConnection(final AtlasContext atlasContext) {
    LoadingCache<String, TableMetadata> metadataCache = CacheBuilder.newBuilder()
            .build(new CacheLoader<String, TableMetadata>() {
                @Override//from w w  w  .  jav  a  2 s .c  om
                public TableMetadata load(String tableName) throws Exception {
                    byte[] metadataBytes = atlasContext.getKeyValueService().getMetadataForTable(tableName);
                    if (metadataBytes == null || metadataBytes.length == 0) {
                        return new TableMetadata();
                    }
                    return TableMetadata.BYTES_HYDRATOR.hydrateFromBytes(metadataBytes);
                }
            });
    for (Entry<String, byte[]> e : atlasContext.getKeyValueService().getMetadataForTables().entrySet()) {
        if (e.getValue() == null || e.getValue().length == 0) {
            metadataCache.put(e.getKey(), new TableMetadata());
        } else {
            metadataCache.put(e.getKey(), TableMetadata.BYTES_HYDRATOR.hydrateFromBytes(e.getValue()));
        }
    }
    return new AtlasShellConnection(atlasContext.getKeyValueService(), atlasContext.getTransactionManager(),
            metadataCache);
}

From source file:name.martingeisse.sql.cache.RowCacheGroup.java

/**
 * //from ww  w . j  a  v a  2 s  .c  o m
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private void distributeHelper(Wrapper wrapper, Function valueToKeyMapping, LoadingCache cache) {
    Object key = valueToKeyMapping.apply(wrapper.getValue());
    if (key != null) {
        cache.put(key, wrapper);
    }
}

From source file:org.apache.rocketmq.console.task.DashboardCollectTask.java

private void writeFile(LoadingCache<String, List<String>> map, Map<String, List<String>> fileMap, File file)
        throws IOException {
    Map<String, List<String>> newMap = map.asMap();
    Map<String, List<String>> resultMap = Maps.newHashMap();
    if (fileMap.size() == 0) {
        resultMap = newMap;//ww w .j  a v a2 s .c o  m
    } else {
        for (Map.Entry<String, List<String>> entry : fileMap.entrySet()) {
            List<String> oldList = entry.getValue();
            List<String> newList = newMap.get(entry.getKey());
            resultMap.put(entry.getKey(), appendData(newList, oldList));
            if (newList == null || newList.size() == 0) {
                map.put(entry.getKey(), appendData(newList, oldList));
            }
        }

        for (Map.Entry<String, List<String>> entry : newMap.entrySet()) {
            List<String> oldList = fileMap.get(entry.getKey());
            if (oldList == null || oldList.size() == 0) {
                resultMap.put(entry.getKey(), entry.getValue());
            }
        }
    }
    Files.write(JsonUtil.obj2String(resultMap).getBytes(), file);
}