美文网首页
180613-GuavaCache返回Null的注意事项

180613-GuavaCache返回Null的注意事项

作者: 一灰灰blog | 来源:发表于2018-06-13 18:16 被阅读59次
logo

GuavaCache返回Null的注意事项

Guava在实际的Java后端项目中应用的场景还是比较多的,比如限流,缓存,容器操作之类的,有挺多实用的工具类,这里记录一下,在使用GuavaCache,返回null的一个问题

I. 常见使用姿势

@Test
public void testGuava() {
    LoadingCache<String, String> cache = CacheBuilder.newBuilder().build(new CacheLoader<String, String>() {
        @Override
        public String load(String key) throws Exception {
            if ("hello".equals(key)) {
                return "word";
            }
            return null;
        }
    });

    String word = cache.getUnchecked("hello");
    System.out.println(word);

    System.out.println(cache.getUnchecked("word"));
}

上面是一个非常简单的测试case,需要注意的是,cache.get("word") 的执行,并不如逾期的返回的是null,而是会抛一个异常出来

word
com.google.common.cache.CacheLoader$InvalidCacheLoadException: CacheLoader returned null for key word.
    at com.google.common.cache.LocalCache$Segment.getAndRecordStats(LocalCache.java:2287)
...

从异常描述能看出,不允许返回null,这一块之前倒是没怎么注意,因此对于null的情况,要么定义一个标记表示不存在,要么在load()方法中主动抛一个异常出来,在使用的时候注意下,通过异常的使用方式,可以如下

public class NoVlaInGauvaException extends Exception {
    public NoVlaInGauvaException(String msg) {
        super(msg);
    }

    @Override
    public synchronized Throwable fillInStackTrace() {
        return this;
    }
}

说明:为什么重写fillInStackTrace方法

  • 对于这种缓存未命中的情况下,一般而言是不需要关注完整的堆栈信息的,没有数据而已,可以节省一点点性能(当然除非是在高频率的抛出时,才会有表现症状)

其次就是getgetUnchecked的区别了

  • get要求显示处理exception状况
  • getUnchecked 一般是可确认不会有问题的场景,直接调用

II. 其他

1. 一灰灰Bloghttps://liuyueyi.github.io/hexblog

一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

2. 声明

尽信书则不如,已上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激

3. 扫描关注

blogInfoV2.png

相关文章

网友评论

      本文标题:180613-GuavaCache返回Null的注意事项

      本文链接:https://www.haomeiwen.com/subject/grujeftx.html