guava做本地缓存
来源:互联网 发布:mac软件下载网站排名 编辑:程序博客网 时间:2024/05/26 08:41
package tool;import com.google.common.cache.CacheBuilder;import com.google.common.cache.CacheLoader;import com.google.common.cache.LoadingCache;import java.util.Optional;import java.util.Random;import java.util.concurrent.TimeUnit;public class GuavaTest { private static Integer generateNewMsgCode(String phone) { Integer msgCode = (new Random()).nextInt(1000000 - 100000) + 100000; System.out.println("generate new Msg code. phone=" + phone + ",msgCode=" + msgCode); return msgCode; } private static LoadingCache<String, Optional<Integer>> msgCodeCache = CacheBuilder.newBuilder() .maximumSize(1000L) .expireAfterWrite(5, TimeUnit.SECONDS)//5s过期 .concurrencyLevel(10) .build(new CacheLoader<String, Optional<Integer>>() { @Override public Optional<Integer> load(String phone) throws Exception { return Optional.ofNullable(generateNewMsgCode(phone)); } }); private static Integer getMsgCodeByPhone(String phone){ return msgCodeCache.getUnchecked(phone).orElse(0); } public static void main(String[] args) throws InterruptedException { String phone = "13100000000"; //first System.out.println("first .msgCode:" + getMsgCodeByPhone(phone)); Thread.sleep(1000); //second System.out.println("second .msgCode:" + getMsgCodeByPhone(phone)); Thread.sleep(5000); //third System.out.println("third .msgCode:" + getMsgCodeByPhone(phone)); }}
"com.google.guava:guava:$guavaVersion"
阅读全文
0 0
- guava做本地缓存
- guava实现本地缓存
- Guava cache实现本地缓存
- Guava Cache实现本地缓存
- Google Guava--localcache本地缓存实例
- guava学习笔记-本地缓存工具
- Guava Cache 本地缓存组件浅析
- Google Guava Cache--localcache本地缓存实例
- 本地缓存(Guava Loading Cache)
- 本地缓存实现之Guava Cache
- Guava本地缓存托底缓存以及异步更新缓存
- 使用google guava做内存缓存
- 使用google guava做内存缓存
- Guava---缓存
- guava缓存
- guava缓存
- Guava缓存
- 基于Guava LoadingCache实现的本地缓存工具
- 简单的三个步骤汉化Intelj IDEA
- HDOJ 2710 Max Factor(打表)
- spring boot学习三:基于jsp的spring mvc示例
- 关于responsbody
- [数学杂题]BZOJ 2111—— [ZJOI2010]Perm 排列计数
- guava做本地缓存
- annotation-@RequestBody-接收前端传来的json
- 关于onclick = “函数()”中不能调用window.onload = function(){}中调用function 函数(){}
- pandas---Series基础使用
- 注释转换
- 统计学习方法(第三章)k近邻算法
- 初级web api的设计(二)——防重放攻击
- 神经网络 tensorflow教程 2.2 下载MNIST 数据集 (自动版)
- 统计学习方法总结、应用对比