Guava缓存使用
public class GuavaCache { /**
* LoadingCache当缓冲中不存在时,可自动加载
* */
private static LoadingCache<Integer,Student> studentCache = CacheBuilder.newBuilder()
/**
* 设置写入缓存后过期时间(8秒过期)
* */
.expireAfterWrite(8, TimeUnit.SECONDS)
/**
* 使用SoftReference封装value,当内存不足时,自动回收
* */
.softValues()
/**
* 设置缓存初始化容量
* */
.initialCapacity(100)
/**
* 设置缓存对象权重
* */
.weigher(new Weigher<Integer, Student>() {
@Override
public int weigh(Integer key, Student value) {
return key % 2 == 0 ? 1 : 0;
}
})
/**
* 统计缓存命中率
* */
.recordStats()
/**
* 定义缓存对象失效的时间精度位纳秒级
* */
.ticker(Ticker.systemTicker())
/**
* 设置缓存移除通知
* */
.removalListener(new RemovalListener<Object, Object>() {
@Override
public void onRemoval(RemovalNotification<Object, Object> notification) {
System.out.println((Integer) notification.getKey() + " was removed , " +
"cause is " + notification.getCause());
}
})
/**
* build方法指定CacheLoader,实现数据自动加载
* */
.build(new CacheLoader<Integer, Student>() {
@Override
public Student load(Integer key) throws Exception {
Student st = new Student();
st.setStudentNo(key);
return st;
}
}); /**
* Cache.get(key, Callable)
* 当缓存中不存在key对应缓存对象时,调用Callable获取
* */
public static final Student getStudent(Integer key) throws ExecutionException {
try {
return studentCache.get(key, new Callable<Student>() {
@Override
public Student call() throws Exception {
Student st = new Student();
st.setStudentNo(key);
return st;
}
});
} finally {
System.out.println("Cache hit stats : " + studentCache.stats().toString());
}
} public static final void testStudentCache() throws ExecutionException, InterruptedException {
for (int i = 0; i < 20; ++i) {
Student student = studentCache.get(i);
System.out.println(student);
TimeUnit.SECONDS.sleep(1);
}
System.out.println("Cache hit stats : " + studentCache.stats().toString());
} public static final void testConcurrentStudentCache(int threadNumber) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(100);
Random random = new Random();
ExecutorService es = Executors.newFixedThreadPool(threadNumber);
for (int i = 0; i < 200; ++i) {
es.execute(new Runnable() {
@Override
public void run() {
try {
studentCache.get(random.nextInt(20));
TimeUnit.SECONDS.sleep(1);
} catch (Exception e) {
e.printStackTrace();
} finally {
latch.countDown();
}
}
});
}
latch.await();
es.shutdown();
System.out.println("Cache hit stats : " + studentCache.stats().toString());
} public static void main(String[] args) throws ExecutionException, InterruptedException {
// GuavaCache.testStudentCache(); GuavaCache.getStudent(1);
GuavaCache.getStudent(1);
}
}
Guava缓存使用的更多相关文章
- Guava缓存器源码分析——删除消息
Guava缓存器的删除消息机制 测试代码—— LoadingCache<String, Integer> cache = CacheBuilder.newBuild ...
- Guava缓存器源码分析——缓存统计器
Guava缓存器统计器实现: 全局统计器—— 1.CacheBuilder的静态成员变量Supplier<StatsCounter> CACHE_STATS_COUNTER ...
- guava缓存底层实现
摘要 guava的缓存相信很多人都有用到, Cache<String, String> cache = CacheBuilder.newBuilder() .expireAfterWrit ...
- Google Guava缓存实现接口的限流
一.项目背景 最近项目中需要进行接口保护,防止高并发的情况把系统搞崩,因此需要对一个查询接口进行限流,主要的目的就是限制单位时间内请求此查询的次数,例如1000次,来保护接口. 参考了 开涛的博客聊聊 ...
- springboot集成Guava缓存
很久没有写博客了,这段时间一直忙于看论文,写论文,简直头大,感觉还是做项目比较舒服,呵呵,闲话不多说,今天学习了下Guava缓存,这跟Redis类似的,但是适用的场景不一样,学习下吧.今天我们主要是s ...
- spring中添加google的guava缓存(demo)
1.pom文件中配置 <dependencies> <dependency> <groupId>org.springframework</groupId> ...
- guava缓存设置return null一直报错空指针
guava缓存设置return null一直报错空指针 因为缓存不允许返回为空
- spring boot使用guava缓存
1.pom中插入依赖: <!--guava缓存cache--> <dependency> <groupId>com.google.guava</groupId ...
- guava缓存批量获取的一个坑
摘要 Guava Cache是Google开源的Java工具集库Guava里的一款缓存工具,一直觉得使用起来比较简单,没想到这次居然还踩了一个坑 背景 功能需求抽象出来很简单,就是将数据库的查询sth ...
- guava缓存第一篇
guava缓存主要有2个接口,Cache和LoadingCache. Cache,全类名是com.google.common.cache.Cache,支持泛型.Cache<K, V>. L ...
随机推荐
- django-BBS(1)
1.首先分析BBS的设计需要,然后设计相应的数据库.填写在models.py 中 2.修改setting.py中的内容: a.将appname加入INSTALLED_APP中 b.修改DATABASE ...
- 决策树:ID3与C4.5算法
1.基本概念 1)定义: 决策树是一个预测模型:他代表的是对象属性与对象值之间的一种映射关系,树中每个节点代表的某个可能的属性值. 2)表示方法: 通过把实例从根结点排列到某个叶子结点来分类实例,叶子 ...
- AndroidManifest.xml文件详解(uses-feature)
http://blog.csdn.net/think_soft/article/details/7596796 语法(SYNTAX): <uses-featureandroid:name=&qu ...
- 最短路&生成树&二分图匹配&费用流问题
最短路 题意理解,建图 https://vjudge.net/problem/UVALive-4128 飞机票+行程建图 https://vjudge.net/problem/UVALive-3561 ...
- SSL压力测试工具THC-SSL-DOS
SSL压力测试工具THC-SSL-DOS SSL广泛应用安全加密和认证领域,如HTTPS.POP等服务.使用SSL,会加重服务器的负担.例如,在协商阶段,服务器的CPU开销是客户端的15倍.Kal ...
- mysql中timestamp类型的应用
在开发过程中我们一般需要记住某条记录的创建时间,在MySQL中如果使用dateTime类型的话,无法设定默认值,我们可以采用timestamp类型来记录创建时间.但是随之而来的有个问题,比如说你的这个 ...
- 【BZOJ 3881】【COCI 2015】Divljak
http://www.lydsy.com/JudgeOnline/problem.php?id=3881 好难的一道题啊qwq 一开始我想对T建AC自动机,根本不可做. 正解是对S建AC自动机. fa ...
- JZYZOJ1535 [haoi2014]穿越封锁线
http://172.20.6.3/Problem_Show.asp?id=1535 整体来说是道水题,但是穿过点的判定把我坑得wa了两次,考场上这可是40分的水分啊啊啊. 开始的错误想法:排序后向上 ...
- 【最大流】BZOJ1305-[CQOI2009]dance跳舞
[题目大意] 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会“单向喜欢 ...
- 使用redis时出现java.util.ArrayList cannot be cast to java.lang.Long
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.Long at redis.clients. ...