您现在的位置是:主页 > news > 网络专业的网站建设/长沙企业seo服务

网络专业的网站建设/长沙企业seo服务

admin2025/6/27 18:18:00news

简介网络专业的网站建设,长沙企业seo服务,建设工程合同违约金上限,山东高端网站建设服务商1 Cache Cache缓存数据,可自动刷新数据和清除数据,通过get或getIfPresent获取数据。 构建参数: 序号参数描述1initialCapacity初始化容量,Cache继承Map2maximumSizeCache最大容量,当缓存数量达到或者接近最大值时&am…

网络专业的网站建设,长沙企业seo服务,建设工程合同违约金上限,山东高端网站建设服务商1 Cache Cache缓存数据,可自动刷新数据和清除数据,通过get或getIfPresent获取数据。 构建参数: 序号参数描述1initialCapacity初始化容量,Cache继承Map2maximumSizeCache最大容量,当缓存数量达到或者接近最大值时&am…

1 Cache

Cache缓存数据,可自动刷新数据和清除数据,通过get或getIfPresent获取数据。
构建参数:

序号参数描述
1initialCapacity初始化容量,Cache继承Map
2maximumSizeCache最大容量,当缓存数量达到或者接近最大值时,Cache清除最少使用的数据
3concurrencyLevel并发数量
4expireAfterWrite数据过期时间
5refreshAfterWrite数据刷新时间,当在此查询数据时,刷新
6maximumWeight最大权重容量,key的weight值大于该值,清除数据

2 Usage

package thirdparty;import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListenableFutureTask;
import common.constant.DigitalConstant;import java.util.concurrent.*;
import java.util.logging.Logger;import static common.constant.DigitalConstant.*;
import static common.constant.DigitalConstant.TWO;/*** Cache测试样例.** @author xindaqi* @date 2021-07-18 13:01*/
public class GuavaCacheTest {private static final Logger logger = Logger.getLogger("GuavaCacheTest");/*** 缓存:Cache*/private static Cache<String, String> cache = CacheBuilder.newBuilder().initialCapacity(DigitalConstant.ONE).maximumSize(DigitalConstant.TWO).build();/*** 初始化Cache.** @return 默认值*/private static String initializeCache() {cache.put("1", "xiaohua");cache.put("2", "xiaolan");cache.put("3", "xiaoxiao");logger.info("Load cache for initial");return "xiaoxin";}/*** 刷新Cache.** @return 默认值*/private static String reloadCache() {cache.put("1", "xiaohuahua");cache.put("2", "xiaolanlan");cache.put("3", "xiaoxiaoxiao");logger.info("Reload cache for refresh");return "xiaoxinxin";}/*** Cache默认值.** @return 默认值*/private static String cacheDefault() {return "xiaoxin";}/*** 读取Cache*/private static void readCache() {initializeCache();String val1 = cache.getIfPresent("1");logger.info("key1, value1:" + val1);String val2 = cache.getIfPresent("2");logger.info("key2, value2:" + val2);try {String val1Get = cache.get("1", GuavaCacheTest::cacheDefault);logger.info("val1:" + val1Get);} catch(ExecutionException ee) {throw new RuntimeException(ee);}}public static void main(String[] args) {readCache();}
}
  • 结果
七月 18, 2021 1:52:28 下午 thirdparty.GuavaCacheTest initializeCache
信息: Load cache for initial
七月 18, 2021 1:52:28 下午 thirdparty.GuavaCacheTest readCache
信息: key1, value1:null
七月 18, 2021 1:52:28 下午 thirdparty.GuavaCacheTest readCache
信息: key2, value2:xiaolan
七月 18, 2021 1:52:28 下午 thirdparty.GuavaCacheTest readCache
信息: val1:xiaoxin

2 读取Cache值

2.1 get

该方法先从缓存中获取数据:
(1)Cache中有值时,直接返回Cache中的值;
(2)Cache没有值时,无Key或值为Null时,返回默认值,即valueLoader的值。
因此,使用Get方法获取Cache值时,需要配置默认值,在Cache中没有查到值则返回默认值。

  • get源码
public V get(K key, final Callable<? extends V> valueLoader) throws ExecutionException {Preconditions.checkNotNull(valueLoader);return this.localCache.get(key, new CacheLoader<Object, V>(this) {public V load(Object key) throws Exception {return valueLoader.call();}});}

2.2 getIfPresent

该方法获取Cache中的值,当没有值时返回null,因此,使用该方法,通过判断值是否为null进一步逻辑处理。
该方法不会读取默认值,无法设定默认值。

  • getIfPresent源码
@NullableDeclpublic V getIfPresent(Object key) {return this.localCache.getIfPresent(key);}
  • localCache.getIfPresent源码
@NullableDeclpublic V getIfPresent(Object key) {int hash = this.hash(Preconditions.checkNotNull(key));V value = this.segmentFor(hash).get(key, hash);if (value == null) {this.globalStatsCounter.recordMisses(1);} else {this.globalStatsCounter.recordHits(1);}return value;}

3 小结

Cache:

  • get获取值,可以设定默认值;
  • getIfPresent获取值,无法设定默认值,当Cache中没有查询的数据时返回null,通过判断Cache返回的数据进一步逻辑处理。