Guava Cache
guava cache是一个本地缓存。
优点
缺点
public static void main(String[] args) throws ExecutionException, InterruptedException{
//缓存接口这里是LoadingCache,LoadingCache在缓存项不存在时可以自动加载缓存
LoadingCache studentCache
//CacheBuilder的构造函数是私有的,只能通过其静态方法newBuilder()来获得CacheBuilder的实例
= CacheBuilder.newBuilder()
//设置并发级别为8,并发级别是指可以同时写缓存的线程数
.concurrencyLevel(8)
//设置写缓存后8秒钟过期
.expireAfterWrite(8, TimeUnit.SECONDS)
//设置缓存容器的初始容量为10
.initialCapacity(10)
//设置缓存最大容量为100,超过100之后就会按照LRU最近虽少使用算法来移除缓存项
.maximumSize(100)
//设置要统计缓存的命中率
.recordStats()
//设置缓存的移除通知
.removalListener(new RemovalListener
输出结果:
cache stats:
CacheStats{hitCount=17, missCount=3, loadSuccessCount=3, loadExceptionCount=0, totalLoadTime=1348802, evictionCount=2}
原因:看看到在20此循环中命中次数是17次,未命中3次,这是因为我们设定缓存的过期时间是写入后的8秒,所以20秒内会失效两次,另外第一次获取时缓存中也是没有值的,所以才会未命中3次,其他则命中。
常用方法
核心类
个人工具类方法,仅供参考
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Maps;
/**
* @ClassName: CacheUtil
* @Description: TODO
*/
public class CacheUtil {
private static final Logger logger = LoggerFactory.getLogger(CacheUtil.class);
private static final RedisUtil redisUtil = RedisUtil.getInstance();
/**
* load方式缓存
*
* @param key
* @param value
*/
public static void putByLoad(String key, Object value) {
String valStr = JSON.toJSONString(value);
logger.info("[CacheUtil] putByLoad, key -> " + key + ", value -> " + valStr);
CacheUtil.getInstanceForLoad().put(key, valStr);
redisUtil.set(key, valStr);
}
/**
* load方式读取单个bean
*
* @param key
* @param t
* @return
* @throws Exception
*/
public static T getObjectByLoad(String key, Class t) {
if (key == null)
return null;
String object = null;
try {
object = CacheUtil.getInstanceForLoad().get(key);
logger.info("[CacheUtil] get from cache, key -> " + key + ", value -> " + object);
} catch (Exception e) {
logger.error("[CacheUtil] getByLoad error -> " + e);
}
if (object == null)
return null;
return (T) JSON.parseObject(object, t);
}
/**
* load方式读取list
*
* @param key
* @param t
* @return
*/
public static List getListByLoad(String key, Class t) {
if (key == null)
return null;
String object = null;
try {
object = CacheUtil.getInstanceForLoad().get(key);
} catch (Exception e) {
logger.error("[CacheUtil] getByLoad error -> " + e);
}
if (object == null)
return null;
return JSON.parseArray(object, t);
}
/**
* load方式读取map
*
* @param key
* @param t
* @return
*/
public static Map getMapByLoad(String key, Class t) {
if (key == null)
return null;
String object = null;
try {
object = CacheUtil.getInstanceForLoad().get(key);
logger.info("[CacheUtil] get from cache, key -> " + key + ", value -> " + object);
} catch (Exception e) {
logger.error("[CacheUtil] getByLoad error -> " + e);
}
if (object == null)
return null;
Map map = JSONObject.parseObject(object, new TypeReference
参考
https://www.cnblogs.com/shoren/p/guava_cache.html