Guava LoadingCache不能缓存null值
测试的时候发现项目中的LoadingCache没有刷新,但是明明调用了refresh方法了。后来发现LoadingCache是不支持缓存null值的,如果load回调方法返回null,则在get的时候会抛出异常。
通过几个例子开看这个问题:
public void test_loadNull() {
LoadingCache<String, String> stringCache = CacheBuilder.newBuilder()
.maximumSize(10)
.build(new CacheLoader<String, String>() {
@Override
public String load(String s) throws Exception {
System.out.println("xx");
if (s.equals("hello"))
return "world";
else
return null;
}
});
try {
System.out.println(stringCache.get("hello"));
// get触发load,load返回null则抛出异常:
// com.google.common.cache.CacheLoader$InvalidCacheLoadException: CacheLoader returned null for key other_key.
System.out.println(stringCache.get("other_key"));
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public void test_loadNullWhenRefresh() {
LoadingCache<String, String> stringCache = CacheBuilder.newBuilder()
.maximumSize(10)
.build(new CacheLoader<String, String>() {
int i = 0;
@Override
public String load(String s) throws Exception {
if (i == 0) {
i++;
return "world";
}
return null;
}
});
try {
System.out.println(stringCache.get("hello"));
System.out.println(stringCache.get("hello"));
// refresh的时候,如果load函数返回null,则refresh抛出异常:
// Exception thrown during refresh
// com.google.common.cache.CacheLoader$InvalidCacheLoadException: CacheLoader returned null for key hello.
stringCache.refresh("hello");
System.out.println(stringCache.get("hello"));
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public void test_loadNullAfterInvalidate() {
LoadingCache<String, String> stringCache = CacheBuilder.newBuilder()
.maximumSize(10)
.build(new CacheLoader<String, String>() {
int i = 0;
@Override
public String load(String s) throws Exception {
if (i == 0) {
i++;
return "world";
}
return null;
}
});
try {
System.out.println(stringCache.get("hello"));
System.out.println(stringCache.get("hello"));
// invalidate不会触发load
stringCache.invalidate("hello");
// invalidate后,再次get,触发load,抛出异常:
// com.google.common.cache.CacheLoader$InvalidCacheLoadException: CacheLoader returned null for key hello.
System.out.println(stringCache.get("hello"));
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public void test_loadThrowException() {
LoadingCache<String, String> stringCache = CacheBuilder.newBuilder()
.maximumSize(10)
.build(new CacheLoader<String, String>() {
@Override
public String load(String s) throws Exception {
if (s.equals("hello"))
return "world";
else
throw new IllegalArgumentException("only_hello");
}
});
try {
System.out.println(stringCache.get("hello"));
// get触发load,load抛出异常,get也会抛出封装后的异常:
// com.google.common.util.concurrent.UncheckedExecutionException: java.lang.IllegalArgumentException: only_hello
System.out.println(stringCache.get("other_key"));
} catch (ExecutionException e) {
e.printStackTrace();
}
}
所以如果你需要缓存“空”值,推荐的做法是使用Optional对象来封装结果:
public void test_loadUseOptional() {
LoadingCache<String, Optional<String>> stringCache = CacheBuilder.newBuilder()
.maximumSize(10)
.build(new CacheLoader<String, Optional<String>>() {
@Override
public Optional<String> load(String s) throws Exception {
if (s.equals("hello"))
return Optional.of("world");
else
return Optional.absent();
}
});
try {
Optional<String> hello = stringCache.get("hello");
if(hello.isPresent()) {
System.out.println(hello.get());
}
Optional<String> otherKey = stringCache.get("other_key");
if(otherKey.isPresent()){
System.out.println(otherKey.get());
}
} catch (ExecutionException e) {
e.printStackTrace();
}
}
如果你的场景中认为null是不存在的,那么你可以在load函数中抛出异常,这个异常会通过get抛出。
另外还有一个问题,如果是key==null呢?答案是直接抛出java.lang.NullPointerException
。Guava对于null是很不待见的。
参考资料
- [Google Guava] 3-缓存 | 并发编程网 – ifeve.com
http://ifeve.com/google-guava-cachesexplained/ - Guava Cache使用笔记 - 代码说-Let code talk - ITeye博客
http://bylijinnan.iteye.com/blog/2225074 - guava - How to avoid caching when values are null? - Stack Overflow
https://stackoverflow.com/questions/13379071/how-to-avoid-caching-when-values-are-null
本文独立博客地址:Guava LoadingCache不能缓存null值 | 木杉的博客
Guava LoadingCache不能缓存null值的更多相关文章
- Spring Cacheable 注解不缓存null值
用Cacheable注解时,发现空值,也会被缓存下来.如果我们期望空值不被缓存,可以做如下设置: @Cacheable(key = "#id", unless="#res ...
- 使用google guava做内存缓存
google guava中有cache包,此包提供内存缓存功能.内存缓存需要考虑很多问题,包括并发问题,缓存失效机制,内存不够用时缓存释放,缓存的命中率,缓存的移除等等. 当然这些东西guava都考虑 ...
- 本地缓存google.guava及分布式缓存redis 随笔
近期项目用到了缓存,我选用的是主流的google.guava作本地缓存,redis作分布式 缓存,先说说我对本地缓存和分布式缓存的理解吧,可能不太成熟的地方,大家指出,一起 学习.本地缓存的特点是速度 ...
- 为什么说JAVA中要慎重使用继承 C# 语言历史版本特性(C# 1.0到C# 8.0汇总) SQL Server事务 事务日志 SQL Server 锁详解 软件架构之 23种设计模式 Oracle与Sqlserver:Order by NULL值介绍 asp.net MVC漏油配置总结
为什么说JAVA中要慎重使用继承 这篇文章的主题并非鼓励不使用继承,而是仅从使用继承带来的问题出发,讨论继承机制不太好的地方,从而在使用时慎重选择,避开可能遇到的坑. JAVA中使用到继承就会有两 ...
- 【Java必修课】四类方法删除List里面的所有null值
1 简介 万恶的null已经折磨程序员许久了,也带来了许多难以发现却造成严重损失的NullPointerException.我们需要尽可能的避免它,有一种简单的办法就是在它进入下轮处理前,我们就把它扼 ...
- oracle 关于null值排序
在oracle中根据字段来desc排序的话null值可能会在数据的最前面.然而有时候我们查看数据的时候并不希望能够在前面看到这些null值的排序数据. 因此我查了一下: 1.排序的时候运用nvl(). ...
- SQL中NULL值
SQL的表达式,除了IS NULL和NOT NULL以外,只要出现NULL值结果都为FALSE 简单的例子: SELECT * FROM table WHERE name!='abc' 只要name值 ...
- 关于null值的排序
关于空值null的排序问题 Oracle排序中NULL值处理的五种常用方法: 1.缺省Oracle在Order by 时缺省认为null是最大值,所以如果是ASC升序则排在最后,DESC降序则排在 ...
- SQL Server表分区的NULL值问题
SQL Server表分区的NULL值问题 SQL Server表分区只支持range分区这一种类型,但是本人觉得已经够用了 虽然MySQL支持四种分区类型:RANGE分区.LIST分区.HASH分区 ...
随机推荐
- canvas的描述
// 1.找到DOM节点 const canvas = document.getElementById('canvas'); // 2.画笔 --- canvas的上下文对象 const ctx = ...
- ANSYS中 *VWRITE命令使用
目录 1. *VWRITE命令 2. Fortran字段描述符 1. *VWRITE命令 ANSYS输出结果到文件,采用*VWRITE命令,具体命令如下: *VWRITE,Par1,Par2,.... ...
- 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)
题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...
- 【C语言】无参函数调用实例
#include<stdio.h> void hello() { printf("年轻人,加油!"); } int main() { hello(); ; }
- 【MySQL】完整性约束
" 目录 not null default unique 单列唯一 联合唯一 primary key 单列主键 复合主键 auto_increment 步长与偏移量 foreign key ...
- C++标准库里面没有字符分割函数split,自己编写函数实现字符串分割功能
#include <vector> #include <string> #include <iostream> using namespace std; vecto ...
- windows jdk8
C:\Program Files (x86)\Java\jdk1.8.0_65 //JAVA_HOME .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.j ...
- Docker容器CPU限制选项测试
目录 Docker容器CPU限制选项测试 参考 实验环境 --cpu-shares选项 测试 结论 --cpus选项 测试 结论 --cpuset-cpus选项 测试 结论 Docker容器CPU限制 ...
- SSIS 从oracle 转数据回MS SQL
SSIS 从oracle 转数据回MS SQL,转每月的销售记录,大概15000以内,在开发机器上没问题,部署到生产环境出现各种状况“ SSIS 无法从连接伺服器的OLE DB提供者BULK 提取资料 ...
- 【代码总结】PHP面向对象之抽象类
一.什么是抽象方法? 一个方法如果没有方法体(不使用"{}",直接使用分号结束的方法,才是没有方法体的方法),则这个方法就是抽象方法 1.声明一个方法,不使用{},而直接分号结束 ...