LoadingCache<Map<String, String>, String> xxxx= CacheBuilder.newBuilder() //设置并发级别为10,并发级别是指可以同时写缓存的线程数 .concurrencyLevel(10) //设置写缓存后10分钟没有写操作就重新加载(异步,不阻塞) .refreshAfterWrite(10, TimeUnit.MINUTES) //设置写缓存后12分钟没有写操作就失效 .expireAfterWrite(12, TimeUnit.MINUTES) //设置缓存容器的初始容量为100 .initialCapacity(100) //设置缓存最大容量为10000,超过10000之后就会按照LRU最近虽少使用算法来移除缓存项 .maximumSize(10000) //设置要统计缓存的命中率 .recordStats() //设置缓存的移除通知 .removalListener(new RemovalListener<Object, Object>() { @Override public void onRemoval(RemovalNotification<Object, Object> notification) { } }).build(//build方法中可以指定CacheLoader,在缓存不存在时通过CacheLoader的实现自动加载缓存 new CacheLoader<Map<String, String>, String>() { @Override public String load(Map<String, String> map) throws Exception { String value = redis.hget(map.get("key"), map.get("field")); return StringUtils.defaultIfBlank(value, StringUtils.EMPTY); } } );
本地缓存
最新推荐文章于 2024-06-18 07:59:58 发布