Java Code Examples for com.google.common.cache.Cache#get()

The following examples show how to use com.google.common.cache.Cache#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CacheUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
static
public <K extends PMMLObject, V> V getValue(K key, Cache<K, V> cache, Callable<? extends V> loader){

	try {
		return cache.get(key, loader);
	} catch(ExecutionException | UncheckedExecutionException e){
		Throwable cause = e.getCause();

		if(cause instanceof PMMLException){
			throw (PMMLException)cause;
		}

		throw new InvalidElementException(key)
			.initCause(cause);
	}
}
 
Example 2
Source File: CodeGenerationBenchmark.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Benchmarks the part of creating Bindable instances from
 * {@link EnumerableInterpretable#getBindable(ClassDeclaration, String, int)}
 * method with an additional cache layer.
 */
@Benchmark
public Bindable<?> getBindableWithCache(
    QueryState jState,
    CacheState chState) throws Exception {
  PlanInfo info = jState.planInfos[jState.nextPlan()];
  Cache<String, Bindable> cache = chState.cache;

  EnumerableInterpretable.StaticFieldDetector detector =
      new EnumerableInterpretable.StaticFieldDetector();
  info.classExpr.accept(detector);
  if (!detector.containsStaticField) {
    return cache.get(
        info.javaCode,
        () -> (Bindable) info.cbe.createInstance(new StringReader(info.javaCode)));
  }
  throw new IllegalStateException("Benchmark queries should not arrive here");
}
 
Example 3
Source File: CachingDiscoveryProvider.java    From endpoints-java with Apache License 2.0 6 votes vote down vote up
private <T> T getDiscoveryDoc(Cache<ApiKey, T> cache, String root, String name, String version,
    Callable<T> loader) throws NotFoundException, InternalServerErrorException {
  ApiKey key = new ApiKey(name, version, root);
  try {
    return cache.get(key, loader);
  } catch (ExecutionException | UncheckedExecutionException e) {
    // Cast here so we can maintain specific errors for documentation in throws clauses.
    if (e.getCause() instanceof NotFoundException) {
      throw (NotFoundException) e.getCause();
    } else if (e.getCause() instanceof InternalServerErrorException) {
      throw (InternalServerErrorException) e.getCause();
    } else {
      logger.atSevere().withCause(e.getCause()).log("Could not generate or cache discovery doc");
      throw new InternalServerErrorException("Internal Server Error", e.getCause());
    }
  }
}
 
Example 4
Source File: GuavaLevel1CacheProvider.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T get(String cacheName,String key){
	try {			
		Cache<String, Object> cache = getCacheHolder(cacheName);
		if(cache != null){
			Object result = cache.get(key, new Callable<Object>() {
				@Override
				public Object call() throws Exception {
					return _NULL;
				}
			});
			if(result != null && !_NULL.equals(result)){
				return (T)result;
			}
		}
	} catch (Exception e) {
		logger.warn("get LEVEL1 cache error",e);
	}
	return null;
}
 
Example 5
Source File: GuavaLevel1CacheProvider.java    From azeroth with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T get(String cacheName, String key) {
    try {
        Cache<String, Object> cache = getCacheHolder(cacheName);
        if (cache != null) {
            Object result = cache.get(key, new Callable<Object>() {
                @Override
                public Object call() throws Exception {
                    return _NULL;
                }
            });
            if (result != null && !_NULL.equals(result)) {
                return (T) result;
            }
        }
    } catch (Exception e) {
        logger.warn("get LEVEL1 cache error", e);
    }
    return null;
}
 
Example 6
Source File: SchemaCache.java    From tx-lcn with Apache License 2.0 5 votes vote down vote up
private Schema<?> get(final Class<?> cls, Cache<Class<?>, Schema<?>> cache) {
    try {
        return cache.get(cls, () -> RuntimeSchema.createFrom(cls,idStrategy));
    } catch (ExecutionException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example 7
Source File: SingleBuildActionRuleKeyCache.java    From buck with Apache License 2.0 5 votes vote down vote up
private <K> V getInternal(Cache<K, V> cache, K key, Function<K, V> create) {
  try {
    return cache.get(key, () -> create.apply(key));
  } catch (ExecutionException e) {
    throw new RuntimeException(e);
  }
}
 
Example 8
Source File: BitsetFilterCache.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private BitSet getAndLoadIfNotPresent(final Query query, final LeafReaderContext context) throws IOException, ExecutionException {
    final Object coreCacheReader = context.reader().getCoreCacheKey();
    final ShardId shardId = ShardUtils.extractShardId(context.reader());
    if (shardId != null // can't require it because of the percolator
            && index.getName().equals(shardId.getIndex()) == false) {
        // insanity
        throw new IllegalStateException("Trying to load bit set for index [" + shardId.getIndex()
                + "] with cache of index [" + index.getName() + "]");
    }
    Cache<Query, Value> filterToFbs = loadedFilters.get(coreCacheReader, new Callable<Cache<Query, Value>>() {
        @Override
        public Cache<Query, Value> call() throws Exception {
            context.reader().addCoreClosedListener(BitsetFilterCache.this);
            return CacheBuilder.newBuilder().build();
        }
    });
    return filterToFbs.get(query,new Callable<Value>() {
        @Override
        public Value call() throws Exception {
            final IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(context);
            final IndexSearcher searcher = new IndexSearcher(topLevelContext);
            searcher.setQueryCache(null);
            final Weight weight = searcher.createNormalizedWeight(query, false);
            final Scorer s = weight.scorer(context);
            final BitSet bitSet;
            if (s == null) {
                bitSet = null;
            } else {
                bitSet = BitSet.of(s.iterator(), context.reader().maxDoc());
            }

            Value value = new Value(bitSet, shardId);
            listener.onCache(shardId, value.bitset);
            return value;
        }
    }).bitset;
}
 
Example 9
Source File: SchemaCache.java    From myth with Apache License 2.0 5 votes vote down vote up
private Schema<?> get(final Class<?> cls, final Cache<Class<?>, Schema<?>> cache) {
    try {
        return cache.get(cls, () -> RuntimeSchema.createFrom(cls));
    } catch (ExecutionException e) {
        return null;
    }
}
 
Example 10
Source File: SchemaCache.java    From Lottor with MIT License 5 votes vote down vote up
private Schema<?> get(final Class<?> cls, Cache<Class<?>, Schema<?>> cache) {
    try {
        return cache.get(cls, () -> RuntimeSchema.createFrom(cls));
    } catch (ExecutionException e) {
        return null;
    }
}
 
Example 11
Source File: CacheUtils.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
static <K, V> V getUnchecked(Cache<K, V> cache, K key, Supplier<V> supplier) {
    try {
        return cache.get(key, supplier::get);
    } catch(ExecutionException e) {
        throw Throwables.propagate(e);
    }
}
 
Example 12
Source File: SchemaCache.java    From hmily with Apache License 2.0 5 votes vote down vote up
private Schema<?> get(final Class<?> cls, final Cache<Class<?>, Schema<?>> cache) {
    try {
        return cache.get(cls, () -> RuntimeSchema.createFrom(cls));
    } catch (ExecutionException e) {
        return null;
    }
}
 
Example 13
Source File: SchemaCache.java    From tx-lcn with Apache License 2.0 5 votes vote down vote up
private Schema<?> get(final Class<?> cls, Cache<Class<?>, Schema<?>> cache) {
    try {
        return cache.get(cls, new Callable() {
            @Override
            public Object call() throws Exception {
                return RuntimeSchema.createFrom(cls);
            }
        });
    } catch (ExecutionException e) {
        return null;
    }
}
 
Example 14
Source File: SchemaCache.java    From Raincat with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Schema<?> get(final Class<?> cls, final Cache<Class<?>, Schema<?>> cache) {
    try {
        return cache.get(cls, () -> RuntimeSchema.createFrom(cls));
    } catch (ExecutionException e) {
        return null;
    }
}
 
Example 15
Source File: SchemaCache.java    From Raincat with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Schema<?> get(final Class<?> cls, Cache<Class<?>, Schema<?>> cache) {
    try {
        return cache.get(cls, () -> RuntimeSchema.createFrom(cls));
    } catch (ExecutionException e) {
        return null;
    }
}
 
Example 16
Source File: UnitTest.java    From albert with MIT License 5 votes vote down vote up
public void getNameFromLocalCache() throws Exception{
    //new一个cache的对象出来
    Cache<String/*name*/,String/*nick*/> cache = CacheBuilder.newBuilder().maximumSize(10).build();
    //在get的时候,如果缓存里面没有,则通过实现一个callback的方法去获取
    String name = cache.get("bixiao", new Callable<String>() {
        public String call() throws Exception {
            return "bixiao.zy"+"-"+"iamzhongyong";
        }
    });
    System.out.println(name);
    System.out.println(cache.toString());
}
 
Example 17
Source File: SchemaCache.java    From Lottor with MIT License 5 votes vote down vote up
private Schema<?> get(final Class<?> cls, Cache<Class<?>, Schema<?>> cache) {
    try {
        return cache.get(cls, () -> RuntimeSchema.createFrom(cls));
    } catch (ExecutionException e) {
        return null;
    }
}