Example usage for com.google.common.cache CacheBuilder newBuilder

List of usage examples for com.google.common.cache CacheBuilder newBuilder

Introduction

In this page you can find the example usage for com.google.common.cache CacheBuilder newBuilder.

Prototype

public static CacheBuilder<Object, Object> newBuilder() 

Source Link

Document

Constructs a new CacheBuilder instance with default settings, including strong keys, strong values, and no automatic eviction of any kind.

Usage

From source file:bai4.Bai4.java

/**
 * @param args the command line arguments
 *///w  w w  . j av  a  2s  . c  o m
public static void main(String[] args) {
    // TODO code application logic here
    LoadingCache<Integer, Integer> cache = CacheBuilder.newBuilder().maximumSize(100)
            .expireAfterAccess(10, TimeUnit.SECONDS).build(new CacheLoader<Integer, Integer>() {
                @Override
                public Integer load(Integer k) throws Exception {
                    return calculate(k);
                }
            });
    get("/factorial", (req, res) -> {
        ///System.out.println("Hello World");
        int n = Integer.parseInt(req.queryParams("n"));
        return cache.get(n);
    });
}

From source file:org.kaaproject.kaa.examples.powerplant.ViewConsumer.java

public static void main(String[] args) {
    System.out.println("Usage: java -jar JarName.jar clusterIP DBName design view");

    String clusterIP = null;/*from www .j  av  a 2 s. c  o  m*/
    String dbName = null;

    if (args.length != 4) {
        System.out.println("Using default options:");
        clusterIP = DEFAULT_CLUSTER_IP;
        dbName = DEFAULT_DB_NAME;
        design = DEFAULT_DESIGN;
        view = DEFAULT_VIEW;
    } else {
        clusterIP = args[0];
        dbName = args[1];
        design = args[2];
        view = args[3];
    }

    System.out.println("Cluster IP: " + DEFAULT_CLUSTER_IP);
    System.out.println("DB name: " + DEFAULT_DB_NAME);
    System.out.println("Design: " + DEFAULT_DESIGN);
    System.out.println("View: " + DEFAULT_VIEW);

    // Create a cluster reference
    CouchbaseCluster cluster = CouchbaseCluster.create(clusterIP);
    documentCache = CacheBuilder.newBuilder().initialCapacity(INITIAL_CAPACITY).maximumSize(MAX_CACHE_SIZE)
            .expireAfterWrite(TTL_SECONDS, TimeUnit.SECONDS).removalListener(new DocumentRemovalListener())
            .build();

    bucket = cluster.openBucket(dbName);

    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(new Worker());

    try {
        System.out.println("Press Enter to exit");
        System.in.read();
    } catch (IOException e) {
        e.printStackTrace();
    }

    executor.shutdownNow();
    bucket.close();
    cluster.disconnect();
}

From source file:org.mule.module.apikit.transform.TransformerCache.java

public static LoadingCache<DataTypePair, Transformer> getTransformerCache(MuleContext muleContext)
        throws RegistrationException {
    if (muleContext.getRegistry().get(REGISTRY_TRANSFORMER_CACHE_KEY) == null) {
        LoadingCache<DataTypePair, Transformer> transformerCache = CacheBuilder.newBuilder().maximumSize(1000)
                .build(new TransformerCacheLoader(muleContext));

        muleContext.getRegistry().registerObject(REGISTRY_TRANSFORMER_CACHE_KEY, transformerCache);
    }//  w ww  .ja  v a 2  s .c o  m

    return muleContext.getRegistry().get(REGISTRY_TRANSFORMER_CACHE_KEY);
}

From source file:de.rnd7.urlcache.URLCacheFactory.java

public static URLCache create(final File localCacheFolder) {
    final LoadingCache<URLCacheKey, CachedElement> cache = CacheBuilder.newBuilder()
            .expireAfterAccess(1, TimeUnit.MINUTES).build(new URLCacheLoader(localCacheFolder));

    return cache::get;
}

From source file:org.mule.module.apikit.validation.cache.JsonSchemaCache.java

public static LoadingCache<String, JsonSchemaAndNode> getJsonSchemaCache(MuleContext muleContext,
        String configId, Raml api) throws RegistrationException {
    String cacheKey = REGISTRY_JSON_SCHEMA_CACHE_KEY_PREFIX + configId;
    if (muleContext.getRegistry().get(cacheKey) == null) {
        LoadingCache<String, JsonSchemaAndNode> transformerCache = CacheBuilder.newBuilder().maximumSize(1000)
                .build(new JsonSchemaCacheLoader(api));

        muleContext.getRegistry().registerObject(cacheKey, transformerCache);
    }/*from   w  w  w. j  a v a2 s. com*/

    return muleContext.getRegistry().get(cacheKey);
}

From source file:com.google.gapid.util.Caches.java

public static <K, V> Cache<K, V> softCache() {
    return CacheBuilder.newBuilder().softValues().build();
}

From source file:org.mule.module.apikit.validation.cache.XmlSchemaCache.java

public static LoadingCache<String, Schema> getXmlSchemaCache(MuleContext muleContext, String configId, Raml api)
        throws RegistrationException {
    String cacheKey = REGISTRY_XML_SCHEMA_CACHE_KEY_PREFIX + configId;
    if (muleContext.getRegistry().get(cacheKey) == null) {
        LoadingCache<String, Schema> transformerCache = CacheBuilder.newBuilder().maximumSize(1000)
                .build(new XmlSchemaCacheLoader(api));

        muleContext.getRegistry().registerObject(cacheKey, transformerCache);
    }//w  ww. j a va2s.c o m

    return muleContext.getRegistry().get(cacheKey);
}

From source file:com.google.gapid.util.Caches.java

public static <K, V> Cache<K, V> hardCache() {
    return CacheBuilder.newBuilder().build();
}

From source file:org.terasology.cities.common.CachingFunction.java

/**
 * @param function the function to wrap/*ww w. j a va2s.c  om*/
 * @return the caching function
 */
public static <F, T> LoadingCache<F, T> wrap(Function<F, T> function) {
    return CacheBuilder.newBuilder().build(CacheLoader.from(function));
}

From source file:org.caleydo.view.domino.api.model.typed.MappingCaches.java

/**
 * create a mapping cache for converting to a fix source or targets assert that source or target is null
 * //from   w  w w  . ja  v  a2  s  . c  o m
 * @param source
 * @param target
 * @return
 */
public static LoadingCache<IDType, IIDTypeMapper<Integer, Integer>> create(IDType source, IDType target) {
    return CacheBuilder.newBuilder().build(new MapperCache(source, target));
}