gauva cache
CacheLoader来根据key加载数据。而且可以定时刷新缓存(有访问才异步刷新缓存,可以让缓存自动过期。)
package testjava; import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListenableFutureTask; import java.util.concurrent.*; /**
* Create with test01
* Auther: hp.wang on 2017/9/22
* DateTime: 2017/9/22 13:20
*/
public class testCache {
static ExecutorService executor = Executors.newFixedThreadPool(10);
static Integer i1=0;
public static void main(String[] args) throws InterruptedException, ExecutionException {
// Some keys don't need refreshing, and we want refreshes to be done asynchronously.
LoadingCache<String, String> cache = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterAccess(7, TimeUnit.SECONDS)
.refreshAfterWrite(2, TimeUnit.SECONDS)
.build(
new CacheLoader<String, String>() {
public String load(String key) { // no checked exception
return "load";
} public ListenableFuture<String> reload(final String key, String prevGraph) { // asynchronous!
ListenableFutureTask<String> task = ListenableFutureTask.create(new Callable<String>() {
final int i = 0; public String call() throws Exception {
Thread.sleep(152);
i1 = i1 + 1;
System.out.println("reloading." + i1);
return "reload value:" + i1;
}
});
executor.execute(task);
return task; }
}); for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
System.out.println(cache.get("aa"));
}
System.out.println("=== wait for expired. ================");
Thread.sleep(10000); for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
System.out.println(cache.get("aa"));
}
System.out.println("===================");
}
}
output:
===============================
load
load
load
reloading.1
reload value:1
reload value:1
reload value:1
reloading.2
reload value:2
reload value:2
reload value:2
reloading.3
reload value:3
=== wait for expired. ================
load
load
load
load
reloading.4
reload value:4
reload value:4
reload value:4
reloading.5
reload value:5
reload value:5
reload value:5
===================
reloading.6
gauva cache的更多相关文章
- DBCacheServer升级
前段时间完成了该服务的设计的功能,花了很多时间和经历,最终完成了一个版本,已经测试了:现在后期再次在以前的基础上,完成了一些扩展. 1.扩展了内存存储 最初版本只是采用了gauva cache进行存储 ...
- DBCacheServer服务升级
前段时间完成了该服务的设计的功能,花了很多时间和经历,最终完成了一个版本,已经测试了:现在后期再次在以前的基础上,完成了一些扩展. 1.扩展了内存存储 最初版本只是采用了gauva cache进行存储 ...
- 【Distributed】缓存技术
一.缓存概述 1.1 缓存技术分类 1.2 缓存框架分类 1.3 Session理解的误区 二.基于Map集合实现本地缓存 2.1 定义Map缓存工具类 2.2 使用案例 三.Ehcache 缓存框架 ...
- 手把手教学在Springboot中搭建使用Guava cache,包教包会,不会我输一包辣条给你
guava cache使用简介 概述 缓存是日常开发中经常应用到的一种技术手段,合理的利用缓存可以极大的改善应用程序的性能. Guava官方对Cache的描述连接 缓存在各种各样的用例中非常有用.例 ...
- 使用Guava cache构建本地缓存
前言 最近在一个项目中需要用到本地缓存,在网上调研后,发现谷歌的Guva提供的cache模块非常的不错.简单易上手的api:灵活强大的功能,再加上谷歌这块金字招牌,让我毫不犹豫的选择了它.仅以此博客记 ...
- ASP.NET Core 折腾笔记二:自己写个完整的Cache缓存类来支持.NET Core
背景: 1:.NET Core 已经没System.Web,也木有了HttpRuntime.Cache,因此,该空间下Cache也木有了. 2:.NET Core 有新的Memory Cache提供, ...
- [Java 缓存] Java Cache之 DCache的简单应用.
前言 上次总结了下本地缓存Guava Cache的简单应用, 这次来继续说下项目中使用的DCache的简单使用. 这里分为几部分进行总结, 1)DCache介绍; 2)DCache配置及使用; 3)使 ...
- Spring cache简单使用guava cache
Spring cache简单使用 前言 spring有一套和各种缓存的集成方式.类似于sl4j,你可以选择log框架实现,也一样可以实现缓存实现,比如ehcache,guava cache. [TOC ...
- 笔记:Memory Notification: Library Cache Object loaded into SGA
笔记:Memory Notification: Library Cache Object loaded into SGA在警告日志中发现一些这样的警告信息:Mon Nov 21 14:24:22 20 ...
- ABP源码分析十三:缓存Cache实现
ABP中有两种cache的实现方式:MemroyCache 和 RedisCache. 如下图,两者都继承至ICache接口(准确说是CacheBase抽象类).ABP核心模块封装了MemroyCac ...
随机推荐
- 最全面 think php 实现微信公众号回复编号进行投票,自定义菜单功能
前期准备工作 https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Receiving_standard_messag ...
- C#导出Excel设置单元格样式
C# 导出Excel 1 //导出Excel 2 private void ExportExcel(string fileName, System.Data.DataTable myDGV, stri ...
- python容易被忽略的问题
1.int()强制转换浮点数 在int()的强制转换浮点数时候,不管是正数还是负数,只取整数部分. print(int(6.235)) # 6 print(int(-6.235)) # -6 注意:这 ...
- Voletile-多线程小例子
public class Test{ public static volatile int t = 0; //如果没有下面的全局锁标识,则结果不一定为10*1000 public static Str ...
- linux 下安装django时出的错误
解决方法 找到widgets.py 之后vim widgets.py 找到出错语句: 去掉末尾那个逗号即可.
- Oracle 详细-创建用户并导入sql文件
0.基本信息查询SQL select * from dba_users; 查看数据库里面所有用户,前提是你是有dba权限的帐号,如sys,system select * from all_u ...
- LESS-8
根据题目,这是一道布尔型注入.页面只有返回正常和不正常两种. payload: ' and substr(database(),1,1)='s' --+ 判断当前数据库名的第一个字母,是's'页面就 ...
- jar包下不下来
1.maven中的settings.xml文件中的镜像资源配置 <mirror> <id>alimaven</id> <name>aliyun mave ...
- 支持批量图片转文字的【OCR】文字识别工具,支持截图识别,速度快,准确度高,免费OCR,开源软件
想必大家偶尔也有需要从图片中提取文字的需求,虽然现在手机上都自带了拍照识别功能,可是从电脑上的图片传到手机识别再将结果发回来实 在是有点麻烦,尤其当图片数量较多时,更是费时费力,使用网页版工具又要面临 ...
- .NET Framework 中对webapi进行jwt验证
最近在项目中对webapi进行了jwt验证,做一个记录 有关于jwt生成和验证token的操作全部记录在jwthelper.cs文件中: /// <summary> /// 授权JWT类 ...