package guavacache;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification; public class cachetest {
public static class Student {
private int id;
public String name; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return id + "| " + name;
}
} public static void main(String[] args) throws ExecutionException, InterruptedException {
// 缓存接口这里是LoadingCache,LoadingCache在缓存项不存在时可以自动加载缓存
LoadingCache<Integer, Student> studentCache
// CacheBuilder的构造函数是私有的,只能通过其静态方法newBuilder()来获得CacheBuilder的实例
= CacheBuilder.newBuilder()
// 设置并发级别为8,并发级别是指可以同时写缓存的线程数
.concurrencyLevel(8)
// 设置写缓存后8秒钟过期
.expireAfterWrite(18, TimeUnit.SECONDS)
// 设置缓存容器的初始容量为10
.initialCapacity(2)
// 设置缓存最大容量为100,超过100之后就会按照LRU最近虽少使用算法来移除缓存项
.maximumSize(2)
// 设置要统计缓存的命中率
.recordStats()
// 设置缓存的移除通知
.removalListener(new RemovalListener<Object, Object>() {
public void onRemoval(RemovalNotification<Object, Object> notification) {
System.out.println(
notification.getKey() + " was removed, cause is " + notification.getCause());
}
}) // build方法中可以指定CacheLoader,在缓存不存在时通过CacheLoader的实现自动加载缓存
.build(new CacheLoader<Integer, Student>() {
@Override
public Student load(Integer key) throws Exception {
System.out.println("load student " + key);
Student student = new Student();
student.setId(key);
student.setName("name " + key);
return student;
}
}); // for (int i = 0; i < 20; i++) {
// // 从缓存中得到数据,由于我们没有设置过缓存,所以需要通过CacheLoader加载缓存数据
// Student student = studentCache.get(i);
// System.out.println(student);
// // 休眠1秒
// TimeUnit.SECONDS.sleep(1);
// } System.out.println("cache stats:");
// 最后打印缓存的命中率等 情况
System.out.println(studentCache.stats().toString());
} }

  测试最大容量LRU算法, 感觉更像是把使用时间最近的保留

		Student student = studentCache.get(1);
System.out.println(student);
student = studentCache.get(1);
System.out.println(student);
student = studentCache.get(1);
System.out.println(student); student = studentCache.get(2);
System.out.println(student); student = studentCache.get(3);
System.out.println(student);

  结果为 1 was removed, cause is SIZE

maven

    <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>

  

Google Guava -缓存cache简单使用的更多相关文章

  1. Google Guava缓存实现接口的限流

    一.项目背景 最近项目中需要进行接口保护,防止高并发的情况把系统搞崩,因此需要对一个查询接口进行限流,主要的目的就是限制单位时间内请求此查询的次数,例如1000次,来保护接口. 参考了 开涛的博客聊聊 ...

  2. Google Guava之--cache

    一.简介 Google Guava包含了Google的Java项目许多依赖的库,如:集合 [collections] .缓存 [caching] .原生类型支持 [primitives support ...

  3. Google Guava Cache 全解析

    Google guava工具类的介绍和使用https://blog.csdn.net/wwwdc1012/article/details/82228458 LoadingCache缓存使用(Loadi ...

  4. 使用google guava做内存缓存

    google guava中有cache包,此包提供内存缓存功能.内存缓存需要考虑很多问题,包括并发问题,缓存失效机制,内存不够用时缓存释放,缓存的命中率,缓存的移除等等. 当然这些东西guava都考虑 ...

  5. google guava cache缓存基本使用讲解

    代码地址:https://github.com/vikde/demo-guava-cache 一.简介 guava cache是google guava中的一个内存缓存模块,用于将数据缓存到JVM内存 ...

  6. SpringBoot学习笔记(6) SpringBoot数据缓存Cache [Guava和Redis实现]

    https://blog.csdn.net/a67474506/article/details/52608855 Spring定义了org.springframework.cache.CacheMan ...

  7. 本地缓存google.guava及分布式缓存redis 随笔

    近期项目用到了缓存,我选用的是主流的google.guava作本地缓存,redis作分布式 缓存,先说说我对本地缓存和分布式缓存的理解吧,可能不太成熟的地方,大家指出,一起 学习.本地缓存的特点是速度 ...

  8. Spring cache简单使用guava cache

    Spring cache简单使用 前言 spring有一套和各种缓存的集成方式.类似于sl4j,你可以选择log框架实现,也一样可以实现缓存实现,比如ehcache,guava cache. [TOC ...

  9. (翻译)Google Guava Cache

    翻译自Google Guava Cache This Post is a continuation of my series on Google Guava, this time covering G ...

随机推荐

  1. hibernate5的一些坑

    SessionFactory创建的修改 如果你是刚刚从hibernate4升级到hibernate5,这时候你的项目肯定就要出错了,什么错呢? org.hibernate.MappingExcepti ...

  2. mybatis整合spring的时候配置数据库信息文件properties注意事项

    信息后面不能有空格 ,格式要xxx.driver xxx.url 这样

  3. 洛谷P1072 Hankson 的趣味题(数学)

    题意 题目链接 Sol 充满套路的数学题.. 如果你学过莫比乌斯反演的话不难得到两个等式 \[gcd(\frac{x}{a_1}, \frac{a_0}{a_1}) = 1\] \[gcd(\frac ...

  4. ElementUI组件库常见方法及问题汇总(持续更新)

    本文主要介绍在使用ElementUI组件库的时候,常遇见的问题及使用到的方法,汇总记录便于查找. 1.表单 阻止表单的默认提交 <!-- @submit.native.prevent --> ...

  5. 两种实现光标点插入range

    一.insertNode <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  6. spring org.springframework.web.bind.annotation 常用注解

    开发中常用的注解记录,查缺补漏 Request注解 @RequestBody @RequestHeader @RequestMapping @RequestParam @RequestPart @Co ...

  7. 大规模WEB服务技术

    CPU负载的扩展很简单,增加相同结构的服务器,通过负载均衡来分散. I/O负载的扩展很困难.要考虑局部性.

  8. (C# 正则表达式)判断匹配, 提取字符串或数值

    string s = "if ( \"ch\" == \"os\" ) "; string pattern = @"if\s*\( ...

  9. 颜色矩原理及Python实现

    原理 颜色矩(color moments)是由Stricker 和Orengo所提出的一种非常简单而有效的颜色特征.这种方法的数学基础在于图像中任何的颜色分布均可以用它的矩来表示.此外,由于颜色分布信 ...

  10. create-react-native-app

    create-react-native-app官网介绍链接,github文档,可以看看了解一下,总之是一个5分钟快速搭建react native项目并能看到效果的方法. 假设你已经安装了Node,你可 ...