Example usage for com.google.common.cache CacheLoader CacheLoader

List of usage examples for com.google.common.cache CacheLoader CacheLoader

Introduction

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

Prototype

protected CacheLoader() 

Source Link

Document

Constructor for use by subclasses.

Usage

From source file:bai4.Bai4.java

/**
 * @param args the command line arguments
 */// w  w w  . java  2s.com
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:rickbw.incubator.cache.TimedEvictionCacheBuilderExample.java

public static void main(final String... args) {
    final LoadingCache<String, Object> cache = TimedEvictionCacheBuilder.forKeysOfType(String.class)
            // After being written, ...
            .evictAfterWrite()/*from  ww w .j av  a 2 s .c  om*/
            // ...20% of values will expire after 17 minutes:
            .period(17, TimeUnit.MINUTES, 20)
            // ...30% will expire after 31 minutes:
            .period(31, TimeUnit.MINUTES, 30)
            // ...and the remaining 50% will expire after 1 hour:
            .period(1, TimeUnit.HOURS, 50).build(new CacheLoader<String, Object>() {
                @Override
                public Object load(final String key) {
                    // Imagine something expensive here...
                    return key.trim().toUpperCase();
                }
            });

    final Object cached = cache.getUnchecked("Hello, World");
    assert cached != null;
}

From source file:org.sprinkle.CacheTool.java

public static <K, V> Cache<K, CompletableFuture<Try<V>>> constructCache(
        CacheBuilder<K, CompletableFuture<Try<V>>> cacheBuilder,
        Function<K, CompletableFuture<Try<V>>> future) {
    return cacheBuilder.build(new CacheLoader<K, CompletableFuture<Try<V>>>() {
        public CompletableFuture<Try<V>> load(K key) { // no checked exception
            return future.apply(key);
        }//from www  .ja v a  2  s  .  c om
    });
}

From source file:org.wso2.carbon.logging.appender.StreamDefinitionCache.java

private static void init() {
    if (streamIdLoadingCache != null) {
        return;//from   w  w  w .  j  a va 2s . com
    }
    synchronized (StreamDefinitionCache.class) {
        if (streamIdLoadingCache != null) {
            return;
        }
        streamIdLoadingCache = CacheBuilder.newBuilder().maximumSize(1000)
                .expireAfterAccess(120, TimeUnit.MINUTES).build(new CacheLoader<String, StreamData>() {
                    @Override
                    public StreamData load(String tenantId) throws Exception {
                        return new StreamData(tenantId, "");
                    }
                });
    }

}

From source file:org.wso2.carbon.logging.service.appender.StreamDefinitionCache.java

private static void init() {
    if (streamIdLoadingCache != null) {
        return;//  w w  w  .  j a v  a2 s . c om
    }
    synchronized (StreamDefinitionCache.class) {
        if (streamIdLoadingCache != null) {
            return;
        }
        streamIdLoadingCache = CacheBuilder.newBuilder().maximumSize(1000)
                .expireAfterAccess(120, TimeUnit.MINUTES).build(new CacheLoader<String, StreamData>() {
                    @Override
                    public StreamData load(String tenantId) throws Exception {
                        return new StreamData(tenantId, "", "");
                    }
                });
    }

}

From source file:com.palantir.atlasdb.cleaner.CachingPuncherStore.java

public static CachingPuncherStore create(final PuncherStore puncherStore, long granularityMillis) {
    LoadingCache<Long, Long> timeMillisToTimestamp = CacheBuilder.newBuilder().maximumSize(CACHE_SIZE)
            .<Long, Long>build(new CacheLoader<Long, Long>() {
                @Override// w  w w .  j  a v a 2  s .c o  m
                public Long load(Long timeMillis) throws Exception {
                    return puncherStore.get(timeMillis);
                }
            });
    return new CachingPuncherStore(puncherStore, timeMillisToTimestamp, granularityMillis);
}

From source file:com.android.tools.idea.gradle.project.GradleProjectDependencyParser.java

@NotNull
public static Function<VirtualFile, Iterable<String>> newInstance(@NotNull final Project project) {
    return CacheBuilder.newBuilder().build(new CacheLoader<VirtualFile, Iterable<String>>() {
        @Override//from   w  w w.ja v a  2s  .co  m
        public Iterable<String> load(@NotNull VirtualFile key) throws Exception {
            return parse(key, project);
        }
    });
}

From source file:com.lithium.flow.util.Caches.java

@Nonnull
public static <K, V> CacheLoader<K, V> loader(@Nonnull CheckedFunction<K, V, Exception> function) {
    checkNotNull(function);//  ww  w  .  j  av a2s . c  om
    return new CacheLoader<K, V>() {
        @Override
        @Nonnull
        public V load(@Nonnull K key) throws Exception {
            return function.apply(key);
        }
    };
}

From source file:edu.illinois.cs.cogcomp.wikifier.utils.datastructure.LRUCache.java

public LRUCache(int maxSize) {
    cache = CacheBuilder.newBuilder().maximumSize(maxSize).build(new CacheLoader<K, V>() {
        @Override//from  www . j a v a  2  s.c  o  m
        public V load(K k) throws Exception {
            return loadValue(k);
        }

    });
}

From source file:myDarkDiary.service.service.LoginAttemptService.java

public LoginAttemptService() {
    super();//from  w ww  .ja  va2 s.c  om
    attemptsCache = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.DAYS)
            .build(new CacheLoader<String, Integer>() {
                @Override
                public Integer load(String key) {
                    return 0;
                }
            });
}