gpt4 book ai didi

java - Cache.asMap().put() 或 Cache.put()

转载 作者:行者123 更新时间:2023-12-01 19:39:14 27 4
gpt4 key购买 nike

我正在使用 Google-guava-cache。将其定义为:

Cache<String, String> myCache= CacheBuilder.newBuilder().maximumSize(100).build();  

当我执行 map 操作(例如获取/放置)时,使用:

  myCache.asMap().put("someString", "someString");
String someValueFromCache = myCache.asMap().get("someString");

我想知道如果我按照以下方式使用它,即不将其用作 map ,是否会有任何优化或任何类型的差异:

  myCache.put("someString", "someString");
String someValueFromCache = myCache.get("someString");

最佳答案

来自 Guava 缓存文档:https://github.com/google/guava/wiki/CachesExplained#inserted-directly

Values may be inserted into the cache directly with cache.put(key, value). This overwrites any previous entry in the cache for the specified key. Changes can also be made to a cache using any of the ConcurrentMap methods exposed by the Cache.asMap() view. Note that no method on the asMap view will ever cause entries to be automatically loaded into the cache. Further, the atomic operations on that view operate outside the scope of automatic cache loading, so Cache.get(K, Callable) should always be preferred over Cache.asMap().putIfAbsent in caches which load values using either CacheLoader or Callable.

不要认为您应该使用 asMap() View 来修改缓存。

关于java - Cache.asMap().put() 或 Cache.put(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55914135/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com