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

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

Introduction

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

Prototype

public static <V> CacheLoader<Object, V> from(Supplier<V> supplier) 

Source Link

Document

Returns a cache loader based on an existing supplier instance.

Usage

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

/**
 * @param function the function to wrap/*from w w  w  . ja  v a  2  s.  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.apache.whirr.util.Utils.java

/**
 * converts a map to a loading cache.//from   w ww.  j a v a2 s  .  c  o  m
 */
public static <K, V> LoadingCache<K, V> convertMapToLoadingCache(Map<K, V> in) {
    return CacheBuilder.newBuilder().build(CacheLoader.from(Functions.forMap(in)));
}

From source file:business.GuavaUtils.java

public static <K, V> LoadingCache<K, V> makeCache(com.google.common.base.Function<K, V> cacheLoadFunction) {
    return CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.HOURS).concurrencyLevel(8).recordStats()
            .maximumSize(1000).initialCapacity(100).build(CacheLoader.from(cacheLoadFunction));
}

From source file:com.google.javascript.jscomp.bundle.CachedTransformer.java

public CachedTransformer(Source.Transformer delegate, CacheBuilder<? super Source, ? super Source> builder) {
    this.cache = builder.build(CacheLoader.from(delegate::transform));
}

From source file:net.sourcedestination.util.MemoizedFunction.java

/** modifies a function to cache its results
 * //from www  .  j a v  a2  s.  c o  m
 * @param f function to be memoized
 * @param builder a cache builder which creates a cache for mapping inputs to outputs
 * @return memozied function f
 */
public static <F, T> MemoizedFunction<F, T> memoize(final Function<F, T> f,
        final CacheBuilder<? super F, ? super T> builder) {
    final LoadingCache<F, T> cache = builder.build(CacheLoader.from(f::apply));
    return new MemoizedFunction<F, T>() {

        public T apply(final F input) {
            try {
                return cache.get(input);
            } catch (ExecutionException e) {
                //Functions can't throw checked exceptions, so this never happens
                return null;
            }
        }

        @Override
        public LoadingCache<F, T> getCache() {
            return cache;
        }

        @Override
        public Function<F, T> getOriginalFunction() {
            return f;
        }
    };
}

From source file:com.facebook.buck.core.cell.impl.DistributedCellProviderFactory.java

public static CellProvider create(DistBuildCellParams rootCell,
        ImmutableMap<Path, DistBuildCellParams> cellParams) {
    Map<String, Path> cellPaths = cellParams.values().stream().filter(p -> p.getCanonicalName().isPresent())
            .collect(Collectors.toMap(p -> p.getCanonicalName().get(), p -> p.getFilesystem().getRootPath()));
    ImmutableSet<String> declaredCellNames = ImmutableSet.copyOf(cellPaths.keySet());
    Path rootCellPath = rootCell.getFilesystem().getRootPath();
    DefaultCellPathResolver rootCellResolver = DefaultCellPathResolver.of(rootCellPath, cellPaths);

    return new CellProvider(cellProvider -> CacheLoader.from(cellPath -> {
        DistBuildCellParams cellParam = Preconditions.checkNotNull(cellParams.get(cellPath),
                "This should only be called for secondary cells.");
        Path currentCellRoot = cellParam.getFilesystem().getRootPath();
        Preconditions.checkState(!currentCellRoot.equals(rootCellPath));
        CellPathResolver currentCellResolver = rootCellResolver;
        // The CellPathResolverView is required because it makes the
        // [RootPath<->CanonicalName] resolver methods non-symmetrical to handle the
        // fact/* ww  w .  jav a  2s  .co  m*/
        // that relative main cell from inside a secondary cell resolves actually to
        // secondary cell. If the DefaultCellPathResolver is used, then it would return
        // a BuildTarget as if it belonged to the main cell.
        currentCellResolver = new CellPathResolverView(rootCellResolver, declaredCellNames, currentCellRoot);
        BuckConfig configWithResolver = cellParam.getConfig().withCellPathResolver(currentCellResolver);
        RuleKeyConfiguration ruleKeyConfiguration = ConfigRuleKeyConfigurationFactory.create(configWithResolver,
                cellParam.getBuckModuleManager());
        ToolchainProvider toolchainProvider = new DefaultToolchainProvider(cellParam.getPluginManager(),
                cellParam.getEnvironment(), configWithResolver, cellParam.getFilesystem(),
                cellParam.getProcessExecutor(), cellParam.getExecutableFinder(), ruleKeyConfiguration);

        return ImmutableCell.of(cellParams.keySet(),
                // Distributed builds don't care about cell names, use a sentinel value that
                // will show up if it actually does care about them.
                cellParam.getCanonicalName(), WatchmanFactory.NULL_WATCHMAN, cellProvider, toolchainProvider,
                ruleKeyConfiguration, cellParam.getFilesystem(), configWithResolver);
    }), cellProvider -> RootCellFactory.create(cellProvider, rootCellResolver, rootCell.getFilesystem(),
            rootCell.getBuckModuleManager(), rootCell.getPluginManager(), rootCell.getConfig(),
            rootCell.getEnvironment(), rootCell.getProcessExecutor(), rootCell.getExecutableFinder(),
            WatchmanFactory.NULL_WATCHMAN));
}

From source file:org.wso2.carbon.databridge.core.internal.authentication.session.SessionCache.java

public SessionCache(int expirationTimeInMinutes) {
    cache = CacheBuilder.newBuilder().expireAfterWrite(expirationTimeInMinutes, TimeUnit.MINUTES)
            .build(CacheLoader.from(new SessionFunction()));
}

From source file:com.facebook.buck.rules.query.QueryCache.java

public QueryCache() {
    evaluators = CacheBuilder.newBuilder().build(CacheLoader.from(CachingQueryEvaluator::new));
}

From source file:jbyoshi.robotgame.impl.GameView.java

public GameView(GameModel game, PlayerImpl player) {
    this.model = game;
    this.player = player;
    views = CacheBuilder.newBuilder().weakValues().<Model, ModelView<?>>build(
            CacheLoader.from(model -> ViewRegistry.wrap(model, this)))::getUnchecked;
}

From source file:net.sf.derquinsej.stats.CounterMap.java

/** Constructor. */
private CounterMap() {
    this.cache = CacheBuilder.newBuilder().build(CacheLoader.from(compose(Counter.creator(), constant(0L))));
    this.view = Collections.unmodifiableMap(cache.asMap());
}