com.google.common.cache.Cache.getIfPresent()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(522)

本文整理了Java中com.google.common.cache.Cache.getIfPresent()方法的一些代码示例,展示了Cache.getIfPresent()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cache.getIfPresent()方法的具体详情如下:
包路径:com.google.common.cache.Cache
类名称:Cache
方法名:getIfPresent

Cache.getIfPresent介绍

[英]Returns the value associated with key in this cache, or null if there is no cached value for key.
[中]返回与此缓存中的键关联的值,如果没有该键的缓存值,则返回null。

代码示例

代码示例来源:origin: Graylog2/graylog2-server

@Override
public V put(K key, V value) throws CacheException {
  final V old = cache.getIfPresent(key);
  cache.put(key, value);
  return old;
}

代码示例来源:origin: prestodb/presto

private <K, V> V loadValue(Cache<K, V> cache, K key, Supplier<V> valueSupplier)
{
  if (replay) {
    return Optional.ofNullable(cache.getIfPresent(key))
        .orElseThrow(() -> new PrestoException(NOT_FOUND, "Missing entry found for key: " + key));
  }
  V value = valueSupplier.get();
  cache.put(key, value);
  return value;
}

代码示例来源:origin: mpusher/mpush

@Override
public Set<RemoteRouter> lookupAll(String userId) {
  Set<RemoteRouter> cached = cache.getIfPresent(userId);
  if (cached != null) return cached;
  Set<RemoteRouter> remoteRouters = super.lookupAll(userId);
  if (remoteRouters != null) {
    cache.put(userId, remoteRouters);
  }
  return remoteRouters;
}

代码示例来源:origin: apache/hive

public static UnionStructObjectInspector getUnionStructObjectInspector(
  List<StructObjectInspector> structObjectInspectors) {
 UnionStructObjectInspector result = cachedUnionStructObjectInspector.getIfPresent(structObjectInspectors);
 if (result == null) {
  result = new UnionStructObjectInspector(structObjectInspectors);
  cachedUnionStructObjectInspector.put(structObjectInspectors, result);
 }
 return result;
}

代码示例来源:origin: apache/kylin

private SqlConverter getSqlConverter(String sourceDialect, String targetDialect) throws SQLException {
  String cacheKey = sourceDialect + "_" + targetDialect;
  SqlConverter sqlConverter = sqlConverterCache.getIfPresent(cacheKey);
  if (sqlConverter == null) {
    sqlConverter = createSqlConverter(sourceDialect, targetDialect);
    sqlConverterCache.put(cacheKey, sqlConverter);
  }
  return sqlConverter;
}

代码示例来源:origin: prestodb/presto

private static Model getOrLoadModel(Slice slice)
  {
    HashCode modelHash = ModelUtils.modelHash(slice);

    Model model = MODEL_CACHE.getIfPresent(modelHash);
    if (model == null) {
      model = ModelUtils.deserialize(slice);
      MODEL_CACHE.put(modelHash, model);
    }

    return model;
  }
}

代码示例来源:origin: apache/kylin

public ISource getCachedSource(ISourceAware aware) {
  String key = createSourceCacheKey(aware);
  ISource source = sourceMap.getIfPresent(key);
  if (source != null)
    return source;
  synchronized (this) {
    source = sourceMap.getIfPresent(key);
    if (source != null)
      return source;
    source = createSource(aware);
    sourceMap.put(key, source);
    return source;
  }
}

代码示例来源:origin: cloudfoundry/uaa

protected Set<String> getOrCreateHashList(String cacheKey) {
  Set<String> result = cache.getIfPresent(cacheKey);
  if (result==null) {
    if (cache.size()>=getMaxKeys()) {
      cache.invalidateAll();
    }
    cache.put(cacheKey, Collections.synchronizedSet(new LinkedHashSet<>()));
  }
  return cache.getIfPresent(cacheKey);
}

代码示例来源:origin: ben-manes/caffeine

@Override
public void record(long key) {
 Object value = cache.getIfPresent(key);
 if (value == null) {
  cache.put(key, key);
  policyStats.recordMiss();
 } else {
  policyStats.recordHit();
 }
}

代码示例来源:origin: apache/kylin

public Map<Integer, SqlNode> getParamNodes() {
  paramPath = SqlParamsFinder.PATH_CACHE.getIfPresent(this.sourceTmpl);
  if (paramPath == null) {
    this.paramPath = new TreeMap<>();
    genParamPath(this.sourceTmpl, new ArrayList<Integer>());
    SqlParamsFinder.PATH_CACHE.put(this.sourceTmpl, this.paramPath);
  }
  Map<Integer, SqlNode> sqlNodes = new HashMap<>();
  for (Map.Entry<Integer, List<Integer>> entry : paramPath.entrySet()) {
    List<Integer> path = entry.getValue();
    sqlNodes.put(entry.getKey(), getParamNode(path, sqlCall, 0));
  }
  return sqlNodes;
}

代码示例来源:origin: apache/rocketmq

public static BooleanExpression parse(String sql) throws MQFilterException {
  Object result = PARSE_CACHE.getIfPresent(sql);
  if (result instanceof MQFilterException) {
    throw (MQFilterException) result;
      PARSE_CACHE.put(sql, e);
      return e;
    } catch (MQFilterException t) {
      PARSE_CACHE.put(sql, t);
      throw t;
    } finally {

代码示例来源:origin: Graylog2/graylog2-server

@Override
public V get(K key, Callable<? extends V> loader) throws ExecutionException {
  V value = delegate.getIfPresent(key);
  if (value == null) {
    try {
      value = loader.call();
      delegate.put(key, value);
      cache.incrMissCount();
    } catch (Exception e) {
      throw new ExecutionException(e);
    }
  } else {
    cache.incrHitCount();
  }
  cache.incrTotalCount();
  return value;
}

代码示例来源:origin: google/guava

public void testSizeConstraint() {
 final Cache<Integer, Integer> cache = CacheBuilder.newBuilder().maximumSize(4).build();
 cache.put(1, 10);
 cache.put(2, 20);
 cache.put(3, 30);
 cache.put(4, 40);
 cache.put(5, 50);
 assertEquals(null, cache.getIfPresent(10));
 // Order required to remove dependence on access order / write order constraint.
 assertEquals(Integer.valueOf(20), cache.getIfPresent(2));
 assertEquals(Integer.valueOf(30), cache.getIfPresent(3));
 assertEquals(Integer.valueOf(40), cache.getIfPresent(4));
 assertEquals(Integer.valueOf(50), cache.getIfPresent(5));
 cache.put(1, 10);
 assertEquals(Integer.valueOf(10), cache.getIfPresent(1));
 assertEquals(Integer.valueOf(30), cache.getIfPresent(3));
 assertEquals(Integer.valueOf(40), cache.getIfPresent(4));
 assertEquals(Integer.valueOf(50), cache.getIfPresent(5));
 assertEquals(null, cache.getIfPresent(2));
}

代码示例来源:origin: apache/nifi

public synchronized void addSnippet(final StandardSnippet snippet) {
  if (snippetMap.getIfPresent(snippet.getId()) != null) {
    throw new IllegalStateException("Snippet with ID " + snippet.getId() + " already exists");
  }
  snippetMap.put(snippet.getId(), snippet);
}

代码示例来源:origin: thinkaurelius/titan

ksqs[i] = new KeySliceQuery(key,query);
EntryList result = null;
if (!isExpired(ksqs[i])) result = cache.getIfPresent(ksqs[i]);
else ksqs[i]=null;
if (result!=null) results.put(key,result);
  if (subresult!=null) {
    results.put(key,subresult);
    if (ksqs[i]!=null) cache.put(ksqs[i],subresult);

代码示例来源:origin: Netflix/EVCache

public void onStart(final EVCacheEvent e) {
  if(!enableThrottleHotKeys.get()) return;
  final Cache<String, Integer> cache = getCache(e.getAppName());
  if(cache == null) return;
  for(String key : e.getKeys()) {
    Integer val = cache.getIfPresent(key);
    if(val == null) {
      cache.put(key, START_VAL);
    } else {
      cache.put(key, Integer.valueOf(val.intValue() + 1));
    }
  }
}

代码示例来源:origin: thinkaurelius/titan

Long id;
if (types==null) {
  id = typeNamesBackup.getIfPresent(schemaName);
  if (id==null) {
    id = retriever.retrieveSchemaByName(schemaName);
    if (id!=null) { //only cache if type exists
      typeNamesBackup.put(schemaName,id);

代码示例来源:origin: Netflix/EVCache

public void onError(EVCacheEvent e, Throwable t) {
  if(!enableThrottleHotKeys.get()) return;
  final String appName = e.getAppName();
  final Cache<String, Integer> cache = getCache(appName);
  if(cache == null) return;
  for(String key : e.getKeys()) {
    Integer val = cache.getIfPresent(key);
    if(val != null) {
      cache.put(key, Integer.valueOf(val.intValue() - 1));
    }
  }
}

代码示例来源:origin: google/guava

public void testExpireAfterAccess() {
 final Cache<Integer, Integer> cache =
   CacheBuilder.newBuilder()
     .expireAfterAccess(1000, TimeUnit.MILLISECONDS)
     .ticker(fakeTicker)
     .build();
 cache.put(0, 10);
 cache.put(2, 30);
 fakeTicker.advance(999, TimeUnit.MILLISECONDS);
 assertEquals(Integer.valueOf(30), cache.getIfPresent(2));
 fakeTicker.advance(1, TimeUnit.MILLISECONDS);
 assertEquals(Integer.valueOf(30), cache.getIfPresent(2));
 fakeTicker.advance(1000, TimeUnit.MILLISECONDS);
 assertEquals(null, cache.getIfPresent(0));
}

代码示例来源:origin: Netflix/EVCache

public void onComplete(EVCacheEvent e) {
  if(!enableThrottleHotKeys.get()) return;
  final String appName = e.getAppName();
  final Cache<String, Integer> cache = getCache(appName);
  if(cache == null) return;
  for(String key : e.getKeys()) {
    Integer val = cache.getIfPresent(key);
    if(val != null) {
      cache.put(key, Integer.valueOf(val.intValue() - 1));
    }
  }
}

相关文章