Guava Cache - 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 Cache class.

This code example shows how to use the following methods:getIfPresent, put
This code example shows how to use the following methods:getIfPresent, put
            this.id = id;
            this.stats = stats;
        } 
 
        public T load(Factory<T> createIfNotPresent) {
            T out = cache.getIfPresent(id);
            if (out != null) {
                stats.readFromCache();
                return out;
            } 
            long start = System.currentTimeMillis();
            T value = createIfNotPresent.create();
            stats.readFromDisk(start);
            cache.put(id, value);
            return value;
        } 
    } 
} 
7
CodeRank
This code example shows how to use the following methods:put, getUnchecked
         return null; 
      String containerName = from.getContainer();
      if (containerName == null)
         return null; 
      try { 
         URI uri = cdnContainer.getUnchecked(containerName);
         if (uri == NEGATIVE_ENTRY) {  // intentionally use reference equality
            // TODO: GetCDNMetadata.load returns null on failure cases.  We use 
            // a negative entry to avoid repeatedly issuing failed CDN queries. 
            // The LoadingCache removes this value after its normal expiry. 
            return null; 
         } 
         return uriBuilder(uri).clearQuery().appendPath(from.getName()).build();
      } catch (CacheLoader.InvalidCacheLoadException e) {
         // nulls not permitted from cache loader 
         cdnContainer.put(containerName, NEGATIVE_ENTRY);
         return null; 
      } catch (NullPointerException e) {
         // nulls not permitted from cache loader 
         return null; 
      } 
7
CodeRank
This code example shows how to use the following methods:cleanUp, size, stats
    protected abstract Cache<?, ?> getCache();
 
    private CacheStats getCachedCacheStats() {
        if (cache == null || nextLoad <= System.currentTimeMillis()) {
            cache = getCache();
            cacheStats = cache.stats();
            size = cache.size();
            nextLoad = System.currentTimeMillis() + loadIterval;
        } 
         
        return cacheStats;
    } 
 
    public final long getLoadIterval() { 
        return loadIterval;
    } 
 
    /** 
     * Number of milliseconds between calls to {@link CharacterDataEventImpl#getEventCacheStats()} 
     */ 

7
CodeRank
This code example shows how to use the following methods:put, getUnchecked
         return null; 
      String containerName = from.getContainer();
      if (containerName == null)
         return null; 
      try { 
         URI uri = cdnContainer.getUnchecked(containerName);
         if (uri == NEGATIVE_ENTRY) {  // intentionally use reference equality
            // TODO: GetCDNMetadata.load returns null on failure cases.  We use 
            // a negative entry to avoid repeatedly issuing failed CDN queries. 
            // The LoadingCache removes this value after its normal expiry. 
            return null; 
         } 
         return uriBuilder(uri).clearQuery().appendPath(from.getName()).build();
      } catch (CacheLoader.InvalidCacheLoadException e) {
         // nulls not permitted from cache loader 
         cdnContainer.put(containerName, NEGATIVE_ENTRY);
         return null; 
      } catch (NullPointerException e) {
         // nulls not permitted from cache loader 
         // TODO this shouldn't occur when the above exception is reliably presented 
         return null; 
7
CodeRank
This code example shows how to use the following methods:invalidate, get
 
   protected void releaseAnyPublicIpForInstanceInRegion(String instanceId, String region) {
      if (!autoAllocateElasticIps)
         return; 
      try { 
         String ip = elasticIpCache.get(new RegionAndName(region, instanceId));
         logger.debug(">> disassociating elastic IP %s", ip);
         client.getElasticIPAddressApi().get().disassociateAddressInRegion(region, ip);
         logger.trace("<< disassociated elastic IP %s", ip);
         elasticIpCache.invalidate(new RegionAndName(region, instanceId));
         logger.debug(">> releasing elastic IP %s", ip);
         client.getElasticIPAddressApi().get().releaseAddressInRegion(region, ip);
         logger.trace("<< released elastic IP %s", ip);
      } catch (CacheLoader.InvalidCacheLoadException e) {
         // no ip was found 
         return; 
      } catch (ExecutionException e) {
         // don't propagate as we need to clean up the node regardless 
         logger.warn(e, "error cleaning up elastic ip for instance %s/%s", region, instanceId);
      } 
6
CodeRank
This code example shows how to use the following methods:getIfPresent, put
    @Override 
    public Long getSchemaId(final String schemaName, final StandardTitanTx tx) {
        ConcurrentMap<String,Long> types = typeNames;
        Long id;
        if (types==null) {
            id = typeNamesBackup.getIfPresent(schemaName);
            if (id==null) {
                id = retriever.retrieveSchemaByName(schemaName, tx);
                if (id!=null) { //only cache if type exists
                    typeNamesBackup.put(schemaName,id);
                } 
            } 
        } else { 
            id = types.get(schemaName);
            if (id==null) { //Retrieve it
                if (types.size()> maxCachedTypes) {
                    /* Safe guard against the concurrent hash map growing to large - this would be a VERY rare event 
                    as it only happens for graph databases with thousands of types. 
                     */ 
                    typeNames = null;
7
CodeRank
This code example shows how to use the following methods:asMap, size
		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());
 
		// sleep a while to expire the entry 
		Duration.milliseconds(50).sleep(); 
 
		// the entry should be considered expired now 
6
CodeRank
This code example shows how to use the following methods:getIfPresent
        this.cache = cacheBuilder.build();
    } 
 
    @Override 
    public Query get(QueryParserSettings queryString) {
        Query value =  cache.getIfPresent(queryString);
        if (value != null) { 
            return value.clone(); 
        } else { 
            return null; 
        } 
    } 
 
    @Override 
    public void put(QueryParserSettings queryString, Query query) {
        if (queryString.isCacheable()) {
            cache.put(queryString, query);
        } 
    } 
 
6
CodeRank
This code example shows how to use the following methods:getIfPresent, invalidate
	}; 
 
	final Cache<String, Download> downloads = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build();
 
	public Download get(String blobId) {
		Download download = downloads.getIfPresent(blobId);
		if (download != null) {
			downloads.invalidate(blobId);
		} 
		return download;
	} 
 
	final SecureRandom r = new SecureRandom();
 
	public String put(Download download) {
		String key;
		synchronized (r) {
			byte[] b = new byte[32];
			r.nextBytes(b);
			key = Hex.encodeHexString(b);
6
CodeRank
See Code Examples for Guava Cache Methods: