Guava CacheBuilder - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Guava CacheBuilder class.

This code example shows how to use the following methods:build, concurrencyLevel, expireAfterAccess, maximumSize
    public static final int DEFAULT_MAX_ENTRIES = 25000;
 
    @Override 
    public <K extends Serializable, V extends Serializable> Cache<K, V> getCache(String cacheName) {
 
        Cache<K, V> cache = CacheBuilder.newBuilder().weakValues()
                .concurrencyLevel(DEFAULT_CONCURRENCY_LEVEL)
                .expireAfterAccess(DEFAULT_EXPIRATION_MINUTES, TimeUnit.MINUTES)
                .maximumSize(DEFAULT_MAX_ENTRIES).build();
 
        return cache;
    } 
 
    public static CacheProvider findProvider() { 
        CacheProvider cacheProvider = GeoServerExtensions.bean(CacheProvider.class);
        if (cacheProvider == null) {
            cacheProvider = new DefaultCacheProvider();
        } 
        return cacheProvider;
    } 
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:build, expireAfterAccess
        this.expire = componentSettings.getAsTime("expire", null);
        logger.debug("using [resident] query cache with max_size [{}], expire [{}]", maxSize, expire);
 
        CacheBuilder cacheBuilder = CacheBuilder.newBuilder().maximumSize(maxSize);
        if (expire != null) {
            cacheBuilder.expireAfterAccess(expire.nanos(), TimeUnit.NANOSECONDS);
        } 
 
        this.cache = cacheBuilder.build();
    } 
 
    @Override 
    public Query get(QueryParserSettings queryString) {
        Query value =  cache.getIfPresent(queryString);
        if (value != null) { 
            return value.clone(); 
        } else { 
            return null; 
        } 
    } 
7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:build, initialCapacity, maximumSize
        maxCachedTypes = size;
        maxCachedRelations = maxCachedTypes *CACHE_RELATION_MULTIPLIER;
        this.retriever=retriever;
 
        typeNamesBackup = CacheBuilder.newBuilder()
                .concurrencyLevel(CONCURRENCY_LEVEL).initialCapacity(INITIAL_CACHE_SIZE)
                .maximumSize(maxCachedTypes).build();
        typeNames = new ConcurrentHashMap<String, Long>(INITIAL_CAPACITY,0.75f,CONCURRENCY_LEVEL);
 
        schemaRelationsBackup = CacheBuilder.newBuilder()
                .concurrencyLevel(CONCURRENCY_LEVEL).initialCapacity(INITIAL_CACHE_SIZE *CACHE_RELATION_MULTIPLIER)
                .maximumSize(maxCachedRelations).build();
//        typeRelations = new ConcurrentHashMap<Long, EntryList>(INITIAL_CAPACITY*CACHE_RELATION_MULTIPLIER,0.75f,CONCURRENCY_LEVEL); 
        schemaRelations = new NonBlockingHashMapLong<EntryList>(INITIAL_CAPACITY*CACHE_RELATION_MULTIPLIER); //TODO: Is this data structure safe or should we go with ConcurrentHashMap (line above)?
    } 
 
 
    @Override 
    public Long getSchemaId(final String schemaName, final StandardTitanTx tx) {
        ConcurrentMap<String,Long> types = typeNames;
7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:build, concurrencyLevel
        this.store = store;
        this.serializer = serializer;
        this.exec = exec;
        blocked = CacheBuilder.newBuilder()
                .expireAfterWrite(cooldownTime, cooldownUnit)
                .concurrencyLevel(COOLDOWN_CONCURRENCY_LEVEL)
                .<KeyColumn, Long>build()
                .asMap(); 
    } 
 
    public StandardLockCleanerService(KeyColumnValueStore store, ConsistentKeyLockerSerializer serializer) {
        this (store, serializer, getDefaultExecutor(), COOLDOWN_TIME, COOLDOWN_UNIT);
    } 
 
    @Override 
    public void clean(KeyColumn target, long cutoff, StoreTransaction tx) {
        Long b = blocked.putIfAbsent(target, cutoff);
        if (null == b) {
            log.info("Enqueuing expired lock cleaner task for target={}, tx={}, cutoff={}", 
                    new Object[] { target, tx, cutoff }); 
7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:build
public class JarCache { 
    private final Lock lock = new ReentrantLock();
    private final Cache<File, FileInfo> cachedFiles;
 
    public JarCache() { 
        this.cachedFiles = CacheBuilder.newBuilder().maximumSize(200).build();
    } 
 
    /** 
     * Returns a cached copy of the given file. The cached copy is guaranteed to not be modified or removed. 
     * 
     * @param original The source file. 
     * @param baseDirFactory A factory that can provide a base directory for the file cache. 
     * @return The cached file. 
     */ 
    public File getCachedJar(File original, Factory<File> baseDirFactory) {
        File source = GFileUtils.canonicalise(original);
        FileInfo fileInfo;
        // TODO - do some of this work concurrently 
        lock.lock();
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:build
         @Named(PROPERTY_SESSION_INTERVAL) long sessionIntervalSeconds) {
      this.imageCache = checkNotNull(imageCache, "imageCache");
      // We use a cache to let the entries in the "uncached" set expire as soon as the image cache expires. We want the 
      // uncached set to be regenerated when the original cache is also regenerated. 
      this.uncachedImages = CacheBuilder.newBuilder().expireAfterWrite(sessionIntervalSeconds, TimeUnit.SECONDS)
            .build();
   } 
 
   @Override 
   public Set<? extends Image> get() {
      return ImmutableSet.copyOf(concat(imageCache.get(), uncachedImages.asMap().values()));
   } 
 
   /** 
    * Registers a new image in the image cache. 
    * <p> 
    * This method should be called to register new images into the image cache, when some image that is known to exist 
    * in the provider is still not cached. For example, this can happen when an image is created after the image cache 
    * has been populated for the first time. 
    * <p> 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:build, maximumSize
    private final Cache<Method, Map<List<?>, Object>> caches;
 
    private Memoizer(final Object object) { 
        this.object = object;
        caches = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.HOURS)
                .maximumSize(250).build();
    } 
 
    @Override 
    public Object invoke(final Object proxy, final Method method, final Object[] args)
            throws Throwable {
 
        if (method.getReturnType().equals(Void.TYPE)) {
            // Don't cache void methods 
            return invoke(method, args);
        } 
        final Map<List<?>, Object> cache = getCache(method);
        final List<?> key = Arrays.asList(args);
        Object value = cache.get(key);
 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:build
public class ZoneIdToZoneSupplier implements Supplier<LoadingCache<String, Zone>> {
   private final LoadingCache<String, Zone> cache;
 
   @Inject 
   public ZoneIdToZoneSupplier(CacheLoader<String, Zone> zoneIdToZone, @Named(PROPERTY_SESSION_INTERVAL) long expirationSecs) {
      cache = CacheBuilder.newBuilder().expireAfterWrite(expirationSecs, TimeUnit.SECONDS).build(zoneIdToZone);
   } 
 
   @Override 
   public LoadingCache<String, Zone> get() {
      return cache;
   } 
} 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:build
 */ 
public class EvictingSetTest extends Assert
{ 
	@Test
	public void makeSureEntriesExpireEvenWhenUsingIndirectAccessToCache() { 
		Cache<String, Boolean> cache = CacheBuilder.newBuilder()
				.expireAfterAccess(10, TimeUnit.MILLISECONDS)
				.build();
 
		// create evicting Set out of the Cache data structures 
		Set<String> set = Sets.newSetFromMap(cache.asMap());
 
		// there are no entries initially 
		assertEquals(0, set.size());
 
		// add an entry directly in the Set (do not use Cache APIs) 
		set.add("value");
 
		// make sure the cache sees the new entry 
		assertEquals(1, cache.size());
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:build
   private final LoadingCache<RegionAndName, Image> cache;
 
   @Inject 
   protected RegionAndNameToImageSupplier(CacheLoader<RegionAndName, Image> regionAndIdToImage, 
            @Named(PROPERTY_SESSION_INTERVAL) long expirationSecs) {
      cache = CacheBuilder.newBuilder().expireAfterWrite(expirationSecs,  TimeUnit.SECONDS).build(regionAndIdToImage);
   } 
 
   @Override 
   public LoadingCache<RegionAndName, ? extends Image> get() {
      return cache;
   } 
} 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
See Code Examples for Guava CacheBuilder Methods: