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

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

Introduction

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

Prototype

@GwtIncompatible("To be supported")
public static CacheBuilder<Object, Object> from(String spec) 

Source Link

Document

Constructs a new CacheBuilder instance with the settings specified in spec .

Usage

From source file:com.eucalyptus.auth.euare.CachingPrincipalProvider.java

private static Cache<PrincipalCacheKey, PrincipalCacheValue> cache(final String cacheSpec) {
    return CacheBuilder.from(CacheBuilderSpec.parse(cacheSpec)).build();
}

From source file:org.grouplens.lenskit.data.sql.JDBCRatingDAOBuilder.java

/**
 * Set a cache builder spec to use for making the DAO's internal caches.
 * @param spec A cache builder spec./*w ww . j  a v a2s .  c o m*/
 * @return The builder (for chaining).
 */
public JDBCRatingDAOBuilder setCacheBuilder(CacheBuilderSpec spec) {
    return setCacheBuilder(CacheBuilder.from(spec));
}

From source file:com.eucalyptus.tokens.oidc.OidcDiscoveryCache.java

private Cache<String, OidcDiscoveryCachedResource> cache(final String cacheSpec) {
    Cache<String, OidcDiscoveryCachedResource> cache;
    final Pair<String, Cache<String, OidcDiscoveryCachedResource>> cachePair = cacheReference.get();
    if (cachePair == null || !cacheSpec.equals(cachePair.getLeft())) {
        final Pair<String, Cache<String, OidcDiscoveryCachedResource>> newCachePair = Pair.pair(cacheSpec,
                CacheBuilder.from(CacheBuilderSpec.parse(cacheSpec)).build());
        if (cacheReference.compareAndSet(cachePair, newCachePair) || cachePair == null) {
            cache = newCachePair.getRight();
        } else {//from  w w  w  . j  av  a  2s.  c o m
            cache = cachePair.getRight();
        }
    } else {
        cache = cachePair.getRight();
    }
    return cache;
}

From source file:com.eucalyptus.compute.metadata.VmMetadata.java

private static <K, V> LoadingCache<K, V> cache(final Function<K, V> loader, final String cacheSpec) {
    return CacheBuilder.from(CacheBuilderSpec.parse(cacheSpec)).build(CacheLoader.from(loader));
}

From source file:net.librec.recommender.cf.ranking.WBPRRecommender.java

/**
 * cache each user's candidate items with probabilities
 *
 * @return cache each user's candidate items with probabilities
 *//*  w ww.  java  2s  . c o m*/
private LoadingCache<Integer, List<Map.Entry<Integer, Double>>> getCacheItemProbs() {
    LoadingCache<Integer, List<Map.Entry<Integer, Double>>> cache = CacheBuilder.from(cacheSpec)
            .build(new CacheLoader<Integer, List<Map.Entry<Integer, Double>>>() {

                @Override
                public List<Map.Entry<Integer, Double>> load(Integer u) throws Exception {
                    List<Map.Entry<Integer, Double>> itemProbs = new ArrayList<>();

                    Set<Integer> ratedItemsSet = userItemsSet.get(u);

                    // filter candidate items
                    double sum = 0;
                    for (Map.Entry<Integer, Double> itemPop : sortedItemPops) {
                        Integer itemIdx = itemPop.getKey();
                        double popularity = itemPop.getValue();

                        if (!ratedItemsSet.contains(itemIdx) && popularity > 0) {
                            // make a clone to prevent bugs from normalization
                            itemProbs.add(itemPop);
                            sum += popularity;
                        }
                    }

                    // normalization
                    for (Map.Entry<Integer, Double> itemProb : itemProbs) {
                        itemProb.setValue(itemProb.getValue() / sum);
                    }

                    return itemProbs;
                }
            });
    return cache;
}

From source file:com.heliosapm.easymq.cache.CacheService.java

/**
 * Returns (creating if necessary) instance caches for the passed MQ instance key
 * @param key The instance key/*from  ww w  .j a v a2s.co m*/
 * @return the instance caches map
 */
public ConcurrentHashMap<String, Cache<?, ?>> getCachesForMQInstance(final String key) {
    ConcurrentHashMap<String, Cache<?, ?>> allInstanceCaches = caches.get(key);
    if (allInstanceCaches == null) {
        synchronized (caches) {
            allInstanceCaches = caches.get(key);
            if (allInstanceCaches == null) {
                final StringBuilder b = new StringBuilder("===Created instance caches for [").append(key)
                        .append("]:");
                allInstanceCaches = new ConcurrentHashMap<String, Cache<?, ?>>(64, 0.75f,
                        Runtime.getRuntime().availableProcessors());
                for (final Map.Entry<String, String> entry : instanceSpecs.entrySet()) {
                    final DelegatingRemovalListener<?, ?> listener = new DelegatingRemovalListener<Object, Object>();
                    final Cache<?, ?> cache = CacheBuilder.from(entry.getValue()).removalListener(listener)
                            .build();
                    registerCacheMBean(key, entry.getKey(), cache, listener);
                    allInstanceCaches.put(entry.getKey(), cache);
                    b.append("\n\t").append(entry.getKey());
                }
                b.append("\n===");
                caches.put(key, allInstanceCaches);
                log.info(b.toString());
            }
        }
    }
    return allInstanceCaches;
}

From source file:com.heliosapm.easymq.cache.CacheService.java

/**
 * Returns (creating if necessary) the named instance cache for the passed MQ instance key and cache name
 * @param poolKey The instance key//w  w  w.j  a  v  a2 s .co m
 * @param cacheName The cache name
 * @return the named cache
 */
public Cache<?, ?> getNamedInstanceCache(final String poolKey, final String cacheName) {
    final ConcurrentHashMap<String, Cache<?, ?>> instanceCache = getCachesForMQInstance(poolKey);
    Cache<?, ?> cache = instanceCache.get(cacheName);
    if (cache == null) {
        synchronized (instanceCache) {
            cache = instanceCache.get(cacheName);
            if (cache == null) {
                final DelegatingRemovalListener<?, ?> listener = new DelegatingRemovalListener<Object, Object>();

                cache = CacheBuilder.from(defaultCacheSpec(null, enableJmx)).removalListener(listener).build();
                registerCacheMBean(poolKey, cacheName, cache, listener);
                instanceCache.put(cacheName, cache);
            }
        }
    }
    return cache;
}

From source file:librec.data.SparseMatrix.java

/**
 * create a row cache of a matrix in {row, row-specific vector}
 * /*from   w w  w .j a  v a2 s  .com*/
 * @param cacheSpec
 *            cache specification
 * @return a matrix row cache in {row, row-specific vector}
 */
public LoadingCache<Integer, SparseVector> rowCache(String cacheSpec) {
    LoadingCache<Integer, SparseVector> cache = CacheBuilder.from(cacheSpec)
            .build(new CacheLoader<Integer, SparseVector>() {

                @Override
                public SparseVector load(Integer rowId) throws Exception {
                    return row(rowId);
                }
            });

    return cache;
}

From source file:com.comcast.cdn.traffic_control.traffic_router.core.dns.ZoneManager.java

private static LoadingCache<ZoneKey, Zone> createZoneCache(final ZoneCacheType cacheType,
        final CacheBuilderSpec spec) {
    final RemovalListener<ZoneKey, Zone> removalListener = new RemovalListener<ZoneKey, Zone>() {
        public void onRemoval(final RemovalNotification<ZoneKey, Zone> removal) {
            LOGGER.debug(cacheType + " " + removal.getKey().getClass().getSimpleName() + " "
                    + removal.getKey().getName() + " evicted from cache: " + removal.getCause());
        }/*from  ww  w  . j a  va  2 s.  c o  m*/
    };

    return CacheBuilder.from(spec).recordStats().removalListener(removalListener)
            .build(new CacheLoader<ZoneKey, Zone>() {
                final boolean writeZone = (cacheType == ZoneCacheType.STATIC) ? true : false;

                public Zone load(final ZoneKey zoneKey) throws IOException, GeneralSecurityException {
                    LOGGER.debug("loading " + cacheType + " " + zoneKey.getClass().getSimpleName() + " "
                            + zoneKey.getName());
                    return loadZone(zoneKey, writeZone);
                }

                public ListenableFuture<Zone> reload(final ZoneKey zoneKey, final Zone prevZone)
                        throws IOException, GeneralSecurityException {
                    final ListenableFutureTask<Zone> zoneTask = ListenableFutureTask
                            .create(new Callable<Zone>() {
                                public Zone call() throws IOException, GeneralSecurityException {
                                    return loadZone(zoneKey, writeZone);
                                }
                            });

                    zoneExecutor.execute(zoneTask);

                    return zoneTask;
                }
            });
}

From source file:librec.data.SparseMatrix.java

/**
 * create a row cache of a matrix in {row, row-specific columns}
 * /*from   ww  w. j  av  a  2  s  .  co  m*/
 * @param cacheSpec
 *            cache specification
 * @return a matrix row cache in {row, row-specific columns}
 */
public LoadingCache<Integer, List<Integer>> rowColumnsCache(String cacheSpec) {
    LoadingCache<Integer, List<Integer>> cache = CacheBuilder.from(cacheSpec)
            .build(new CacheLoader<Integer, List<Integer>>() {

                @Override
                public List<Integer> load(Integer rowId) throws Exception {
                    return getColumns(rowId);
                }
            });

    return cache;
}