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是很不待見的。

參考資料

本文獨立博客地址:Guava LoadingCache不能緩存null值 | 木杉的博客


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM