{"id":682,"date":"2023-02-26T14:39:54","date_gmt":"2023-02-26T06:39:54","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=682"},"modified":"2023-04-29T17:58:31","modified_gmt":"2023-04-29T09:58:31","slug":"guava-common-tool-class","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/guava-common-tool-class\/","title":{"rendered":"Guava \u5e38\u7528\u5de5\u5177\u7c7b"},"content":{"rendered":"<h2>Preconditions \u524d\u7f6e\u6821\u9a8c<\/h2>\n<p>\u524d\u7f6e\u6761\u4ef6\u9002\u7528\u4e8e\u5f53\u5224\u65ad\u4e0e\u8bbe\u7f6e\u7684\u6761\u4ef6\u4e0d\u7b26\u5408\u65f6\uff0c\u629b\u51fa\u5f02\u5e38\u7684\u64cd\u4f5c\uff08\u6ce8\u610f\uff1a\u662f\u629b\u51fa\u5f02\u5e38\uff0c\u5bf9\u4e8e\u90a3\u4e9b\u60f3\u5728\u5224\u7a7a\u65f6\u505a\u76f8\u5e94\u7684\u5904\u7406\u53ef\u4ee5\u7528jdk8\u4e2d\u7684Optional\uff09<\/p>\n<p>1\uff09\u5bf9\u8c61\u5224\u7a7a\uff0c\u629b\u51fa\u5f02\u5e38<br \/>\n2\uff09List\u5bf9\u8c61\u5224\u7a7a\uff0c\u629b\u51fa\u5f02\u5e38<br \/>\n3\uff09\u6570\u5b57\u7c7b\u578b\u6761\u4ef6\u5224\u65ad\uff0c\u629b\u51fa\u5f02\u5e38<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">public class PreconditionsExample {\n\n    public static void main(String[] args) {\n\n        \/**\n         * \u5bf9\u8c61\u5224\u7a7a\u5904\u7406\n         *\/\n        UserInfo userInfo = null;\n        Preconditions.checkNotNull(userInfo, &quot;userInfo\u4e0d\u80fd\u4e3anull&quot;);\n\n        \/**\n         * List\u5bf9\u8c61\u5224\u7a7a\u5904\u7406\n         *\/\n        List&lt;String&gt; list = Lists.newArrayList();\n        Preconditions.checkNotNull(list, &quot;\u4f20\u5165\u7684list\u4e0d\u80fd\u4e3anull&quot;);\n\n        \/**\n         * \u6570\u503c\u7c7b\u578b\u5224\u65ad\u5904\u7406\n         *\/\n        Long projectId = -12L;\n        Preconditions.checkNotNull(projectId, &quot;projectId\u4e0d\u80fd\u4e3anull&quot;);\n        Preconditions.checkArgument(projectId &gt; 0, &quot;\u8f93\u5165projectId\u5fc5\u987b\u5927\u4e8e0&quot;, projectId);\n    }\n\n    class UserInfo {\n        private String name;\n    }\n}<\/code><\/pre>\n<p>\u5229\u7528Otpinal\u6765\u5bf9\u8fd4\u56de\u7684\u5355\u4e2a\u5bf9\u8c61\u8fdb\u884c\u5305\u88c5\uff08\u6ce8\u610f\uff1a\u6240\u6709\u7684\u5bf9\u8c61\u90fd\u8981\u5c01\u88c5\uff09<\/p>\n<p>\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5224\u65adUserInfo\u662f\u5426\u4e3anull\uff0c\u4ee5\u53ca\u5bf9\u4e8eLong\u7c7b\u578b\u5982\u679c\u4e3anull\uff0c\u6bd4\u5982\uff0c\u524d\u7aef\u6ca1\u6709\u4f20\u9012\u8be5\u53c2\u6570\uff0c\u5219\u6b64\u65f6\u53ef\u4ee5\u5c06\u5176\u8bbe\u7f6e\u4e3a0\u3002<\/p>\n<pre><code class=\"language-java\">@Slf4j\npublic class OptionalExample {\n\n    public static void main(String[] args) {\n        Optional&lt;UserInfo&gt; userInfo = Optional.ofNullable(getUserInfo());\n        if (!userInfo.isPresent()){\n            log.info(&quot;userInfo is null&quot;);\n        }\n\n        Optional&lt;Long&gt; projectIdOptional = Optional.ofNullable(getProjectId());\n        Long projectId = projectIdOptional.orElse(0L);  \/\/ \u5982\u679cprojectId\u4e3anull\u65f6\uff0c\u8fd9\u503c\u4e3a0\n    }\n\n    public static UserInfo getUserInfo() {\n        return null;\n    }\n\n    public static Long getProjectId() {\n        return null;\n    }\n\n    @Getter\n    @Setter\n    class UserInfo{\n        private String userName;\n    }\n}<\/code><\/pre>\n<h2>retryer\u5b9e\u73b0\u63a5\u53e3\u91cd\u8bd5\u673a\u5236<\/h2>\n<p>\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u8981\u8c03\u7528\u5916\u90e8\u670d\u52a1\u548c\u63a5\u53e3\u7684\u573a\u666f\u3002\u5916\u90e8\u670d\u52a1\u5bf9\u4e8e\u8c03\u7528\u8005\u6765\u8bf4\u4e00\u822c\u662f\u4e0d\u9760\u8c31\u7684\uff0c\u5c24\u5176\u662f\u5728\u7f51\u7edc\u73af\u5883\u6bd4\u8f83\u5dee\u7684\u60c5\u51b5\u4e0b\uff0c\u7f51\u7edc\u6296\u52a8\u5f88\u5bb9\u6613\u5bfc\u81f4\u8bf7\u6c42\u8d85\u65f6\u7b49\u5f02\u5e38\u60c5\u51b5\uff0c\u8fd9\u65f6\u5019\u9700\u8981\u4f7f\u7528\u5931\u8d25\u91cd\u8bd5\u8c03\u7528API\u63a5\u53e3\u6765\u83b7\u53d6\u3002<\/p>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/rholder\/guava-retrying\">https:\/\/github.com\/rholder\/guava-retrying<\/a><\/p>\n<p>\uff081\uff09\u9700\u8981\u518d\u5f15\u5165guava-retrying\u5305\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;com.github.rholder&lt;\/groupId&gt;\n    &lt;artifactId&gt;guava-retrying&lt;\/artifactId&gt;\n    &lt;version&gt;2.0.0&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\uff082\uff09\u5b9e\u73b0\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">@Slf4j\npublic class RetryerExample {\n\n    public static void main(String[] args) throws Exception {\n        Retryer&lt;Boolean&gt; retryer = RetryerBuilder.&lt;Boolean&gt;newBuilder()\n                .retryIfResult(Predicates.&lt;Boolean&gt;isNull())    \/\/ \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u6bb5\u5143\u91cd\u8bd5\u6e90\n                .retryIfExceptionOfType(Exception.class)        \/\/ \u8bbe\u7f6e\u5f02\u5e38\u91cd\u8bd5\u6e90\n                .retryIfRuntimeException()                      \/\/ \u8bbe\u7f6e\u5f02\u5e38\u91cd\u8bd5\u6e90\n                .withStopStrategy(StopStrategies.stopAfterAttempt(5))   \/\/ \u8bbe\u7f6e\u91cd\u8bd5\u6b21\u6570\n                .withWaitStrategy(WaitStrategies.fixedWait(5L, TimeUnit.SECONDS))   \/\/ \u8bbe\u7f6e\u6bcf\u6b21\u91cd\u8bd5\u95f4\u9694\n                .build();\n\n        Callable&lt;Boolean&gt; task = new Callable&lt;Boolean&gt;() {\n            int i = 0;\n            @Override\n            public Boolean call() throws Exception {\n                i++;\n                log.info(&quot;\u7b2c{}\u6b21\u6267\u884c\uff01&quot;, i);\n                if (i &lt; 3) {\n                    log.info(&quot;\u6a21\u62df\u6267\u884c\u5931\u8d25&quot;);\n                    throw new IOException(&quot;\u5f02\u5e38&quot;);\n                }\n                return true;\n            }\n        };\n\n        try {\n            retryer.call(task);\n        } catch (ExecutionException e) {\n            log.error(&quot;error&quot;, e);\n        } catch (RetryException e) {\n            log.error(&quot;error&quot;, e);\n        }\n\n        Boolean result = task.call();\n        log.info(&quot;\u6210\u529f\u8f93\u51fa\u7ed3\u679c\uff1a{}&quot;, result);\n    }\n\n}<\/code><\/pre>\n<p>\u5206\u6790\uff1a\u4e0a\u8ff0\u4e2d\u65b9\u6cd5\u8c03\u7528\u5931\u8d25\u4e86\u4e09\u6b21\uff0c\u5728\u91cd\u8bd5\u7b2c4\u6b21\u4e4b\u540e\uff0c\u6210\u529f\u8fd4\u56de\u6570\u636e\u3002<\/p>\n<h2>\u672c\u5730\u5185\u5b58 Guava Cache<\/h2>\n<p>\u7f13\u5b58\u6709\u5206\u5e03\u5f0f\u7f13\u5b58\u548c\u672c\u5730\u7f13\u5b58\uff0c\u8fd9\u91cc\u4e3b\u8981\u4ecb\u7ecdGoogle\u4e2d\u7684Guava\u5de5\u5177\u5305\u4e2d\u5b9e\u73b0\u7684\u672c\u5730\u7f13\u5b58\u5de5\u5177\u7c7b\uff0c\u80fd\u591f\u6709\u6548\u7684\u63a7\u5236\u7f13\u5b58\u7684\u7b56\u7565\u3002<\/p>\n<p>Guava Cache\u4e0eConcurrentMap\u5f88\u76f8\u4f3c\uff0c\u4f46\u4e5f\u4e0d\u5b8c\u5168\u4e00\u6837\u3002\u6700\u57fa\u672c\u7684\u533a\u522b\u662fConcurrentMap\u4f1a\u4e00\u76f4\u4fdd\u5b58\u6240\u6709\u6dfb\u52a0\u7684\u5143\u7d20\uff0c\u76f4\u5230\u663e\u5f0f\u5730\u79fb\u9664\u3002\u76f8\u5bf9\u5730\uff0cGuava Cache\u4e3a\u4e86\u9650\u5236\u5185\u5b58\u5360\u7528\uff0c\u901a\u5e38\u90fd\u8bbe\u5b9a\u4e3a\u81ea\u52a8\u56de\u6536\u5143\u7d20\u3002\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0c\u5c3d\u7ba1LoadingCache \u4e0d\u56de\u6536\u5143\u7d20\uff0c\u5b83\u4e5f\u662f\u5f88\u6709\u7528\u7684\uff0c\u56e0\u4e3a\u5b83\u4f1a\u81ea\u52a8\u52a0\u8f7d\u7f13\u5b58\u3002<\/p>\n<p>Guava Cache\u662f\u5728\u5185\u5b58\u4e2d\u7f13\u5b58\u6570\u636e\uff0c\u76f8\u6bd4\u8f83\u4e8e\u6570\u636e\u5e93\u6216Redis\u5b58\u50a8\uff0c\u8bbf\u95ee\u5185\u5b58\u4e2d\u7684\u6570\u636e\u4f1a\u66f4\u52a0\u9ad8\u6548\u3002Guava\u5b98\u7f51\u4ecb\u7ecd\uff0c\u4e0b\u9762\u7684\u8fd9\u51e0\u79cd\u60c5\u51b5\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Guava Cache\uff1a<\/p>\n<ul>\n<li>\u613f\u610f\u6d88\u8017\u4e00\u4e9b\u5185\u5b58\u7a7a\u95f4\u6765\u63d0\u5347\u901f\u5ea6<\/li>\n<li>\u9884\u6599\u5230\u67d0\u4e9b\u952e\u4f1a\u88ab\u591a\u6b21\u67e5\u8be2<\/li>\n<li>\u7f13\u5b58\u4e2d\u5b58\u653e\u7684\u6570\u636e\u603b\u91cf\u4e0d\u4f1a\u8d85\u51fa\u5185\u5b58\u5bb9\u91cf<\/li>\n<\/ul>\n<p>\u6240\u4ee5\uff0c\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u9891\u7e41\u7528\u5230\u7684\u5c11\u91cf\u6570\u636e\u5b58\u50a8\u5230Guava Cache\u4e2d\uff0c\u4ee5\u6539\u5584\u7a0b\u5e8f\u6027\u80fd\u3002\u4e0b\u9762\u5bf9Guava Cache\u7684\u7528\u6cd5\u8fdb\u884c\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002<\/p>\n<h3>\u6784\u5efa\u7f13\u5b58\u5bf9\u8c61<\/h3>\n<p>\u63a5\u53e3Cache\u4ee3\u8868\u4e00\u5757\u7f13\u5b58\uff0c\u5b83\u6709\u5982\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">public interface Cache&lt;K, V&gt; {\n    V get(K key, Callable&lt;? extends V&gt; valueLoader) throws ExecutionException;\n\n    ImmutableMap&lt;K, V&gt; getAllPresent(Iterable&lt;?&gt; keys);\n\n    void put(K key, V value);\n\n    void putAll(Map&lt;? extends K, ? extends V&gt; m);\n\n    void invalidate(Object key);\n\n    void invalidateAll(Iterable&lt;?&gt; keys);\n\n    void invalidateAll();\n\n    long size();\n\n    CacheStats stats();\n\n    ConcurrentMap&lt;K, V&gt; asMap();\n\n    void cleanUp();\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u901a\u8fc7CacheBuilder\u7c7b\u6784\u5efa\u4e00\u4e2a\u7f13\u5b58\u5bf9\u8c61\uff0cCacheBuilder\u7c7b\u91c7\u7528builder\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u7684\u6bcf\u4e2a\u65b9\u6cd5\u90fd\u8fd4\u56deCacheBuilder\u672c\u8eab\uff0c\u76f4\u5230build\u65b9\u6cd5\u88ab\u8c03\u7528\u3002\u6784\u5efa\u4e00\u4e2a\u7f13\u5b58\u5bf9\u8c61\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n<pre><code class=\"language-java\">public class StudyGuavaCache {\n    public static void main(String[] args) {\n        Cache&lt;String,String&gt; cache = CacheBuilder.newBuilder().build();\n        cache.put(&quot;word&quot;, &quot;Hello Guava Cache&quot;);\n        System.out.println(cache.getIfPresent(&quot;word&quot;));\n    }\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u901a\u8fc7<code>CacheBuilder.newBuilder().build()<\/code>\u8fd9\u53e5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2aCache\u7f13\u5b58\u5bf9\u8c61\uff0c\u5e76\u5728\u7f13\u5b58\u5bf9\u8c61\u4e2d\u5b58\u50a8\u4e86key\u4e3aword\uff0cvalue\u4e3a<code>Hello Guava Cache<\/code>\u7684\u4e00\u6761\u8bb0\u5f55\u3002\u53ef\u4ee5\u770b\u5230Cache\u975e\u5e38\u7c7b\u4f3c\u4e8eJDK\u4e2d\u7684Map\uff0c\u4f46\u662f\u76f8\u6bd4\u4e8eMap\uff0cGuava Cache\u63d0\u4f9b\u4e86\u5f88\u591a\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u3002<\/p>\n<p>\u4eceLoadingCache\u67e5\u8be2\u7684\u6b63\u89c4\u65b9\u5f0f\u662f\u4f7f\u7528<code>get(K)<\/code>\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u8981\u4e48\u8fd4\u56de\u5df2\u7ecf\u7f13\u5b58\u7684\u503c\uff0c\u8981\u4e48\u4f7f\u7528CacheLoader\u5411\u7f13\u5b58\u539f\u5b50\u5730\u52a0\u8f7d\u65b0\u503c\uff08\u901a\u8fc7<code>load(String key)<\/code> \u65b9\u6cd5\u52a0\u8f7d\uff09\u3002\u7531\u4e8eCacheLoader\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\uff0c<code>LoadingCache.get(K)<\/code>\u4e5f\u58f0\u660e\u629b\u51fa<code>ExecutionException<\/code>\u5f02\u5e38\u3002\u5982\u679c\u5b9a\u4e49\u7684CacheLoader\u6ca1\u6709\u58f0\u660e\u4efb\u4f55\u68c0\u67e5\u578b\u5f02\u5e38\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7<code>getUnchecked(K)<\/code>\u67e5\u627e\u7f13\u5b58\uff1b\u4f46\u5fc5\u987b\u6ce8\u610f\uff0c\u4e00\u65e6CacheLoader\u58f0\u660e\u4e86\u68c0\u67e5\u578b\u5f02\u5e38\uff0c\u5c31\u4e0d\u53ef\u4ee5\u8c03\u7528<code>getUnchecked(K)<\/code>\u3002<\/p>\n<pre><code class=\"language-java\">LoadingCache&lt;Key, Value&gt; cache = CacheBuilder.newBuilder()\n    .build(\n        new CacheLoader&lt;Key, Value&gt;() {\n            public Value load(Key key) throws AnyException {\n                return createValue(key);\n            }\n        });\n...\ntry {\n    return cache.get(key);\n} catch (ExecutionException e) {\n    throw new OtherException(e.getCause());\n}<\/code><\/pre>\n<h3>\u8bbe\u7f6e\u6700\u5927\u5b58\u50a8<\/h3>\n<p>Guava Cache\u53ef\u4ee5\u5728\u6784\u5efa\u7f13\u5b58\u5bf9\u8c61\u65f6\u6307\u5b9a\u7f13\u5b58\u6240\u80fd\u591f\u5b58\u50a8\u7684\u6700\u5927\u8bb0\u5f55\u6570\u91cf\u3002\u5f53Cache\u4e2d\u7684\u8bb0\u5f55\u6570\u91cf\u8fbe\u5230\u6700\u5927\u503c\u540e\u518d\u8c03\u7528put\u65b9\u6cd5\u5411\u5176\u4e2d\u6dfb\u52a0\u5bf9\u8c61\uff0cGuava\u4f1a\u5148\u4ece\u5f53\u524d\u7f13\u5b58\u7684\u5bf9\u8c61\u8bb0\u5f55\u4e2d\u9009\u62e9\u4e00\u6761\u5220\u9664\u6389\uff0c\u817e\u51fa\u7a7a\u95f4\u540e\u518d\u5c06\u65b0\u7684\u5bf9\u8c61\u5b58\u50a8\u5230Cache\u4e2d\u3002<\/p>\n<pre><code class=\"language-java\">public class StudyGuavaCache {\n    public static void main(String[] args) {\n        Cache&lt;String,String&gt; cache = CacheBuilder.newBuilder()\n                .maximumSize(2)\n                .build();\n        cache.put(&quot;key1&quot;, &quot;value1&quot;);\n        cache.put(&quot;key2&quot;, &quot;value2&quot;);\n        cache.put(&quot;key3&quot;, &quot;value3&quot;);\n        System.out.println(&quot;\u7b2c\u4e00\u4e2a\u503c\uff1a&quot; + cache.getIfPresent(&quot;key1&quot;));\n        System.out.println(&quot;\u7b2c\u4e8c\u4e2a\u503c\uff1a&quot; + cache.getIfPresent(&quot;key2&quot;));\n        System.out.println(&quot;\u7b2c\u4e09\u4e2a\u503c\uff1a&quot; + cache.getIfPresent(&quot;key3&quot;));\n    }\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u4ee3\u7801\u5728\u6784\u9020\u7f13\u5b58\u5bf9\u8c61\u65f6\uff0c\u901a\u8fc7CacheBuilder\u7c7b\u7684<code>maximumSize<\/code>\u65b9\u6cd5\u6307\u5b9aCache\u6700\u591a\u53ef\u4ee5\u5b58\u50a8\u4e24\u4e2a\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528Cache\u7684put\u65b9\u6cd5\u5411\u5176\u4e2d\u6dfb\u52a0\u4e86\u4e09\u4e2a\u5bf9\u8c61\u3002\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u53ef\u4ee5\u770b\u5230\u7b2c\u4e09\u6761\u5bf9\u8c61\u8bb0\u5f55\u7684\u63d2\u5165\uff0c\u5bfc\u81f4\u4e86\u7b2c\u4e00\u6761\u5bf9\u8c61\u8bb0\u5f55\u88ab\u5220\u9664\u3002<\/p>\n<pre><code>\u7b2c\u4e00\u4e2a\u503c\uff1anull\n\u7b2c\u4e8c\u4e2a\u503c\uff1avalue2\n\u7b2c\u4e09\u4e2a\u503c\uff1avalue3<\/code><\/pre>\n<h3>\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4<\/h3>\n<p>\u5728\u6784\u5efaCache\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7CacheBuilder\u7c7b\u7684<code>expireAfterAccess<\/code>\u548c<code>expireAfterWrite<\/code>\u4e24\u4e2a\u65b9\u6cd5\u4e3a\u7f13\u5b58\u4e2d\u7684\u5bf9\u8c61\u6307\u5b9a\u8fc7\u671f\u65f6\u95f4\uff0c\u8fc7\u671f\u7684\u5bf9\u8c61\u5c06\u4f1a\u88ab\u7f13\u5b58\u81ea\u52a8\u5220\u9664\u3002\u5176\u4e2d\uff0c<code>expireAfterWrite<\/code>\u65b9\u6cd5\u6307\u5b9a\u5bf9\u8c61\u88ab\u5199\u5165\u5230\u7f13\u5b58\u540e\u591a\u4e45\u8fc7\u671f\uff0c<code>expireAfterAccess<\/code>\u6307\u5b9a\u5bf9\u8c61\u591a\u4e45\u6ca1\u6709\u88ab\u8bbf\u95ee\u540e\u8fc7\u671f\u3002<\/p>\n<pre><code class=\"language-java\">public class StudyGuavaCache {\n    public static void main(String[] args) throws InterruptedException {\n        Cache&lt;String,String&gt; cache = CacheBuilder.newBuilder()\n                .maximumSize(2)\n                .expireAfterWrite(3, TimeUnit.SECONDS)\n                .build();\n        cache.put(&quot;key1&quot;, &quot;value1&quot;);\n        int time = 1;\n        while (true) {\n            System.out.println(&quot;\u7b2c&quot; + time++ + &quot;\u6b21\u53d6\u5230key1\u7684\u503c\u4e3a\uff1a&quot; + cache.getIfPresent(&quot;key1&quot;));\n            Thread.sleep(1000);\n        }\n    }\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5728\u6784\u9020Cache\u5bf9\u8c61\u65f6\uff0c\u901a\u8fc7CacheBuilder\u7684<code>expireAfterWrite<\/code>\u65b9\u6cd5\u6307\u5b9aput\u5230Cache\u4e2d\u7684\u5bf9\u8c61\u57283\u79d2\u540e\u4f1a\u8fc7\u671f\u3002\u5728Cache\u5bf9\u8c61\u4e2d\u5b58\u50a8\u4e00\u6761\u5bf9\u8c61\u8bb0\u5f55\u540e\uff0c\u6bcf\u96941\u79d2\u8bfb\u53d6\u4e00\u6b21\u8fd9\u6761\u8bb0\u5f55\u3002\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u53ef\u4ee5\u770b\u5230\uff0c\u524d\u4e09\u79d2\u53ef\u4ee5\u4eceCache\u4e2d\u83b7\u53d6\u5230\u5bf9\u8c61\uff0c\u8d85\u8fc7\u4e09\u79d2\u540e\uff0c\u5bf9\u8c61\u4eceCache\u4e2d\u88ab\u81ea\u52a8\u5220\u9664\u3002<\/p>\n<pre><code>\u7b2c1\u6b21\u53d6\u5230key1\u7684\u503c\u4e3a\uff1avalue1\n\u7b2c2\u6b21\u53d6\u5230key1\u7684\u503c\u4e3a\uff1avalue1\n\u7b2c3\u6b21\u53d6\u5230key1\u7684\u503c\u4e3a\uff1avalue1\n\u7b2c4\u6b21\u53d6\u5230key1\u7684\u503c\u4e3a\uff1anull\n\u7b2c5\u6b21\u53d6\u5230key1\u7684\u503c\u4e3a\uff1anull\n\u7b2c6\u6b21\u53d6\u5230key1\u7684\u503c\u4e3a\uff1anull\n\u7b2c7\u6b21\u53d6\u5230key1\u7684\u503c\u4e3a\uff1anull\n\u7b2c8\u6b21\u53d6\u5230key1\u7684\u503c\u4e3a\uff1anull\n\u7b2c9\u6b21\u53d6\u5230key1\u7684\u503c\u4e3a\uff1anull<\/code><\/pre>\n<p>\u4e0b\u9762\u4ee3\u7801\u662f<code>expireAfterAccess<\/code>\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-java\">public class StudyGuavaCache {\n    public static void main(String[] args) throws InterruptedException {\n        Cache&lt;String,String&gt; cache = CacheBuilder.newBuilder()\n                .maximumSize(2)\n                .expireAfterAccess(3, TimeUnit.SECONDS)\n                .build();\n        cache.put(&quot;key1&quot;, &quot;value1&quot;);\n        int time = 1;\n        while (true) {\n            Thread.sleep(time*1000);\n            System.out.println(&quot;\u7761\u7720&quot; + time++ + &quot;\u79d2\u540e\u53d6\u5230key1\u7684\u503c\u4e3a\uff1a&quot; + cache.getIfPresent(&quot;key1&quot;));\n        }\n    }\n}<\/code><\/pre>\n<p>\u901a\u8fc7CacheBuilder\u7684\u00b7expireAfterAccess\u00b7\u65b9\u6cd5\u6307\u5b9aCache\u4e2d\u5b58\u50a8\u7684\u5bf9\u8c61\u5982\u679c\u8d85\u8fc73\u79d2\u6ca1\u6709\u88ab\u8bbf\u95ee\u5c31\u4f1a\u8fc7\u671f\u3002while\u4e2d\u7684\u4ee3\u7801\u6bcfsleep\u4e00\u6bb5\u65f6\u95f4\u5c31\u4f1a\u8bbf\u95ee\u4e00\u6b21Cache\u4e2d\u5b58\u50a8\u7684\u5bf9\u8c61key1\uff0c\u6bcf\u6b21\u8bbf\u95eekey1\u4e4b\u540e\u4e0b\u6b21sleep\u7684\u65f6\u95f4\u4f1a\u52a0\u957f\u4e00\u79d2\u3002\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4ece\u7ed3\u679c\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u5f53\u8d85\u8fc73\u79d2\u6ca1\u6709\u8bfb\u53d6key1\u5bf9\u8c61\u4e4b\u540e\uff0c\u8be5\u5bf9\u8c61\u4f1a\u81ea\u52a8\u88abCache\u5220\u9664\u3002<\/p>\n<pre><code>\u7761\u77201\u79d2\u540e\u53d6\u5230key1\u7684\u503c\u4e3a\uff1avalue1\n\u7761\u77202\u79d2\u540e\u53d6\u5230key1\u7684\u503c\u4e3a\uff1avalue1\n\u7761\u77203\u79d2\u540e\u53d6\u5230key1\u7684\u503c\u4e3a\uff1anull\n\u7761\u77204\u79d2\u540e\u53d6\u5230key1\u7684\u503c\u4e3a\uff1anull\n\u7761\u77205\u79d2\u540e\u53d6\u5230key1\u7684\u503c\u4e3a\uff1anull\n\u7761\u77206\u79d2\u540e\u53d6\u5230key1\u7684\u503c\u4e3a\uff1anull\n\u7761\u77207\u79d2\u540e\u53d6\u5230key1\u7684\u503c\u4e3a\uff1anull<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u540c\u65f6\u7528<code>expireAfterAccess<\/code>\u548c<code>expireAfterWrite<\/code>\u65b9\u6cd5\u6307\u5b9a\u8fc7\u671f\u65f6\u95f4\uff0c\u8fd9\u65f6\u53ea\u8981\u5bf9\u8c61\u6ee1\u8db3\u4e24\u8005\u4e2d\u7684\u4e00\u4e2a\u6761\u4ef6\u5c31\u4f1a\u88ab\u81ea\u52a8\u8fc7\u671f\u5220\u9664\u3002<\/p>\n<p>Guava Cache\u7f13\u5b58\u8fc7\u671f\u540e\u4e0d\u4e00\u5b9a\u4f1a\u7acb\u9a6c\u88ab\u6e05\u7406\uff0c\u4e00\u822c\u4f1a\u5728Cache\u6574\u4f53\u88ab\u8bfb\u53d6\u4e00\u5b9a\u6b21\u6570\u540e\u6e05\u7406\u3002\u8fd9\u4e2d\u7b56\u7565\u5bf9\u6027\u80fd\u662f\u6709\u597d\u5904\u7684\uff0c\u5982\u679c\u60f3\u5f3a\u5236\u6e05\u7406\u53ef\u4ee5\u624b\u52a8\u8c03\u7528<code>Cache.cleanup()<\/code>\u6216\u8005\u4f7f\u7528<code>ScheduledExecutorService<\/code>\u6765\u5b8c\u6210\u5b9a\u671f\u6e05\u7406<\/p>\n<h3>\u5f31\u5f15\u7528<\/h3>\n<p>\u53ef\u4ee5\u901a\u8fc7<code>weakKeys<\/code>\u548c<code>weakValues<\/code>\u65b9\u6cd5\u6307\u5b9aCache\u53ea\u4fdd\u5b58\u5bf9\u7f13\u5b58\u8bb0\u5f55key\u548cvalue\u7684\u5f31\u5f15\u7528\u3002\u8fd9\u6837\u5f53\u6ca1\u6709\u5176\u4ed6\u5f3a\u5f15\u7528\u6307\u5411key\u548cvalue\u65f6\uff0ckey\u548cvalue\u5bf9\u8c61\u5c31\u4f1a\u88ab\u5783\u573e\u56de\u6536\u5668\u56de\u6536\u3002<\/p>\n<pre><code class=\"language-java\">public class StudyGuavaCache {\n    public static void main(String[] args) throws InterruptedException {\n        Cache&lt;String,Object&gt; cache = CacheBuilder.newBuilder()\n                .maximumSize(2)\n                .weakValues()\n                .build();\n        Object value = new Object();\n        cache.put(&quot;key1&quot;, value);\n\n        value = new Object(); \/\/\u539f\u5bf9\u8c61\u4e0d\u518d\u6709\u5f3a\u5f15\u7528\n        System.gc();\n        System.out.println(cache.getIfPresent(&quot;key1&quot;));\n    }\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u4ee3\u7801\u7684\u6253\u5370\u7ed3\u679c\u662fnull\u3002\u6784\u5efaCache\u65f6\u901a\u8fc7<code>weakValues<\/code>\u65b9\u6cd5\u6307\u5b9aCache\u53ea\u4fdd\u5b58\u8bb0\u5f55\u503c\u7684\u4e00\u4e2a\u5f31\u5f15\u7528\u3002\u5f53\u7ed9value\u5f15\u7528\u8d4b\u503c\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61\u4e4b\u540e\uff0c\u5c31\u4e0d\u518d\u6709\u4efb\u4f55\u4e00\u4e2a\u5f3a\u5f15\u7528\u6307\u5411\u539f\u5bf9\u8c61\u3002<code>System.gc()<\/code>\u89e6\u53d1\u5783\u573e\u56de\u6536\u540e\uff0c\u539f\u5bf9\u8c61\u5c31\u88ab\u6e05\u9664\u4e86\u3002<\/p>\n<h3>\u663e\u793a\u6e05\u9664<\/h3>\n<p>\u53ef\u4ee5\u8c03\u7528Cache\u7684<code>invalidateAll<\/code>\u6216<code>invalidate<\/code>\u65b9\u6cd5\u663e\u793a\u5220\u9664Cache\u4e2d\u7684\u8bb0\u5f55\u3002<code>invalidate<\/code>\u65b9\u6cd5\u4e00\u6b21\u53ea\u80fd\u5220\u9664Cache\u4e2d\u4e00\u4e2a\u8bb0\u5f55\uff0c\u63a5\u6536\u7684\u53c2\u6570\u662f\u8981\u5220\u9664\u8bb0\u5f55\u7684key\u3002<code>invalidateAll<\/code>\u65b9\u6cd5\u53ef\u4ee5\u6279\u91cf\u5220\u9664Cache\u4e2d\u7684\u8bb0\u5f55\uff0c\u5f53\u6ca1\u6709\u4f20\u4efb\u4f55\u53c2\u6570\u65f6\uff0c<code>invalidateAll<\/code>\u65b9\u6cd5\u5c06\u6e05\u9664Cache\u4e2d\u7684\u5168\u90e8\u8bb0\u5f55\u3002<code>invalidateAll<\/code>\u4e5f\u53ef\u4ee5\u63a5\u6536\u4e00\u4e2aIterable\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u53c2\u6570\u4e2d\u5305\u542b\u8981\u5220\u9664\u8bb0\u5f55\u7684\u6240\u6709key\u503c\u3002<\/p>\n<pre><code class=\"language-java\">public class StudyGuavaCache {\n    public static void main(String[] args) throws InterruptedException {\n        Cache&lt;String,String&gt; cache = CacheBuilder.newBuilder().build();\n        cache.put(&quot;key1&quot;, &quot;value1&quot;);\n        cache.put(&quot;key2&quot;, &quot;value2&quot;);\n        cache.put(&quot;key3&quot;, &quot;value3&quot;);\n\n        List&lt;String&gt; list = new ArrayList&lt;String&gt;();\n        list.add(&quot;key1&quot;);\n        list.add(&quot;key2&quot;);\n\n        cache.invalidateAll(list); \/\/\u6279\u91cf\u6e05\u9664list\u4e2d\u5168\u90e8key\u5bf9\u5e94\u7684\u8bb0\u5f55\n        System.out.println(cache.getIfPresent(&quot;key1&quot;));\n        System.out.println(cache.getIfPresent(&quot;key2&quot;));\n        System.out.println(cache.getIfPresent(&quot;key3&quot;));\n    }\n}<\/code><\/pre>\n<p>\u4ee3\u7801\u4e2d\u6784\u9020\u4e86\u4e00\u4e2a\u96c6\u5408list\u7528\u4e8e\u4fdd\u5b58\u8981\u5220\u9664\u8bb0\u5f55\u7684key\u503c\uff0c\u7136\u540e\u8c03\u7528<code>invalidateAll<\/code>\u65b9\u6cd5\u6279\u91cf\u5220\u9664key1\u548ckey2\u5bf9\u5e94\u7684\u8bb0\u5f55\uff0c\u53ea\u5269\u4e0bkey3\u5bf9\u5e94\u7684\u8bb0\u5f55\u6ca1\u6709\u88ab\u5220\u9664\u3002<\/p>\n<h3>\u79fb\u9664\u76d1\u542c\u5668<\/h3>\n<p>\u53ef\u4ee5\u4e3aCache\u5bf9\u8c61\u6dfb\u52a0\u4e00\u4e2a\u79fb\u9664\u76d1\u542c\u5668\uff0c\u8fd9\u6837\u5f53\u6709\u8bb0\u5f55\u88ab\u5220\u9664\u65f6\u53ef\u4ee5\u611f\u77e5\u5230\u8fd9\u4e2a\u4e8b\u4ef6\u3002<\/p>\n<pre><code class=\"language-java\">public class StudyGuavaCache {\n    public static void main(String[] args) throws InterruptedException {\n        RemovalListener&lt;String, String&gt; listener = new RemovalListener&lt;String, String&gt;() {\n            public void onRemoval(RemovalNotification&lt;String, String&gt; notification) {\n                System.out.println(&quot;[&quot; + notification.getKey() + &quot;:&quot; + notification.getValue() + &quot;] is removed!&quot;);\n            }\n        };\n        Cache&lt;String,String&gt; cache = CacheBuilder.newBuilder()\n                .maximumSize(3)\n                .removalListener(listener)\n                .build();\n        Object value = new Object();\n        cache.put(&quot;key1&quot;, &quot;value1&quot;);\n        cache.put(&quot;key2&quot;, &quot;value2&quot;);\n        cache.put(&quot;key3&quot;, &quot;value3&quot;);\n        cache.put(&quot;key4&quot;, &quot;value3&quot;);\n        cache.put(&quot;key5&quot;, &quot;value3&quot;);\n        cache.put(&quot;key6&quot;, &quot;value3&quot;);\n        cache.put(&quot;key7&quot;, &quot;value3&quot;);\n        cache.put(&quot;key8&quot;, &quot;value3&quot;);\n    }\n}<\/code><\/pre>\n<p><code>removalListener<\/code>\u65b9\u6cd5\u4e3aCache\u6307\u5b9a\u4e86\u4e00\u4e2a\u79fb\u9664\u76d1\u542c\u5668\uff0c\u8fd9\u6837\u5f53\u6709\u8bb0\u5f55\u4eceCache\u4e2d\u88ab\u5220\u9664\u65f6\uff0c\u76d1\u542c\u5668listener\u5c31\u4f1a\u611f\u77e5\u5230\u8fd9\u4e2a\u4e8b\u4ef6\u3002\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>[key1:value1] is removed!\n[key2:value2] is removed!\n[key3:value3] is removed!\n[key4:value4] is removed!\n[key5:value5] is removed!<\/code><\/pre>\n<h3>\u81ea\u52a8\u52a0\u8f7d<\/h3>\n<p>Cache\u7684get\u65b9\u6cd5\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8981\u4eceCache\u4e2d\u83b7\u53d6\u8bb0\u5f55\u7684key\uff0c\u7b2c\u4e8c\u4e2a\u8bb0\u5f55\u662f\u4e00\u4e2aCallable\u5bf9\u8c61\u3002\u5f53\u7f13\u5b58\u4e2d\u5df2\u7ecf\u5b58\u5728key\u5bf9\u5e94\u7684\u8bb0\u5f55\u65f6\uff0cget\u65b9\u6cd5\u76f4\u63a5\u8fd4\u56dekey\u5bf9\u5e94\u7684\u8bb0\u5f55\u3002\u5982\u679c\u7f13\u5b58\u4e2d\u4e0d\u5305\u542bkey\u5bf9\u5e94\u7684\u8bb0\u5f55\uff0cGuava\u4f1a\u542f\u52a8\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884cCallable\u5bf9\u8c61\u4e2d\u7684call\u65b9\u6cd5\uff0ccall\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u4f1a\u4f5c\u4e3akey\u5bf9\u5e94\u7684\u503c\u88ab\u5b58\u50a8\u5230\u7f13\u5b58\u4e2d\uff0c\u5e76\u4e14\u88abget\u65b9\u6cd5\u8fd4\u56de\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u591a\u7ebf\u7a0b\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-java\">public class StudyGuavaCache {\n\n    private static Cache&lt;String,String&gt; cache = CacheBuilder.newBuilder()\n            .maximumSize(3)\n            .build();\n\n    public static void main(String[] args) throws InterruptedException {\n\n        new Thread(new Runnable() {\n            public void run() {\n                System.out.println(&quot;thread1&quot;);\n                try {\n                    String value = cache.get(&quot;key&quot;, new Callable&lt;String&gt;() {\n                        public String call() throws Exception {\n                            System.out.println(&quot;load1&quot;); \/\/\u52a0\u8f7d\u6570\u636e\u7ebf\u7a0b\u6267\u884c\u6807\u5fd7\n                            Thread.sleep(1000); \/\/\u6a21\u62df\u52a0\u8f7d\u65f6\u95f4\n                            return &quot;auto load by Callable&quot;;\n                        }\n                    });\n                    System.out.println(&quot;thread1 &quot; + value);\n                } catch (ExecutionException e) {\n                    e.printStackTrace();\n                }\n            }\n        }).start();\n\n        new Thread(new Runnable() {\n            public void run() {\n                System.out.println(&quot;thread2&quot;);\n                try {\n                    String value = cache.get(&quot;key&quot;, new Callable&lt;String&gt;() {\n                        public String call() throws Exception {\n                            System.out.println(&quot;load2&quot;); \/\/\u52a0\u8f7d\u6570\u636e\u7ebf\u7a0b\u6267\u884c\u6807\u5fd7\n                            Thread.sleep(1000); \/\/\u6a21\u62df\u52a0\u8f7d\u65f6\u95f4\n                            return &quot;auto load by Callable&quot;;\n                        }\n                    });\n                    System.out.println(&quot;thread2 &quot; + value);\n                } catch (ExecutionException e) {\n                    e.printStackTrace();\n                }\n            }\n        }).start();\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\u6709\u4e24\u4e2a\u7ebf\u7a0b\u5171\u4eab\u540c\u4e00\u4e2aCache\u5bf9\u8c61\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528get\u65b9\u6cd5\u83b7\u53d6\u540c\u4e00\u4e2akey\u5bf9\u5e94\u7684\u8bb0\u5f55\u3002\u7531\u4e8ekey\u5bf9\u5e94\u7684\u8bb0\u5f55\u4e0d\u5b58\u5728\uff0c\u6240\u4ee5\u4e24\u4e2a\u7ebf\u7a0b\u90fd\u5728get\u65b9\u6cd5\u5904\u963b\u585e\u3002\u6b64\u5904\u5728call\u65b9\u6cd5\u4e2d\u8c03\u7528<code>Thread.sleep(1000)<\/code>\u6a21\u62df\u7a0b\u5e8f\u4ece\u5916\u5b58\u52a0\u8f7d\u6570\u636e\u7684\u65f6\u95f4\u6d88\u8017\u3002\u4ee3\u7801\u7684\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre><code>thread1\nthread2\nload1\nthread2 auto load by Callable\nthread1 auto load by Callable<\/code><\/pre>\n<p>\u4ece\u7ed3\u679c\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u867d\u7136\u662f\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528get\u65b9\u6cd5\uff0c\u4f46\u53ea\u6709\u4e00\u4e2aget\u65b9\u6cd5\u4e2d\u7684Callable\u4f1a\u88ab\u6267\u884c(\u6ca1\u6709\u6253\u5370\u51faload2)\u3002Guava\u53ef\u4ee5\u4fdd\u8bc1\u5f53\u6709\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95eeCache\u4e2d\u7684\u4e00\u4e2akey\u65f6\uff0c\u5982\u679ckey\u5bf9\u5e94\u7684\u8bb0\u5f55\u4e0d\u5b58\u5728\uff0cGuava\u53ea\u4f1a\u542f\u52a8\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884cget\u65b9\u6cd5\u4e2dCallable\u53c2\u6570\u5bf9\u5e94\u7684\u4efb\u52a1\u52a0\u8f7d\u6570\u636e\u5b58\u5230\u7f13\u5b58\u3002\u5f53\u52a0\u8f7d\u5b8c\u6570\u636e\u540e\uff0c\u4efb\u4f55\u7ebf\u7a0b\u4e2d\u7684get\u65b9\u6cd5\u90fd\u4f1a\u83b7\u53d6\u5230key\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<h3>\u7edf\u8ba1\u4fe1\u606f<\/h3>\n<p>\u53ef\u4ee5\u5bf9Cache\u7684\u547d\u4e2d\u7387\u3001\u52a0\u8f7d\u6570\u636e\u65f6\u95f4\u7b49\u4fe1\u606f\u8fdb\u884c\u7edf\u8ba1\u3002\u5728\u6784\u5efaCache\u5bf9\u8c61\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7CacheBuilder\u7684<code>recordStats<\/code>\u65b9\u6cd5\u5f00\u542f\u7edf\u8ba1\u4fe1\u606f\u7684\u5f00\u5173\u3002\u5f00\u5173\u5f00\u542f\u540eCache\u4f1a\u81ea\u52a8\u5bf9\u7f13\u5b58\u7684\u5404\u79cd\u64cd\u4f5c\u8fdb\u884c\u7edf\u8ba1\uff0c\u8c03\u7528Cache\u7684stats\u65b9\u6cd5\u53ef\u4ee5\u67e5\u770b\u7edf\u8ba1\u540e\u7684\u4fe1\u606f\u3002<\/p>\n<pre><code class=\"language-java\">public class StudyGuavaCache {\n    public static void main(String[] args) throws InterruptedException {\n        Cache&lt;String,String&gt; cache = CacheBuilder.newBuilder()\n                .maximumSize(3)\n                .recordStats() \/\/\u5f00\u542f\u7edf\u8ba1\u4fe1\u606f\u5f00\u5173\n                .build();\n        cache.put(&quot;key1&quot;, &quot;value1&quot;);\n        cache.put(&quot;key2&quot;, &quot;value2&quot;);\n        cache.put(&quot;key3&quot;, &quot;value3&quot;);\n        cache.put(&quot;key4&quot;, &quot;value4&quot;);\n\n        cache.getIfPresent(&quot;key1&quot;);\n        cache.getIfPresent(&quot;key2&quot;);\n        cache.getIfPresent(&quot;key3&quot;);\n        cache.getIfPresent(&quot;key4&quot;);\n        cache.getIfPresent(&quot;key5&quot;);\n        cache.getIfPresent(&quot;key6&quot;);\n\n        System.out.println(cache.stats()); \/\/\u83b7\u53d6\u7edf\u8ba1\u4fe1\u606f\n    }\n}<\/code><\/pre>\n<p>\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>CacheStats{hitCount=3, missCount=3, loadSuccessCount=0, loadExceptionCount=0, totalLoadTime=0, evictionCount=1}<\/code><\/pre>\n<p>\u8fd9\u4e9b\u7edf\u8ba1\u4fe1\u606f\u5bf9\u4e8e\u8c03\u6574\u7f13\u5b58\u8bbe\u7f6e\u662f\u81f3\u5173\u91cd\u8981\u7684\uff0c\u5728\u6027\u80fd\u8981\u6c42\u9ad8\u7684\u5e94\u7528\u4e2d\u5e94\u8be5\u5bc6\u5207\u5173\u6ce8\u8fd9\u4e9b\u6570\u636e<\/p>\n<h3>LoadingCache<\/h3>\n<p>LoadingCache\u662fCache\u7684\u5b50\u63a5\u53e3\uff0c\u76f8\u6bd4\u8f83\u4e8eCache\uff0c\u5f53\u4eceLoadingCache\u4e2d\u8bfb\u53d6\u4e00\u4e2a\u6307\u5b9akey\u7684\u8bb0\u5f55\u65f6\uff0c\u5982\u679c\u8be5\u8bb0\u5f55\u4e0d\u5b58\u5728\uff0c\u5219LoadingCache\u53ef\u4ee5\u81ea\u52a8\u6267\u884c\u52a0\u8f7d\u6570\u636e\u5230\u7f13\u5b58\u7684\u64cd\u4f5c\u3002LoadingCache\u63a5\u53e3\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">public interface LoadingCache&lt;K, V&gt; extends Cache&lt;K, V&gt;, Function&lt;K, V&gt; {\n\n    V get(K key) throws ExecutionException;\n\n    V getUnchecked(K key);\n\n    ImmutableMap&lt;K, V&gt; getAll(Iterable&lt;? extends K&gt; keys) throws ExecutionException;\n\n    V apply(K key);\n\n    void refresh(K key);\n\n    @Override\n    ConcurrentMap&lt;K, V&gt; asMap();\n}<\/code><\/pre>\n<p>\u4e0e\u6784\u5efaCache\u7c7b\u578b\u7684\u5bf9\u8c61\u7c7b\u4f3c\uff0cLoadingCache\u7c7b\u578b\u7684\u5bf9\u8c61\u4e5f\u662f\u901a\u8fc7CacheBuilder\u8fdb\u884c\u6784\u5efa\uff0c\u4e0d\u540c\u7684\u662f\uff0c\u5728\u8c03\u7528CacheBuilder\u7684build\u65b9\u6cd5\u65f6\uff0c\u5fc5\u987b\u4f20\u9012\u4e00\u4e2aCacheLoader\u7c7b\u578b\u7684\u53c2\u6570\uff0cCacheLoader\u7684load\u65b9\u6cd5\u9700\u8981\u6211\u4eec\u63d0\u4f9b\u5b9e\u73b0\u3002\u5f53\u8c03\u7528LoadingCache\u7684get\u65b9\u6cd5\u65f6\uff0c\u5982\u679c\u7f13\u5b58\u4e0d\u5b58\u5728\u5bf9\u5e94key\u7684\u8bb0\u5f55\uff0c\u5219CacheLoader\u4e2d\u7684load\u65b9\u6cd5\u4f1a\u88ab\u81ea\u52a8\u8c03\u7528\u4ece\u5916\u5b58\u52a0\u8f7d\u6570\u636e\uff0cload\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u4f1a\u4f5c\u4e3akey\u5bf9\u5e94\u7684value\u5b58\u50a8\u5230LoadingCache\u4e2d\uff0c\u5e76\u4eceget\u65b9\u6cd5\u8fd4\u56de\u3002<\/p>\n<pre><code class=\"language-java\">public class StudyGuavaCache {\n    public static void main(String[] args) throws ExecutionException {\n        CacheLoader&lt;String, String&gt; loader = new CacheLoader&lt;String, String&gt; () {\n            public String load(String key) throws Exception {\n                Thread.sleep(1000); \/\/\u4f11\u77201s\uff0c\u6a21\u62df\u52a0\u8f7d\u6570\u636e\n                System.out.println(key + &quot; is loaded from a cacheLoader!&quot;);\n                return key + &quot;&#039;s value&quot;;\n            }\n        };\n\n        LoadingCache&lt;String,String&gt; loadingCache = CacheBuilder.newBuilder()\n                .maximumSize(3)\n                .build(loader);\/\/\u5728\u6784\u5efa\u65f6\u6307\u5b9a\u81ea\u52a8\u52a0\u8f7d\u5668\n\n        loadingCache.get(&quot;key1&quot;);\n        loadingCache.get(&quot;key2&quot;);\n        loadingCache.get(&quot;key3&quot;);\n    }\n}<\/code><\/pre>\n<pre><code>key1 is loaded from a cacheLoader!\nkey2 is loaded from a cacheLoader!\nkey3 is loaded from a cacheLoader!<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Preconditions \u524d\u7f6e\u6821\u9a8c \u524d\u7f6e\u6761\u4ef6\u9002\u7528\u4e8e\u5f53\u5224\u65ad\u4e0e\u8bbe\u7f6e\u7684\u6761\u4ef6\u4e0d\u7b26\u5408\u65f6\uff0c\u629b\u51fa\u5f02\u5e38\u7684\u64cd\u4f5c\uff08\u6ce8\u610f\uff1a\u662f\u629b\u51fa\u5f02 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[113],"class_list":["post-682","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-guava"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/682","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=682"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/682\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}