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 ...
随机推荐
- nginx代理缓存
(1)缓存介绍 1.代理服务器端缓存作用 减少后端压力,提高网站并发延时 2.缓存常见类型 服务器端缓存:代理缓存,获取服务器端内容进行缓存 浏览器端缓存 3.nginx代理缓存:proxy_cach ...
- OSError: libgfortran.so.3: cannot open shared object file: No such file or directory
运行程序遇到下面问题 OSError: libgfortran.so.3: cannot open shared object file: No such file or directory 安装yu ...
- 推荐开源靶场Vulhub
转:https://github.com/phith0n/vulhub Vulhub - Some Docker-Compose files for vulnerabilities environme ...
- 洛谷P2520向量
题目传送门 看到数据范围其实就可以确定这是一道结论题. 首先分析,给定你的向量的两个坐标a,b有八种组合方式可以用,但实际上整理一下可以得出实际上只有五种,x/y ±2a,x/y ±2b,x+a,y+ ...
- 【SQL】oralce中使用group by和case when按照条件求和
假设我们有一个Salary 薪水表.这个表的字段分别为:id, name, salary, level 在这个表中,每个人有不同的级别(level).我们要根据不同的级别统计相同级别员工的薪水总和. ...
- zookeeper,hadoop安装部署其实与防火墙无关
网上查看了很多人关于hadoop,zookeeper的文章,大多都把关闭防火墙作为首要前提,个人觉得这大可不必. 首先你需要知道你部署的是什么东西,它需要哪些端口即可.把相关端口打开就可以了啊.然后把 ...
- pdf转tiff
概述 基于Java,将pdf转成单一的tiff文件. MAVEN依赖 <groupId>com.sun.media</groupId> <artifactId>ja ...
- Linux基础系列-Day2
基础命令(文件内容管理) 1.cat:在当前终端显示文本文件内容 格式:cat [文件路径] -n 从1开始对所有输出的行数编号 -b 和-n相似,只不过对于空白行不编号:2.head:从文件内容开头 ...
- Xamarin Forms启动自带模拟器缓慢
Xamarin Forms启动自带模拟器缓慢 Xamarin Forms启动自带模拟器缓慢,在Windows 10中,Visual Studio可以使用系统自带的Hyper模拟器.但是使用时候,会长时 ...
- Electron:将前端应用打包成桌面应用
首先戳我下载安装对应版本的node.js. 安装完成后,打开命令行输入node -v以及npm -v查看对应版本.能够正常显示说明安装成功. 写一个最简单的hello world的nodejs应用.n ...