{"id":55457,"date":"2025-08-13T22:54:33","date_gmt":"2025-08-13T14:54:33","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/55457.html"},"modified":"2025-08-13T22:54:33","modified_gmt":"2025-08-13T14:54:33","slug":"hbase-blockcache%ef%bc%9alru-cache","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/55457.html","title":{"rendered":"HBase BlockCache\uff1aLRU Cache"},"content":{"rendered":"<h2>BlockCache \u7684\u5185\u5b58\u4f7f\u7528&#xff1a;\u7075\u6d3b\u7684\u4e8c\u7ea7\u7f13\u5b58&#xff08;L1&#043;L2&#xff09;\u67b6\u6784<\/h2>\n<p>HBase \u63d0\u4f9b\u4e86\u4e24\u79cd\u4e3b\u8981\u7684 BlockCache \u5b9e\u73b0&#xff1a;<\/p>\n<li>LruBlockCache: \u8fd9\u662f\u57fa\u4e8e\u5806\u5185\u5185\u5b58\u7684\u3001\u91c7\u7528 LRU (Least Recently Used) \u6dd8\u6c70\u7b97\u6cd5\u7684\u7f13\u5b58\u3002\u5b83\u7ba1\u7406\u7684\u662f\u666e\u901a\u7684 Java \u5bf9\u8c61&#xff0c;\u4f1a\u53d7\u5230 GC \u7684\u5f71\u54cd\u3002<\/li>\n<li>BucketCache: \u8fd9\u662f\u4e00\u79cd\u4e3b\u8981\u7528\u4e8e\u5806\u5916\u5185\u5b58\u7684\u7f13\u5b58\u3002\u5b83\u53ef\u4ee5\u5c06\u6570\u636e\u5757\u7f13\u5b58\u5230\u00a0DirectByteBuffer&#xff08;\u5806\u5916&#xff09;\u3001\u751a\u81f3\u662f SSD \u6587\u4ef6\u6216\u5185\u5b58\u6620\u5c04\u6587\u4ef6&#xff08;mmap&#xff09;\u4e2d\u3002<\/li>\n<p>\u5728\u73b0\u4ee3 HBase \u7248\u672c&#xff08;2.0&#043;&#xff09;\u4e2d&#xff0c;\u5f53\u542f\u7528\u00a0BucketCache\u00a0\u65f6&#xff0c;\u901a\u5e38\u4f1a\u91c7\u7528\u4e00\u79cd\u540d\u4e3a\u00a0CombinedBlockCache\u00a0\u7684\u7ec4\u5408\u6a21\u5f0f&#xff1a;<\/p>\n<ul>\n<li>\u5806\u5185\u00a0LruBlockCache\u00a0(L1 Cache): \u4f5c\u4e3a\u4e00\u7ea7\u7f13\u5b58&#xff0c;\u66f4\u591a\u662f\u4e3a\u4e86\u7f13\u5b58\u5143\u6570\u636e\u5757&#xff08;META blocks&#xff09;&#xff0c;\u5982 HFile \u7684\u7d22\u5f15\u5757&#xff08;INDEX blocks&#xff09;\u548c\u5e03\u9686\u8fc7\u6ee4\u5668\u5757&#xff08;BLOOM blocks&#xff09;\u3002\u8fd9\u4e9b\u5757\u901a\u5e38\u8f83\u5c0f\u4e14\u8bbf\u95ee\u9891\u7e41&#xff0c;\u653e\u5728\u5806\u5185\u53ef\u4ee5\u83b7\u5f97\u6781\u4f4e\u7684\u8bbf\u95ee\u5ef6\u8fdf\u3002<\/li>\n<li>\u5806\u5916\u00a0BucketCache\u00a0(L2 Cache): \u4f5c\u4e3a\u4e8c\u7ea7\u7f13\u5b58&#xff0c;\u4e13\u95e8\u7f13\u5b58\u6570\u636e\u5757&#xff08;DATA blocks&#xff09;\u3002\u6570\u636e\u5757\u901a\u5e38\u5f88\u5927&#xff0c;\u6784\u6210\u4e86\u7f13\u5b58\u7684\u4e3b\u4f53\u3002\u5c06\u5b83\u4eec\u653e\u5728\u5806\u5916\u53ef\u4ee5\u7f13\u5b58\u5de8\u5927\u7684\u6570\u636e\u91cf&#xff08;\u51e0\u5341\u4e0a\u767eGB&#xff09;\u800c\u5b8c\u5168\u4e0d\u589e\u52a0 JVM \u7684 GC \u538b\u529b&#xff0c;\u4ece\u800c\u907f\u514d\u4e86\u56e0\u7f13\u5b58\u8fc7\u5927\u5bfc\u81f4\u7684\u957f\u65f6\u95f4 GC \u505c\u987f\u3002<\/li>\n<\/ul>\n<p>\u4ece\u6587\u6863\u7247\u6bb5\u4e2d\u4e5f\u80fd\u5370\u8bc1\u8fd9\u4e00\u70b9&#xff1a;<\/p>\n<p>src\/main\/asciidoc\/_chapters\/architecture.adoc:973-998\u00a0<\/p>\n<p>&#8230;when BucketCache is enabled, all DATA blocks are kept in the BucketCache tier and meta blocks &#8212; INDEX and BLOOM blocks &#8212; are on-heap in the\u00a0LruBlockCache.<\/p>\n<h2>\u4e3a\u4ec0\u4e48HBase \u6ca1\u6709\u4f7f\u7528\u54c8\u5e0c\u53cc\u94fe\u8868\u5b9e\u73b0LRU<\/h2>\n<p>LruBlockCache\u00a0\u6ca1\u6709\u4f7f\u7528 \u5e38\u89c1\u7684\u201c\u54c8\u5e0c\u8868 &#043; \u53cc\u5411\u94fe\u8868\u201d\u6765\u5b9e\u73b0 LRU\u3002\u8fd9\u80cc\u540e\u5176\u5b9e\u662f HBase \u9488\u5bf9\u5176\u7279\u5b9a\u573a\u666f\u505a\u51fa\u7684\u6027\u80fd\u6743\u8861\u548c\u8bbe\u8ba1\u9009\u62e9\u3002<\/p>\n<p>\u7b80\u5355\u6765\u8bf4&#xff0c;\u539f\u56e0\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u70b9&#xff1a;<\/p>\n<li>\u8fd9\u4e0d\u662f\u4e00\u4e2a\u7b80\u5355\u7684 LRU&#xff0c;\u800c\u662f\u4e00\u4e2a\u5e26\u4f18\u5148\u7ea7\u7684\u3001\u5206\u533a\u7684\u7f13\u5b58\u7b56\u7565\u3002<\/li>\n<li>\u4e3a\u4e86\u4f18\u5316\u9ad8\u5e76\u53d1\u8bfb\u5199\u6027\u80fd&#xff0c;\u5c06\u590d\u6742\u6027\u63a8\u8fdf\u5230\u9a71\u9010&#xff08;Eviction&#xff09;\u9636\u6bb5\u3002<\/li>\n<h3>\u590d\u6742\u7684\u5206\u533a\u548c\u4f18\u5148\u7ea7\u7b56\u7565<\/h3>\n<p>\u4ece\u00a0evict()\u00a0\u65b9\u6cd5\u7684\u5b9e\u73b0\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230&#xff0c;HBase \u7684\u00a0LruBlockCache\u00a0\u5e76\u4e0d\u662f\u4e00\u4e2a\u5355\u4e00\u7684 LRU \u961f\u5217\u3002\u5b83\u5c06\u7f13\u5b58\u7684 Block \u5206\u4e3a\u4e09\u4e2a\u4e0d\u540c\u7684\u4f18\u5148\u7ea7\u6876&#xff08;Bucket&#xff09;&#xff1a;<\/p>\n<ul>\n<li>bucketSingle: \u5b58\u653e\u53ea\u88ab\u8bbf\u95ee\u8fc7\u4e00\u6b21\u7684 Block\u3002<\/li>\n<li>bucketMulti: \u5b58\u653e\u88ab\u591a\u6b21\u8bbf\u95ee\u7684 Block\u3002<\/li>\n<li>bucketMemory: \u5b58\u653e\u9700\u8981\u5e38\u9a7b\u5185\u5b58\u7684 Block&#xff08;\u4f8b\u5982\u5143\u6570\u636e\u3001\u7d22\u5f15\u7b49\u9ad8\u4f18\u5148\u7ea7\u6570\u636e&#xff09;\u3002<\/li>\n<\/ul>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n      \/\/ Instantiate priority buckets<br \/>\n      BlockBucket bucketSingle &#061; new BlockBucket(&#034;single&#034;, bytesToFree, blockSize, singleSize());<br \/>\n      BlockBucket bucketMulti &#061; new BlockBucket(&#034;multi&#034;, bytesToFree, blockSize, multiSize());<br \/>\n      BlockBucket bucketMemory &#061; new BlockBucket(&#034;memory&#034;, bytesToFree, blockSize, memorySize());<\/p>\n<p>      \/\/ Scan entire map putting into appropriate buckets<br \/>\n      for (LruCachedBlock cachedBlock : map.values()) {<br \/>\n        switch (cachedBlock.getPriority()) {<br \/>\n          case SINGLE: {<br \/>\n            bucketSingle.add(cachedBlock);<br \/>\n            break;<br \/>\n          }<br \/>\n          case MULTI: {<br \/>\n            bucketMulti.add(cachedBlock);<br \/>\n            break;<br \/>\n          }<br \/>\n          case MEMORY: {<br \/>\n            bucketMemory.add(cachedBlock);<br \/>\n            break;<br \/>\n          }<br \/>\n        }<br \/>\n      }<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<p>\u9a71\u9010\u6570\u636e\u65f6&#xff0c;\u5b83\u7684\u903b\u8f91\u4e5f\u6bd4\u7b80\u5355\u7684\u201c\u6dd8\u6c70\u6700\u4e45\u672a\u4f7f\u7528\u7684\u201d\u590d\u6742\u5f97\u591a\u3002\u5b83\u4f1a\u6839\u636e\u6bcf\u4e2a\u6876\u7684\u914d\u7f6e\u5927\u5c0f&#xff08;singleFactor,\u00a0multiFactor,\u00a0memoryFactor&#xff09;\u548c\u5f53\u524d\u5b9e\u9645\u5927\u5c0f\u8ba1\u7b97\u51fa\u201c\u6ea2\u51fa\u201d\u91cf&#xff0c;\u7136\u540e\u6309\u6bd4\u4f8b\u4ece\u4e0d\u540c\u7684\u6876\u4e2d\u6dd8\u6c70\u6570\u636e\u3002<\/p>\n<p>\u5982\u679c\u4f7f\u7528\u53cc\u5411\u94fe\u8868&#xff0c;\u5c31\u9700\u8981\u7ef4\u62a4\u4e09\u4e2a\u72ec\u7acb\u7684\u94fe\u8868&#xff0c;\u5e76\u4e14\u5728 Block \u7684\u4f18\u5148\u7ea7\u53d1\u751f\u53d8\u5316\u65f6&#xff08;\u4f8b\u5982&#xff0c;\u4e00\u4e2a\u00a0SINGLE\u00a0\u8bbf\u95ee\u7684 Block \u88ab\u518d\u6b21\u8bbf\u95ee&#xff0c;\u9700\u8981\u5347\u7ea7\u5230\u00a0MULTI&#xff09;&#xff0c;\u9700\u8981\u5728\u4e0d\u540c\u94fe\u8868\u4e4b\u95f4\u79fb\u52a8\u8282\u70b9&#xff0c;\u8fd9\u4f1a\u589e\u52a0\u5e76\u53d1\u63a7\u5236\u7684\u590d\u6742\u6027\u3002<\/p>\n<h3>\u4f18\u5316\u5e76\u53d1\u8bfb\u5199&#xff0c;\u7b80\u5316\u9501\u7ade\u4e89<\/h3>\n<p>HBase \u662f\u4e00\u4e2a\u9ad8\u5e76\u53d1\u7684\u5206\u5e03\u5f0f\u6570\u636e\u5e93&#xff0c;\u5b83\u7684 BlockCache \u4f1a\u88ab\u5927\u91cf\u7684\u8bfb\u5199\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u3002\u8bbe\u8ba1\u4e0a\u5fc5\u987b\u4f18\u5148\u4fdd\u8bc1\u00a0getBlock()&#xff08;\u8bfb\u7f13\u5b58&#xff09;\u548c\u00a0cacheBlock()&#xff08;\u5199\u7f13\u5b58&#xff09;\u7684\u6027\u80fd\u3002<\/p>\n<ul>\n<li>\n<p>\u8bfb\u64cd\u4f5c (getBlock)&#xff1a;\u5728\u00a0LruBlockCache\u00a0\u4e2d&#xff0c;\u4e00\u6b21\u7f13\u5b58\u547d\u4e2d&#xff08;hit&#xff09;\u7684\u64cd\u4f5c\u975e\u5e38\u5feb\u3002\u5b83\u901a\u8fc7\u00a0ConcurrentHashMap.computeIfPresent\u00a0\u6765\u5b9e\u73b0&#xff0c;\u8fd9\u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u662f\u65e0\u9501\u7684\u3002\u547d\u4e2d\u540e&#xff0c;\u4ec5\u4ec5\u662f\u8c03\u7528\u00a0cb.access()\u00a0\u66f4\u65b0\u4e00\u4e0b\u8bbf\u95ee\u65f6\u95f4\u548c\u8ba1\u6570\u5668&#xff0c;\u8fd9\u4e2a\u64cd\u4f5c\u4e5f\u662f\u8f7b\u91cf\u7ea7\u7684&#xff0c;\u4e0d\u6d89\u53ca\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u4fee\u6539&#xff08;\u5982\u94fe\u8868\u6307\u9488\u7684\u79fb\u52a8&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u5199\u64cd\u4f5c (cacheBlock)&#xff1a;\u4e3b\u8981\u662f\u5411\u00a0ConcurrentHashMap\u00a0\u4e2d\u00a0put\u00a0\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5982\u679c\u4f7f\u7528\u4f20\u7edf\u7684\u54c8\u5e0c\u53cc\u5411\u94fe\u8868&#xff0c;\u6bcf\u6b21\u8bbf\u95ee&#xff08;getBlock&#xff09;\u90fd\u9700\u8981\u79fb\u52a8\u8282\u70b9\u5230\u94fe\u8868\u5934\u90e8&#xff0c;\u8fd9\u4e2a\u64cd\u4f5c\u9700\u8981\u5bf9\u94fe\u8868\u8fdb\u884c\u5199\u64cd\u4f5c&#xff0c;\u5fc5\u987b\u52a0\u9501\u6765\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u8fd9\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u4f1a\u6210\u4e3a\u4e25\u91cd\u7684\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<p>LruBlockCache\u00a0\u7684\u8bbe\u8ba1\u505a\u4e86\u4e00\u4e2a\u975e\u5e38\u660e\u786e\u7684\u53d6\u820d&#xff1a;\u5c06\u590d\u6742\u6027\u548c\u6027\u80fd\u5f00\u9500\u4ece\u9ad8\u9891\u7684\u8bfb\u64cd\u4f5c\u8f6c\u79fb\u5230\u4e86\u76f8\u5bf9\u4f4e\u9891\u7684\u9a71\u9010\u64cd\u4f5c\u4e0a\u3002<\/p>\n<p>evict()\u00a0\u65b9\u6cd5\u5728\u6267\u884c\u65f6\u4f1a\u83b7\u53d6\u4e00\u4e2a\u5168\u5c40\u7684\u00a0evictionLock&#xff0c;\u6b64\u65f6\u6574\u4e2a\u9a71\u9010\u8fc7\u7a0b\u662f\u4e32\u884c\u6267\u884c\u7684\u3002\u901a\u8fc7\u904d\u5386\u00a0map\u00a0\u7684\u6240\u6709\u00a0values()\u00a0\u6765\u5b9e\u65f6\u6784\u5efa\u5404\u4e2a\u9a71\u9010\u6876\u548c\u4f18\u5148\u7ea7\u961f\u5217&#xff0c;\u867d\u7136\u8fd9\u4e2a\u64cd\u4f5c\u662f O(N) \u7684&#xff0c;\u4f46\u5b83\u907f\u514d\u4e86\u5728\u6bcf\u6b21\u8bfb\u7f13\u5b58\u65f6\u90fd\u8fdb\u884c\u52a0\u9501\u7684\u94fe\u8868\u64cd\u4f5c\u3002\u5bf9\u4e8e\u4e00\u4e2a\u8bbe\u8ba1\u826f\u597d\u3001\u547d\u4e2d\u7387\u9ad8\u7684\u7f13\u5b58\u6765\u8bf4&#xff0c;\u8fd9\u79cd\u201c\u8bfb\u5feb&#xff0c;\u9a71\u9010\u6162\u201d\u7684\u8bbe\u8ba1\u662f\u5b8c\u5168\u503c\u5f97\u7684\u3002<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>LruBlockCache\u00a0\u4e0d\u4f7f\u7528\u54c8\u5e0c\u53cc\u5411\u94fe\u8868&#xff0c;\u662f\u56e0\u4e3a&#xff1a;<\/p>\n<li>\u7b56\u7565\u590d\u6742&#xff1a;\u5b83\u5b9e\u73b0\u4e86\u57fa\u4e8e\u00a0SINGLE,\u00a0MULTI,\u00a0MEMORY\u00a0\u4e09\u79cd\u4f18\u5148\u7ea7\u7684\u590d\u6742\u9a71\u9010\u7b56\u7565&#xff0c;\u800c\u975e\u5355\u4e00\u7684 LRU\u3002<\/li>\n<li>\u5e76\u53d1\u4f18\u5316&#xff1a;\u5b83\u9009\u62e9\u4e86\u4e00\u79cd\u201c\u8bfb\u65f6\u65e0\u9501\/\u8f7b\u91cf\u9501&#xff0c;\u9a71\u9010\u65f6\u52a0\u91cd\u9501\u201d\u7684\u6a21\u5f0f&#xff0c;\u901a\u8fc7\u5728\u9a71\u9010\u65f6\u8fdb\u884c O(N) \u7684\u5168\u91cf\u626b\u63cf\u548c\u91cd\u5efa&#xff0c;\u6765\u907f\u514d\u5728\u9ad8\u5e76\u53d1\u7684\u8bfb\u8def\u5f84\u4e0a\u5f15\u5165\u91cd\u91cf\u7ea7\u7684\u9501&#xff08;\u5982\u94fe\u8868\u64cd\u4f5c\u9501&#xff09;&#xff0c;\u4ece\u800c\u6700\u5927\u5316\u8bfb\u6027\u80fd\u3002<\/li>\n<p>\u8fd9\u79cd\u8bbe\u8ba1\u662f\u5178\u578b\u7684\u5728\u7279\u5b9a\u4e1a\u52a1\u573a\u666f\u4e0b\u5bf9\u901a\u7528\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u6743\u8861\u548c\u4f18\u5316\u7684\u8303\u4f8b\u3002<\/p>\n<\/p>\n<h2 style=\"background-color:transparent\">\u4e3a\u4ec0\u4e48Caffeine \u4f7f\u7528\u54c8\u5e0c\u548c\u53cc\u94fe\u8868 \u5b9e\u73b0\u591a\u961f\u5217<\/h2>\n<p>Caffeine\u00a0 \u901a\u8fc7\u591a\u4e2a\u961f\u5217\u548c\u590d\u6742\u7684\u7b97\u6cd5&#xff08;W-TinyLFU&#xff09;\u5728\u7ef4\u6301 O(1) \u590d\u6742\u5ea6\u7684\u540c\u65f6&#xff0c;\u5b9e\u73b0\u4e86\u6bd4\u4f20\u7edf LRU \u66f4\u9ad8\u7684\u547d\u4e2d\u7387\u3002<\/p>\n<p>\u90a3\u4e48&#xff0c;\u4e3a\u4ec0\u4e48 HBase \u7684\u00a0LruBlockCache\u00a0\u6ca1\u6709\u91c7\u7528\u7c7b\u4f3c Caffeine \u7684\u201c\u591a\u961f\u5217&#xff08;\u53cc\u5411\u94fe\u8868&#xff09;\u201d\u8bbe\u8ba1&#xff0c;\u800c\u662f\u9009\u62e9\u4e86\u201c\u9a71\u9010\u65f6\u904d\u5386\u5efa\u5806\u201d\u8fd9\u79cd\u770b\u8d77\u6765\u66f4\u201c\u7b28\u91cd\u201d\u7684\u65b9\u5f0f\u5462&#xff1f;<\/p>\n<p>Caffeine \u901a\u8fc7\u591a\u6307\u9488\u6784\u5efa\u4e86\u4e00\u4e2a\u56fe&#xff0c;\u91cd\u590d\u5229\u7528\u4e86\u8282\u70b9<\/p>\n<p>\u8fd9\u4e3b\u8981\u662f\u7531\u5386\u53f2\u539f\u56e0\u3001\u8bbe\u8ba1\u6f14\u8fdb\u548c\u7279\u5b9a\u573a\u666f\u4e0b\u7684\u6027\u80fd\u6743\u8861\u5171\u540c\u51b3\u5b9a\u7684\u3002<\/p>\n<p>LruBlockCache\u00a0\u662f HBase \u4e2d\u4e00\u4e2a\u76f8\u5f53\u201c\u5143\u8001\u201d\u7ea7\u522b\u7684\u7ec4\u4ef6\u3002\u5728\u5b83\u88ab\u8bbe\u8ba1\u548c\u5b9e\u73b0\u7684\u5e74\u4ee3&#xff08;\u8fdc\u65e9\u4e8e Caffeine \u7684\u51fa\u73b0&#xff09;&#xff0c;\u9762\u4e34\u7684\u4e3b\u8981\u75db\u70b9\u662f\u5982\u4f55\u5728\u9ad8\u5e76\u53d1\u4e0b\u51cf\u5c11\u9501\u7ade\u4e89\u3002<\/p>\n<ul>\n<li>\u4f20\u7edf\u201c\u54c8\u5e0c&#043;\u53cc\u5411\u94fe\u8868\u201d\u7684\u95ee\u9898&#xff1a;\u6b63\u5982\u6211\u4eec\u8ba8\u8bba\u7684&#xff0c;\u6bcf\u6b21\u00a0get\u00a0\u64cd\u4f5c\u90fd\u9700\u8981\u79fb\u52a8\u94fe\u8868\u8282\u70b9&#xff0c;\u8fd9\u610f\u5473\u7740\u9ad8\u9891\u7684\u8bfb\u64cd\u4f5c\u9700\u8981\u52a0\u9501&#xff0c;\u8fd9\u5728 HBase \u7684\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002<\/li>\n<li>HBase \u7684\u9009\u62e9&#xff1a;LruBlockCache\u00a0\u7684\u8bbe\u8ba1\u8005\u505a\u51fa\u4e86\u4e00\u4e2a\u975e\u5e38\u52a1\u5b9e\u7684\u6743\u8861\u2014\u2014\u727a\u7272\u4f4e\u9891\u7684\u9a71\u9010\u64cd\u4f5c\u7684\u6027\u80fd&#xff0c;\u6765\u6362\u53d6\u9ad8\u9891\u7684\u8bfb\u64cd\u4f5c\u7684\u6781\u81f4\u6027\u80fd\u3002\u8bfb\u64cd\u4f5c\u51e0\u4e4e\u65e0\u9501&#xff0c;\u800c\u9a71\u9010\u64cd\u4f5c\u5219\u901a\u8fc7\u4e00\u4e2a\u5168\u5c40\u9501\u00a0evictionLock\u00a0\u6765\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u867d\u7136\u9a71\u9010\u65f6 O(N) \u7684\u904d\u5386\u5f00\u9500\u5f88\u5927&#xff0c;\u4f46\u53ea\u8981\u9a71\u9010\u4e0d\u662f\u4e00\u4e2a\u6301\u7eed\u53d1\u751f\u7684\u64cd\u4f5c&#xff0c;\u8fd9\u4e2a\u5f00\u9500\u5c31\u53ef\u4ee5\u88ab\u63a5\u53d7\u3002<\/li>\n<\/ul>\n<h3 style=\"background-color:transparent\">Caffeine \u7684\u590d\u6742\u6027 vs. LruBlockCache \u7684\u4e13\u7528\u6027<\/h3>\n<p>Caffeine \u662f\u4e00\u4e2a\u901a\u7528\u7684\u3001\u9876\u7ea7\u7684\u672c\u5730\u7f13\u5b58\u5e93&#xff0c;\u5b83\u7684 W-TinyLFU \u7b97\u6cd5\u975e\u5e38\u590d\u6742&#xff0c;\u9700\u8981\u7cbe\u786e\u5730\u7ba1\u7406\u591a\u4e2a\u961f\u5217\u3001\u9891\u7387\u8349\u56fe&#xff08;Frequency Sketch&#xff09;\u7b49&#xff0c;\u4ee5\u8fbe\u5230\u8fd1\u4f3c\u6700\u4f18\u7684\u7f13\u5b58\u6548\u679c\u3002<\/p>\n<p>\u800c\u00a0LruBlockCache\u00a0\u7684\u76ee\u6807\u5219\u66f4\u4e13\u4e00&#xff1a;\u4e3a HFile \u7684 Block \u670d\u52a1\u3002\u5b83\u7684\u4e09\u7ea7\u4f18\u5148\u7ea7\u8bbe\u8ba1&#xff08;SINGLE,\u00a0MULTI,\u00a0MEMORY&#xff09;\u662f\u4e3a HBase \u7684\u7279\u5b9a\u5de5\u4f5c\u8d1f\u8f7d\u91cf\u8eab\u5b9a\u505a\u7684&#xff1a;<\/p>\n<ul>\n<li>SINGLE&#xff1a;\u62b5\u5fa1\u626b\u63cf&#xff08;Scan&#xff09;\u64cd\u4f5c\u5bf9\u7f13\u5b58\u7684\u6c61\u67d3\u3002\u4e00\u6b21\u626b\u63cf\u4f1a\u8bfb\u5165\u5927\u91cf\u6570\u636e\u5757&#xff0c;\u4f46\u8fd9\u4e9b\u5757\u5f88\u53ef\u80fd\u4e4b\u540e\u4e0d\u518d\u88ab\u8bbf\u95ee&#xff0c;\u5c06\u5b83\u4eec\u653e\u5728\u6700\u4f4e\u4f18\u5148\u7ea7\u53ef\u4ee5\u88ab\u5feb\u901f\u6dd8\u6c70\u3002<\/li>\n<li>MULTI&#xff1a;\u4fdd\u62a4\u88ab\u591a\u6b21\u8bbf\u95ee\u7684\u70ed\u70b9\u6570\u636e\u3002<\/li>\n<li>MEMORY&#xff1a;\u4fdd\u62a4\u5143\u6570\u636e\u7b49\u5e38\u9a7b\u5185\u5b58\u7684\u91cd\u8981\u6570\u636e\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u79cd\u7b80\u5355\u7684\u4e09\u7ea7\u5206\u7c7b\u867d\u7136\u4e0d\u5982 Caffeine \u7cbe\u7ec6&#xff0c;\u4f46\u5bf9\u4e8e HBase \u7684\u573a\u666f\u6765\u8bf4&#xff0c;\u8db3\u591f\u6709\u6548\u4e14\u5b9e\u73b0\u7b80\u5355\u3001\u903b\u8f91\u6e05\u6670\u3001\u6613\u4e8e\u8c03\u8bd5\u3002<\/p>\n<h3>HBase \u5df2\u7ecf\u62e5\u62b1\u4e86 Caffeine<\/h3>\n<p>HBase \u540e\u6765\u5f15\u5165\u4e86\u57fa\u4e8e Caffeine \u7684\u7f13\u5b58\u5b9e\u73b0&#xff01;<\/p>\n<p>\u4e3a\u4e86\u63d0\u4f9b\u66f4\u4f18\u7684\u7f13\u5b58\u6027\u80fd&#xff0c;HBase \u793e\u533a\u5f00\u53d1\u4e86\u00a0TinyLfuBlockCache\u3002\u8fd9\u4e2a\u7c7b\u5176\u5b9e\u5c31\u662f\u5bf9 Caffeine Cache \u7684\u4e00\u4e2a\u5c01\u88c5\u548c\u9002\u914d&#xff0c;\u4f7f\u5176\u80fd\u591f\u65e0\u7f1d\u5730\u5728 HBase \u4e2d\u4f7f\u7528\u3002<\/p>\n<p>\u53ef\u4ee5\u5728 HBase \u7684\u914d\u7f6e\u4e2d&#xff0c;\u901a\u8fc7\u00a0hfile.block.cache.policy\u00a0\u6765\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u7f13\u5b58\u7b56\u7565\u3002<\/p>\n<p>hbase-default.xml<\/p>\n<p>&lt;property&gt;<br \/>\n  &lt;name&gt;hfile.block.cache.policy&lt;\/name&gt;<br \/>\n  &lt;value&gt;LRU&lt;\/value&gt; &lt;!&#8211; \u9ed8\u8ba4\u662f LruBlockCache &#8211;&gt;<br \/>\n  &lt;!&#8211; \u4f60\u53ef\u4ee5\u5c06\u5176\u4fee\u6539\u4e3a TinyLFU \u6765\u542f\u7528\u57fa\u4e8e Caffeine \u7684\u7f13\u5b58 &#8211;&gt;<br \/>\n  &lt;!&#8211; &lt;value&gt;TinyLFU&lt;\/value&gt; &#8211;&gt;<br \/>\n&lt;\/property&gt;<\/p>\n<p>\u4e4b\u6240\u4ee5\u4e0d\u76f4\u63a5\u7528\u00a0TinyLfuBlockCache\u00a0(Caffeine) \u66ff\u6362\u6389\u00a0LruBlockCache&#xff0c;\u800c\u662f\u4f5c\u4e3a\u4e00\u79cd\u53ef\u9009\u9879&#xff0c;\u4e3b\u8981\u6709\u4ee5\u4e0b\u8003\u8651&#xff1a;<\/p>\n<ul>\n<li>\u7a33\u5b9a\u6027\u4e0e\u517c\u5bb9\u6027&#xff1a;LruBlockCache\u00a0\u7ecf\u8fc7\u4e86\u5341\u591a\u5e74\u7684\u5927\u89c4\u6a21\u751f\u4ea7\u73af\u5883\u9a8c\u8bc1&#xff0c;\u5176\u7a33\u5b9a\u6027\u548c\u884c\u4e3a\u662f\u4f17\u6240\u5468\u77e5\u7684\u3002\u76f4\u63a5\u66ff\u6362\u6838\u5fc3\u7ec4\u4ef6\u98ce\u9669\u5f88\u9ad8\u3002<\/li>\n<li>\u63d0\u4f9b\u9009\u62e9&#xff1a;\u8ba9\u7528\u6237\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u4e1a\u52a1\u8d1f\u8f7d\u548c\u5bf9\u6027\u80fd\u7684\u8981\u6c42\u6765\u9009\u62e9\u6700\u5408\u9002\u7684\u7f13\u5b58\u7b56\u7565\u3002\u5bf9\u4e8e\u67d0\u4e9b\u7279\u5b9a\u573a\u666f&#xff0c;\u7b80\u5355\u7684 LRU \u53ef\u80fd\u5df2\u7ecf\u8db3\u591f&#xff0c;\u751a\u81f3\u884c\u4e3a\u66f4\u53ef\u9884\u6d4b\u3002<\/li>\n<\/ul>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u53ef\u4ee5\u8fd9\u6837\u7406\u89e3&#xff1a;<\/p>\n<ul>\n<li>LruBlockCache\u00a0\u662f HBase \u7684 \u201c\u7ecf\u5178\u6b3e\u201d \u3002\u5b83\u91c7\u7528\u4e86\u4e00\u79cd\u52a1\u5b9e\u7684\u8bbe\u8ba1&#xff0c;\u901a\u8fc7\u727a\u7272\u9a71\u9010\u6027\u80fd\u6765\u4fdd\u8bc1\u9ad8\u5e76\u53d1\u8bfb\u7684\u6027\u80fd&#xff0c;\u5e76\u4e14\u5176\u7b80\u5355\u7684\u4e09\u7ea7\u4f18\u5148\u7ea7\u7b56\u7565\u5bf9\u4e8e HFile Block \u7f13\u5b58\u8fd9\u4e2a\u7279\u5b9a\u573a\u666f\u662f\u957f\u671f\u6709\u6548\u7684\u3002<\/li>\n<li>Caffeine (TinyLfuBlockCache)\u00a0\u662f HBase \u5f15\u5165\u7684 \u201c\u6027\u80fd\u6b3e\u201d \u3002\u5b83\u5229\u7528\u4e86\u73b0\u4ee3\u7f13\u5b58\u7814\u7a76\u7684\u6700\u65b0\u6210\u679c&#xff0c;\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u7f13\u5b58\u547d\u4e2d\u7387\u548c\u6574\u4f53\u6027\u80fd&#xff0c;\u662f\u8ffd\u6c42\u6781\u81f4\u6027\u80fd\u7528\u6237\u7684\u9996\u9009\u3002<\/li>\n<\/ul>\n<p>\u6240\u4ee5&#xff0c;HBase \u5e76\u975e\u6ca1\u6709\u770b\u5230\u66f4\u4f18\u7684\u65b9\u6848&#xff0c;\u800c\u662f\u901a\u8fc7\u201c\u63d2\u4ef6\u5316\u201d\u7684\u65b9\u5f0f&#xff0c;\u5728\u4fdd\u8bc1\u7cfb\u7edf\u7a33\u5b9a\u6027\u7684\u524d\u63d0\u4e0b&#xff0c;\u4f18\u96c5\u5730\u96c6\u6210\u4e86\u66f4\u73b0\u4ee3\u3001\u66f4\u9ad8\u6548\u7684\u8bbe\u8ba1\u3002<\/p>\n<\/p>\n<h2 style=\"background-color:transparent\">LruCachedBlock\u00a0<\/h2>\n<p>LruCachedBlock\u00a0\u662f Apache HBase \u4e2d LRU (Least Recently Used, \u6700\u8fd1\u6700\u5c11\u4f7f\u7528) \u7f13\u5b58\u7b56\u7565\u7684\u6838\u5fc3\u5b9e\u73b0\u4e4b\u4e00\u3002\u5b83\u4ee3\u8868\u4e86\u00a0LruBlockCache\u00a0\u4e2d\u7684\u4e00\u4e2a\u7f13\u5b58\u6761\u76ee\u3002\u6211\u4eec\u53ef\u4ee5\u4ece\u5b83\u7684\u5b9a\u4e49\u3001\u6210\u5458\u53d8\u91cf\u3001\u6784\u9020\u51fd\u6570\u548c\u5173\u952e\u65b9\u6cd5\u7b49\u65b9\u9762\u6765\u6df1\u5165\u7406\u89e3\u5176\u8bbe\u8ba1\u548c\u4f5c\u7528\u3002<\/p>\n<h4><\/h4>\n<p>&#064;InterfaceAudience.Private<br \/>\npublic class LruCachedBlock implements HeapSize, Comparable&lt;LruCachedBlock&gt; {<br \/>\n\/\/&#8230;<br \/>\n}<\/p>\n<ul>\n<li>&#064;InterfaceAudience.Private: \u8fd9\u4e2a\u6ce8\u89e3\u8868\u660e\u8be5\u7c7b\u662f HBase \u5185\u90e8\u4f7f\u7528\u7684\u79c1\u6709 API&#xff0c;\u4e0d\u5efa\u8bae\u5916\u90e8\u5e94\u7528\u76f4\u63a5\u4f9d\u8d56\u5b83&#xff0c;\u56e0\u4e3a\u5b83\u7684\u63a5\u53e3\u53ef\u80fd\u4f1a\u5728\u4e0d\u540c\u7248\u672c\u95f4\u53d1\u751f\u53d8\u5316\u3002<\/li>\n<li>implements HeapSize: \u8fd9\u4e2a\u63a5\u53e3\u610f\u5473\u7740\u00a0LruCachedBlock\u00a0\u7684\u5b9e\u4f8b\u80fd\u591f\u62a5\u544a\u5b83\u5728 JVM \u5806\u5185\u5b58\u4e2d\u6240\u5360\u7528\u7684\u7a7a\u95f4\u5927\u5c0f\u3002\u8fd9\u5bf9\u4e8e\u7f13\u5b58\u7cfb\u7edf\u6765\u8bf4\u81f3\u5173\u91cd\u8981&#xff0c;\u56e0\u4e3a\u7f13\u5b58\u9700\u8981\u7cbe\u786e\u5730\u77e5\u9053\u6bcf\u4e2a\u7f13\u5b58\u9879\u5360\u7528\u7684\u5185\u5b58&#xff0c;\u4ece\u800c\u63a7\u5236\u603b\u5185\u5b58\u4f7f\u7528\u91cf&#xff0c;\u5e76\u5728\u8fbe\u5230\u4e0a\u9650\u65f6\u89e6\u53d1\u6dd8\u6c70\u673a\u5236\u3002\n<p>\/\/ &#8230; existing code &#8230;<br \/>\npublic interface HeapSize {<br \/>\n  \/**<br \/>\n   * Return the approximate &#039;exclusive deep size&#039; of implementing object. Includes count of payload<br \/>\n   * and hosting object sizings.<br \/>\n   *\/<br \/>\n  long heapSize();<br \/>\n}<\/p>\n<\/li>\n<li>implements Comparable&lt;LruCachedBlock&gt;: \u8fd9\u4e2a\u63a5\u53e3\u8868\u660e\u00a0LruCachedBlock\u00a0\u7684\u5b9e\u4f8b\u4e4b\u95f4\u53ef\u4ee5\u8fdb\u884c\u6bd4\u8f83\u3002\u8fd9\u662f\u5b9e\u73b0 LRU \u6dd8\u6c70\u7b56\u7565\u7684\u57fa\u7840\u3002\u901a\u8fc7\u6bd4\u8f83&#xff0c;\u53ef\u4ee5\u786e\u5b9a\u54ea\u4e9b\u5757\u662f\u201c\u6700\u4e45\u672a\u88ab\u4f7f\u7528\u201d\u7684&#xff0c;\u4ece\u800c\u5728\u7f13\u5b58\u6ee1\u65f6\u4f18\u5148\u6dd8\u6c70\u5b83\u4eec\u3002<\/li>\n<\/ul>\n<h3>\u6838\u5fc3\u6210\u5458\u53d8\u91cf<\/h3>\n<p>LruCachedBlock\u00a0\u5c01\u88c5\u4e86\u4f5c\u4e3a\u4e00\u4e2a\u7f13\u5b58\u6761\u76ee\u6240\u9700\u7684\u6240\u6709\u4fe1\u606f\u3002<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  public final static long PER_BLOCK_OVERHEAD &#061;<br \/>\n    ClassSize.align(ClassSize.OBJECT &#043; (3 * ClassSize.REFERENCE) &#043; (3 * Bytes.SIZEOF_LONG)<br \/>\n      &#043; ClassSize.STRING &#043; ClassSize.BYTE_BUFFER);<\/p>\n<p>  private final BlockCacheKey cacheKey;<br \/>\n  private final Cacheable buf;<br \/>\n  private volatile long accessTime;<br \/>\n  private long size;<br \/>\n  private BlockPriority priority;<br \/>\n  \/**<br \/>\n   * Time this block was cached. Presumes we are created just before we are added to the cache.<br \/>\n   *\/<br \/>\n  private final long cachedTime &#061; System.nanoTime();<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<ul>\n<li>PER_BLOCK_OVERHEAD: \u8fd9\u662f\u4e00\u4e2a\u9759\u6001\u5e38\u91cf&#xff0c;\u7528\u4e8e\u4f30\u7b97\u4e00\u4e2a\u00a0LruCachedBlock\u00a0\u5bf9\u8c61\u81ea\u8eab\u7684\u56fa\u5b9a\u5f00\u9500\u3002\u5b83\u901a\u8fc7\u00a0ClassSize\u00a0\u5de5\u5177\u7c7b\u8ba1\u7b97\u5f97\u51fa&#xff0c;\u5305\u62ec\u4e86\u5bf9\u8c61\u5934\u30013\u4e2a\u5f15\u7528&#xff08;cacheKey,\u00a0buf,\u00a0priority&#xff09;\u30013\u4e2along\u7c7b\u578b&#xff08;accessTime,\u00a0size,\u00a0cachedTime&#xff09;\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u57fa\u7840\u5bf9\u8c61\u7684\u5f00\u9500\u3002\u8fd9\u4e2a\u503c\u5728\u8ba1\u7b97\u603b\u7684\u00a0heapSize\u00a0\u65f6\u4f1a\u7528\u5230&#xff0c;\u4ee5\u786e\u4fdd\u5185\u5b58\u7edf\u8ba1\u7684\u51c6\u786e\u6027\u3002<\/li>\n<li>cacheKey\u00a0(BlockCacheKey): \u7f13\u5b58\u5757\u7684\u552f\u4e00\u6807\u8bc6\u3002\u901a\u5e38\u7531 HFile \u7684\u6587\u4ef6\u540d\u548c\u5757\u5728\u6587\u4ef6\u4e2d\u7684\u504f\u79fb\u91cf&#xff08;offset&#xff09;\u7ec4\u6210\u3002\u7f13\u5b58\u7cfb\u7edf\u901a\u8fc7\u8fd9\u4e2a key \u6765\u67e5\u627e\u3001\u5b58\u50a8\u548c\u5220\u9664\u5757\u3002<\/li>\n<li>buf\u00a0(Cacheable): \u5b9e\u9645\u7f13\u5b58\u7684\u6570\u636e\u3002Cacheable\u00a0\u662f\u4e00\u4e2a\u63a5\u53e3&#xff0c;\u901a\u5e38\u7684\u5b9e\u73b0\u662f\u00a0HFileBlock&#xff0c;\u5b83\u5c01\u88c5\u4e86\u4ece HFile \u4e2d\u8bfb\u53d6\u7684\u5b57\u8282\u6570\u636e\u3002<\/li>\n<li>accessTime\u00a0(volatile long): \u5757\u7684\u6700\u540e\u8bbf\u95ee\u65f6\u95f4\u3002\u8fd9\u91cc\u7684\u201c\u65f6\u95f4\u201d\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u5355\u8c03\u9012\u589e\u7684\u5e8f\u5217\u53f7&#xff08;\u7531\u00a0LruBlockCache\u00a0\u4e2d\u7684\u4e00\u4e2a\u00a0AtomicLong\u00a0\u8ba1\u6570\u5668\u751f\u6210&#xff09;\u3002\u6bcf\u6b21\u5757\u88ab\u8bbf\u95ee\u65f6&#xff0c;\u8fd9\u4e2a\u503c\u90fd\u4f1a\u88ab\u66f4\u65b0\u3002volatile\u00a0\u5173\u952e\u5b57\u786e\u4fdd\u4e86\u591a\u7ebf\u7a0b\u4e4b\u95f4\u7684\u53ef\u89c1\u6027&#xff0c;\u56e0\u4e3a\u7f13\u5b58\u7684\u8bfb\u5199\u662f\u9ad8\u5e76\u53d1\u7684\u3002\u8fd9\u4e2a\u5b57\u6bb5\u662f\u00a0compareTo\u00a0\u65b9\u6cd5\u7684\u6838\u5fc3&#xff0c;\u76f4\u63a5\u51b3\u5b9a\u4e86\u6dd8\u6c70\u987a\u5e8f\u3002<\/li>\n<li>size\u00a0(long): \u8be5\u7f13\u5b58\u5757\u7684\u603b\u5806\u5185\u5b58\u5360\u7528&#xff0c;\u5728\u6784\u9020\u51fd\u6570\u4e2d\u8ba1\u7b97\u5f97\u51fa\u3002\u5b83\u7b49\u4e8e\u00a0cacheKey\u00a0\u7684\u5927\u5c0f &#043;\u00a0buf\u00a0\u7684\u5927\u5c0f &#043;\u00a0PER_BLOCK_OVERHEAD\u3002<\/li>\n<li>priority\u00a0(BlockPriority): \u5757\u7684\u4f18\u5148\u7ea7\u3002HBase \u7684 LRU \u7f13\u5b58\u5b9e\u73b0\u4e86\u4e00\u79cd\u5e26\u4f18\u5148\u7ea7\u7684\u6dd8\u6c70\u7b56\u7565&#xff0c;\u5206\u4e3a\u4e09\u7ea7&#xff1a;\n<ul>\n<li>SINGLE: \u5355\u6b21\u8bbf\u95ee\u3002\u65b0\u7f13\u5b58\u8fdb\u6765\u7684\u5757\u9ed8\u8ba4\u4e3a\u8fd9\u4e2a\u4f18\u5148\u7ea7\u3002<\/li>\n<li>MULTI: \u591a\u6b21\u8bbf\u95ee\u3002\u5f53\u4e00\u4e2a\u00a0SINGLE\u00a0\u4f18\u5148\u7ea7\u7684\u5757\u88ab\u518d\u6b21\u8bbf\u95ee\u65f6&#xff0c;\u5b83\u7684\u4f18\u5148\u7ea7\u4f1a\u63d0\u5347\u4e3a\u00a0MULTI\u3002\u8fd9\u7ed9\u4e86\u88ab\u8bbf\u95ee\u8fc7\u7684\u5757\u201c\u7b2c\u4e8c\u6b21\u673a\u4f1a\u201d&#xff0c;\u907f\u514d\u4e86\u201c\u7f13\u5b58\u6c61\u67d3\u201d&#xff08;\u5373\u5927\u91cf\u53ea\u88ab\u8bbf\u95ee\u4e00\u6b21\u7684\u5757\u5c06\u70ed\u70b9\u5757\u6324\u51fa\u7f13\u5b58&#xff09;\u3002<\/li>\n<li>MEMORY: \u5185\u5b58\u4e2d\u3002\u901a\u5e38\u7528\u4e8e\u5143\u6570\u636e\u5757&#xff08;\u5982\u7d22\u5f15\u5757&#xff09;\u6216\u8005\u660e\u786e\u6307\u5b9a\u9700\u8981\u9a7b\u7559\u5185\u5b58\u7684\u5757\u3002\u8fd9\u7c7b\u5757\u6709\u6700\u9ad8\u7684\u4f18\u5148\u7ea7&#xff0c;\u6700\u4e0d\u5bb9\u6613\u88ab\u6dd8\u6c70\u3002<\/li>\n<\/ul>\n<\/li>\n<li>cachedTime\u00a0(final long): \u5757\u88ab\u7f13\u5b58\u65f6\u7684\u65f6\u95f4\u6233&#xff08;\u4f7f\u7528\u00a0System.nanoTime()&#xff09;\u3002\u8fd9\u4e2a\u503c\u662f\u4e0d\u53ef\u53d8\u7684&#xff0c;\u4e3b\u8981\u7528\u4e8e\u7f13\u5b58\u7edf\u8ba1&#xff0c;\u4f8b\u5982\u8ba1\u7b97\u5757\u5728\u88ab\u6dd8\u6c70\u524d\u7684\u5b58\u6d3b\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<h3>\u6784\u9020\u51fd\u6570<\/h3>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  public LruCachedBlock(BlockCacheKey cacheKey, Cacheable buf, long accessTime) {<br \/>\n    this(cacheKey, buf, accessTime, false);<br \/>\n  }<\/p>\n<p>  public LruCachedBlock(BlockCacheKey cacheKey, Cacheable buf, long accessTime, boolean inMemory) {<br \/>\n    this.cacheKey &#061; cacheKey;<br \/>\n    this.buf &#061; buf;<br \/>\n    this.accessTime &#061; accessTime;<br \/>\n    \/\/ We approximate the size of this class by the size of its name string<br \/>\n    \/\/ plus the size of its byte buffer plus the overhead associated with all<br \/>\n    \/\/ the base classes. We also include the base class<br \/>\n    \/\/ sizes in the PER_BLOCK_OVERHEAD variable rather than align()ing them with<br \/>\n    \/\/ their buffer lengths. This variable is used elsewhere in unit tests.<br \/>\n    this.size &#061;<br \/>\n      ClassSize.align(cacheKey.heapSize()) &#043; ClassSize.align(buf.heapSize()) &#043; PER_BLOCK_OVERHEAD;<br \/>\n    if (inMemory) {<br \/>\n      this.priority &#061; BlockPriority.MEMORY;<br \/>\n    } else {<br \/>\n      this.priority &#061; BlockPriority.SINGLE;<br \/>\n    }<br \/>\n  }<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<p>\u6784\u9020\u51fd\u6570\u5b8c\u6210\u4e86\u6240\u6709\u5b57\u6bb5\u7684\u521d\u59cb\u5316\u3002<\/p>\n<li>\u5b83\u8bb0\u5f55\u4e86\u00a0cacheKey\u3001buf\u00a0\u548c\u00a0accessTime\u3002<\/li>\n<li>\u6838\u5fc3\u5de5\u4f5c\u662f\u8ba1\u7b97\u00a0size\u3002\u5b83\u5c06\u00a0cacheKey\u00a0\u548c\u00a0buf\u00a0\u7684\u5806\u5927\u5c0f\u5206\u522b\u5bf9\u9f50&#xff08;ClassSize.align&#xff0c;\u901a\u5e38\u662f8\u5b57\u8282\u5bf9\u9f50&#xff09;&#xff0c;\u7136\u540e\u52a0\u4e0a\u9884\u5148\u8ba1\u7b97\u597d\u7684\u00a0PER_BLOCK_OVERHEAD&#xff0c;\u5f97\u5230\u8fd9\u4e2a\u7f13\u5b58\u9879\u7684\u603b\u5185\u5b58\u5360\u7528\u3002<\/li>\n<li>\u6839\u636e\u00a0inMemory\u00a0\u53c2\u6570\u8bbe\u7f6e\u521d\u59cb\u4f18\u5148\u7ea7\u3002\u5982\u679c\u00a0inMemory\u00a0\u4e3a\u00a0true&#xff0c;\u5219\u4e3a\u00a0BlockPriority.MEMORY&#xff1b;\u5426\u5219&#xff0c;\u9ed8\u8ba4\u4e3a\u00a0BlockPriority.SINGLE\u3002<\/li>\n<h3>\u5173\u952e\u65b9\u6cd5<\/h3>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u5b9a\u4e49\u4e86\u00a0LruCachedBlock\u00a0\u7684\u884c\u4e3a&#xff0c;\u662f LRU \u7f13\u5b58\u903b\u8f91\u80fd\u591f\u6b63\u786e\u8fd0\u884c\u7684\u4fdd\u8bc1\u3002<\/p>\n<ul>\n<li>\n<p>access(long accessTime): \u5f53\u7f13\u5b58\u5757\u88ab\u547d\u4e2d\u65f6\u8c03\u7528\u3002<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  public void access(long accessTime) {<br \/>\n    this.accessTime &#061; accessTime;<br \/>\n    if (this.priority &#061;&#061; BlockPriority.SINGLE) {<br \/>\n      this.priority &#061; BlockPriority.MULTI;<br \/>\n    }<br \/>\n  }<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<p>\u5b83\u4f1a\u66f4\u65b0\u00a0accessTime\u00a0\u4e3a\u6700\u65b0\u7684\u8bbf\u95ee\u5e8f\u5217\u53f7&#xff0c;\u5e76\u68c0\u67e5\u5f53\u524d\u4f18\u5148\u7ea7\u3002\u5982\u679c\u4f18\u5148\u7ea7\u662f\u00a0SINGLE&#xff0c;\u5c31\u5c06\u5176\u63d0\u5347\u4e3a\u00a0MULTI\u3002\u8fd9\u4e2a\u673a\u5236\u662f LRU \u7f13\u5b58\u7684\u91cd\u8981\u4f18\u5316\u3002<\/p>\n<\/li>\n<li>\n<p>heapSize(): \u5b9e\u73b0\u4e86\u00a0HeapSize\u00a0\u63a5\u53e3\u3002<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  &#064;Override<br \/>\n  public long heapSize() {<br \/>\n    return size;<br \/>\n  }<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<p>\u76f4\u63a5\u8fd4\u56de\u5728\u6784\u9020\u51fd\u6570\u4e2d\u8ba1\u7b97\u597d\u7684\u00a0size\u00a0\u5b57\u6bb5\u3002<\/p>\n<\/li>\n<li>\n<p>compareTo(LruCachedBlock that): \u5b9e\u73b0\u4e86\u00a0Comparable\u00a0\u63a5\u53e3&#xff0c;\u5b9a\u4e49\u4e86\u6392\u5e8f\u89c4\u5219\u3002<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  &#064;Override<br \/>\n  public int compareTo(LruCachedBlock that) {<br \/>\n    \/\/ Newer accessed blocks sort before older ones.<br \/>\n    if (this.accessTime &#061;&#061; that.accessTime) return 0;<br \/>\n    return this.accessTime &lt; that.accessTime ? 1 : -1;<br \/>\n  }<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<p>\u8fd9\u91cc\u7684\u6bd4\u8f83\u903b\u8f91\u975e\u5e38\u5173\u952e&#xff1a;<\/p>\n<ul>\n<li>\u5b83\u5b8c\u5168\u57fa\u4e8e\u00a0accessTime\u3002<\/li>\n<li>\u5982\u679c\u00a0this.accessTime\u00a0\u5c0f\u4e8e\u00a0that.accessTime&#xff0c;\u610f\u5473\u7740\u00a0this\u00a0\u5757\u6bd4\u00a0that\u00a0\u5757\u66f4\u201c\u8001\u201d&#xff08;\u66f4\u65e9\u88ab\u8bbf\u95ee&#xff09;&#xff0c;\u65b9\u6cd5\u8fd4\u56de\u00a01\u3002<\/li>\n<li>\u5728\u6807\u51c6\u7684\u6392\u5e8f\u7b97\u6cd5\u4e2d&#xff08;\u5982\u00a0PriorityQueue&#xff09;&#xff0c;\u8fd9\u4f1a\u5bfc\u81f4\u00a0accessTime\u00a0\u8f83\u5c0f\u7684&#xff08;\u8001\u7684&#xff09;\u5143\u7d20\u6392\u5728\u540e\u9762&#xff0c;\u800c\u00a0accessTime\u00a0\u8f83\u5927\u7684&#xff08;\u65b0\u7684&#xff09;\u5143\u7d20\u6392\u5728\u524d\u9762\u3002<\/li>\n<li>\u56e0\u6b64&#xff0c;\u5f53\u9700\u8981\u6dd8\u6c70\u65f6&#xff0c;\u4ece\u6392\u5e8f\u96c6\u5408\u7684\u201c\u672b\u5c3e\u201d\u53d6\u51fa\u7684\u5c31\u662f\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u5757\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>equals(Object obj)\u00a0\u548c\u00a0hashCode():<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  &#064;Override<br \/>\n  public int hashCode() {<br \/>\n    return (int) (accessTime ^ (accessTime &gt;&gt;&gt; 32));<br \/>\n  }<\/p>\n<p>  &#064;Override<br \/>\n  public boolean equals(Object obj) {<br \/>\n    if (this &#061;&#061; obj) {<br \/>\n      return true;<br \/>\n    }<br \/>\n    if (obj &#061;&#061; null || getClass() !&#061; obj.getClass()) {<br \/>\n      return false;<br \/>\n    }<br \/>\n    LruCachedBlock other &#061; (LruCachedBlock) obj;<br \/>\n    return compareTo(other) &#061;&#061; 0;<br \/>\n  }<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<p>\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u7684\u5b9e\u73b0\u90fd\u53ea\u4f9d\u8d56\u4e8e\u00a0accessTime\u3002\u8fd9\u610f\u5473\u7740&#xff0c;\u5982\u679c\u4e24\u4e2a\u00a0LruCachedBlock\u00a0\u5b9e\u4f8b\u7684\u00a0accessTime\u00a0\u76f8\u540c&#xff0c;\u5b83\u4eec\u5c31\u88ab\u8ba4\u4e3a\u662f\u00a0equals\u00a0\u7684\u3002\u8fd9\u5728\u901a\u5e38\u7684\u4e1a\u52a1\u5bf9\u8c61\u4e2d\u6bd4\u8f83\u5c11\u89c1&#xff08;\u901a\u5e38\u4f1a\u57fa\u4e8eID\u6216\u5185\u5bb9&#xff09;&#xff0c;\u4f46\u5728\u8fd9\u91cc\u662f\u7279\u610f\u4e3a\u4e4b&#xff0c;\u56e0\u4e3a\u5728\u67d0\u4e9b\u7528\u4e8e\u6392\u5e8f\u7684\u6570\u636e\u7ed3\u6784&#xff08;\u5982\u00a0MinMaxPriorityQueue&#xff09;\u4e2d&#xff0c;\u5143\u7d20\u7684\u76f8\u7b49\u6027\u5224\u65ad\u548c\u6392\u5e8f\u903b\u8f91\u9700\u8981\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>\u603b\u7ed3<\/h3>\n<p>LruCachedBlock\u00a0\u662f\u4e00\u4e2a\u7cbe\u5fc3\u8bbe\u8ba1\u7684\u7c7b&#xff0c;\u5b83\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u6570\u636e\u6301\u6709\u8005&#xff0c;\u66f4\u662f\u00a0LruBlockCache\u00a0\u80fd\u591f\u5b9e\u73b0\u9ad8\u6548\u3001\u5e26\u4f18\u5148\u7ea7\u7684 LRU \u6dd8\u6c70\u7b56\u7565\u7684\u6838\u5fc3\u7ec4\u4ef6\u3002\u5b83\u901a\u8fc7&#xff1a;<\/p>\n<li>\u5b9e\u73b0\u00a0HeapSize\u00a0\u63a5\u53e3&#xff0c;\u8ba9\u7f13\u5b58\u80fd\u591f\u8fdb\u884c\u7cbe\u786e\u7684\u5185\u5b58\u7ba1\u7406\u3002<\/li>\n<li>\u5b9e\u73b0\u00a0Comparable\u00a0\u63a5\u53e3&#xff0c;\u5e76\u6839\u636e\u00a0accessTime\u00a0\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219&#xff0c;\u4ece\u800c\u786e\u7acb\u4e86 LRU \u7684\u6dd8\u6c70\u987a\u5e8f\u3002<\/li>\n<li>\u5185\u90e8\u7ef4\u62a4\u00a0BlockPriority&#xff0c;\u652f\u6301\u4e86\u66f4\u7cbe\u7ec6\u5316\u7684\u7f13\u5b58\u6dd8\u6c70\u7b56\u7565&#xff0c;\u63d0\u9ad8\u4e86\u7f13\u5b58\u547d\u4e2d\u7387\u3002<\/li>\n<li>\u5c01\u88c5\u4e86\u00a0cacheKey\u00a0\u548c\u00a0buf&#xff0c;\u5c06\u7f13\u5b58\u7684\u6807\u8bc6\u548c\u5185\u5bb9\u805a\u5408\u5728\u4e00\u8d77\u3002<\/li>\n<p>\u901a\u8fc7\u5c06\u8fd9\u4e9b\u903b\u8f91\u5185\u805a\u5728\u00a0LruCachedBlock\u00a0\u5185\u90e8&#xff0c;\u4f7f\u5f97\u4e0a\u5c42\u7684\u00a0LruBlockCache\u00a0\u548c\u00a0LruAdaptiveBlockCache\u00a0\u7684\u5b9e\u73b0\u53ef\u4ee5\u66f4\u52a0\u6e05\u6670&#xff0c;\u4e13\u6ce8\u4e8e\u7f13\u5b58\u7684\u5b8f\u89c2\u7ba1\u7406\u548c\u5e76\u53d1\u63a7\u5236&#xff0c;\u800c\u5c06\u5355\u4e2a\u7f13\u5b58\u6761\u76ee\u7684\u72b6\u6001\u548c\u884c\u4e3a\u4ea4\u7531\u00a0LruCachedBlock\u00a0\u81ea\u5df1\u7ba1\u7406\u3002<\/p>\n<h2>LruBlockCache<\/h2>\n<p>LruBlockCache\u00a0\u662f Apache HBase \u4e2d\u9ed8\u8ba4\u7684\u3001\u57fa\u4e8e\u5806\u5185\u5185\u5b58\u7684 Block Cache&#xff08;\u5757\u7f13\u5b58&#xff09;\u5b9e\u73b0\u3002\u5b83\u662f\u4e00\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u7ec4\u4ef6&#xff0c;\u76f4\u63a5\u5f71\u54cd HBase \u7684\u8bfb\u53d6\u6027\u80fd\u3002\u5b83\u7684\u6838\u5fc3\u76ee\u6807\u662f\u5c06\u5728 HDFS \u4e2d\u8bfb\u53d6\u8fc7\u7684\u70ed\u70b9\u6570\u636e\u5757&#xff08;HFile Block&#xff09;\u7f13\u5b58\u5728 RegionServer \u7684 JVM \u5806\u5185\u5b58\u4e2d&#xff0c;\u4ece\u800c\u907f\u514d\u4e0b\u4e00\u6b21\u8bfb\u53d6\u76f8\u540c\u6570\u636e\u65f6\u6602\u8d35\u7684\u78c1\u76d8 I\/O \u64cd\u4f5c\u3002<\/p>\n<h4><\/h4>\n<p>&#064;InterfaceAudience.Private<br \/>\npublic class LruBlockCache implements FirstLevelBlockCache {<br \/>\n\/\/&#8230;<br \/>\n}<\/p>\n<ul>\n<li>implements FirstLevelBlockCache: \u8fd9\u4e2a\u63a5\u53e3\u5b9a\u4e49\u4e86\u5176\u4f5c\u4e3a\u201c\u4e00\u7ea7\u7f13\u5b58\u201d&#xff08;L1 Cache&#xff09;\u7684\u89d2\u8272\u3002\u5728 HBase \u7684\u7f13\u5b58\u4f53\u7cfb\u4e2d&#xff0c;\u53ef\u4ee5\u5b58\u5728\u591a\u7ea7\u7f13\u5b58\u3002LruBlockCache\u00a0\u901a\u5e38\u4f5c\u4e3a L1 \u7f13\u5b58&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u4e0e\u6027\u80fd\u66f4\u9ad8\u7684 L2 \u7f13\u5b58&#xff08;\u5982\u57fa\u4e8e\u5806\u5916\u5185\u5b58\u7684\u00a0BucketCache&#xff09;\u534f\u540c\u5de5\u4f5c\u3002\n<ul>\n<li>FirstLevelBlockCache\u00a0\u63a5\u53e3\u7ee7\u627f\u4e86\u00a0ResizableBlockCache\u00a0\u548c\u00a0HeapSize&#xff0c;\u610f\u5473\u7740\u00a0LruBlockCache\u00a0\u7684\u5927\u5c0f\u662f\u53ef\u8c03\u6574\u7684&#xff0c;\u5e76\u4e14\u80fd\u591f\u62a5\u544a\u81ea\u8eab\u5360\u7528\u7684\u5806\u5185\u5b58\u5927\u5c0f\u3002<\/li>\n<li>\u5b83\u8fd8\u5b9a\u4e49\u4e86\u00a0setVictimCache(BlockCache victimCache)\u00a0\u65b9\u6cd5\u3002\u5f53\u4e00\u4e2a\u5757\u56e0\u4e3a\u7a7a\u95f4\u4e0d\u8db3\u800c\u4ece L1 \u7f13\u5b58\u4e2d\u88ab\u201c\u6dd8\u6c70\u201d&#xff08;evict&#xff09;\u65f6&#xff0c;\u5b83\u53ef\u4ee5\u88ab\u4f20\u9012\u7ed9\u8fd9\u4e2a &#034;victim cache&#034;&#xff08;\u901a\u5e38\u662f L2 \u7f13\u5b58&#xff09;&#xff0c;\u4ece\u800c\u5b9e\u73b0\u5206\u5c42\u7f13\u5b58\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4><\/h4>\n<p>LruBlockCache\u00a0\u7684\u8bbe\u8ba1\u7cbe\u9ad3\u5728\u4e8e\u5b83\u5e76\u975e\u4e00\u4e2a\u7b80\u5355\u7684 LRU \u7f13\u5b58&#xff0c;\u800c\u662f\u878d\u5408\u4e86\u5e76\u53d1\u63a7\u5236\u3001\u4f18\u5148\u7ea7\u548c\u7cbe\u7ec6\u5316\u5185\u5b58\u7ba1\u7406\u7684\u590d\u6742\u7cfb\u7edf\u3002<\/p>\n<h3>\u5e76\u53d1\u6027 (Concurrency)<\/h3>\n<p>\u7f13\u5b58\u7cfb\u7edf\u5fc5\u987b\u652f\u6301\u9ad8\u5e76\u53d1\u7684\u8bfb\u5199\u3002LruBlockCache\u00a0\u4f7f\u7528\u00a0java.util.concurrent.ConcurrentHashMap\u00a0\u4f5c\u4e3a\u5176\u5e95\u5c42\u5b58\u50a8\u7ed3\u6784\u3002<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  \/**<br \/>\n   * Defined the cache map as {&#064;link ConcurrentHashMap} here, because in<br \/>\n   * {&#064;link LruBlockCache#getBlock}, we need to guarantee the atomicity of map#k (key, func).<br \/>\n   * Besides, the func method must execute exactly once only when the key is present and under the<br \/>\n   * lock context, otherwise the reference count will be messed up. Notice that the<br \/>\n   * {&#064;link java.util.concurrent.ConcurrentSkipListMap} can not guarantee that. Some code using<br \/>\n   * #computeIfPresent also expects the supplier to be executed only once. ConcurrentHashMap can<br \/>\n   * guarantee that. Other types may not.<br \/>\n   *\/<br \/>\n  private transient final ConcurrentHashMap&lt;BlockCacheKey, LruCachedBlock&gt; map;<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<p>\u9009\u62e9\u00a0ConcurrentHashMap\u00a0\u7684\u5173\u952e\u5728\u4e8e\u5176\u00a0computeIfPresent\u00a0\u7b49\u539f\u5b50\u64cd\u4f5c\u65b9\u6cd5&#xff0c;\u8fd9\u80fd\u4fdd\u8bc1\u5728\u201c\u83b7\u53d6\u5e76\u66f4\u65b0\u201d\u4e00\u4e2a\u7f13\u5b58\u5757&#xff08;\u4f8b\u5982\u66f4\u65b0\u5176\u8bbf\u95ee\u65f6\u95f4&#xff09;\u8fd9\u4e2a\u590d\u5408\u64cd\u4f5c\u7684\u539f\u5b50\u6027&#xff0c;\u907f\u514d\u4e86\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u4f7f\u7528\u663e\u5f0f\u9501\u5e26\u6765\u7684\u6027\u80fd\u5f00\u9500\u548c\u590d\u6742\u6027\u3002<\/p>\n<h3>\u5e26\u6709\u4f18\u5148\u7ea7\u7684 LRU \u6dd8\u6c70\u7b56\u7565<\/h3>\n<p>\u7eaf\u7cb9\u7684 LRU \u7b97\u6cd5\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u8868\u73b0\u4e0d\u4f73&#xff0c;\u4f8b\u5982&#xff0c;\u4e00\u6b21\u5927\u7684\u626b\u63cf\u64cd\u4f5c&#xff08;Scan&#xff09;\u53ef\u80fd\u4f1a\u8bfb\u5165\u5927\u91cf\u4f4e\u4ef7\u503c\u3001\u53ea\u8bbf\u95ee\u4e00\u6b21\u7684\u6570\u636e\u5757&#xff0c;\u4ece\u800c\u5c06\u771f\u6b63\u9700\u8981\u9891\u7e41\u8bbf\u95ee\u7684\u70ed\u70b9\u6570\u636e\u5757\u201c\u51b2\u5237\u201d\u51fa\u7f13\u5b58\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898&#xff0c;LruBlockCache\u00a0\u5f15\u5165\u4e86\u4e09\u7ea7\u4f18\u5148\u7ea7&#xff1a;<\/p>\n<li>SINGLE (\u5355\u6b21\u8bbf\u95ee): \u65b0\u52a0\u5165\u7684\u3001\u975ein-memory\u7684\u5757\u9ed8\u8ba4\u4e3a\u6b64\u4f18\u5148\u7ea7\u3002<\/li>\n<li>MULTI (\u591a\u6b21\u8bbf\u95ee): \u5f53\u4e00\u4e2a\u00a0SINGLE\u00a0\u4f18\u5148\u7ea7\u7684\u5757\u88ab\u518d\u6b21\u8bbf\u95ee\u65f6&#xff0c;\u5176\u4f18\u5148\u7ea7\u4f1a\u63d0\u5347\u4e3a\u00a0MULTI\u3002\u8fd9\u4f7f\u5f97\u88ab\u591a\u6b21\u8bbf\u95ee\u7684\u5757\u66f4\u4e0d\u5bb9\u6613\u88ab\u6dd8\u6c70\u3002<\/li>\n<li>MEMORY (\u5185\u5b58\u4e2d): \u7528\u4e8e\u914d\u7f6e\u4e3a\u00a0in-memory\u00a0\u7684\u5217\u65cf\u7684\u6570\u636e\u5757\u3002\u8fd9\u4e9b\u5757\u62e5\u6709\u6700\u9ad8\u7684\u4f18\u5148\u7ea7&#xff0c;\u6700\u96be\u88ab\u6dd8\u6c70\u3002<\/li>\n<p>\u8fd9\u4e09\u4e2a\u4f18\u5148\u7ea7\u5404\u81ea\u88ab\u5206\u914d\u4e86\u4e00\u5b9a\u6bd4\u4f8b\u7684\u7f13\u5b58\u7a7a\u95f4&#xff08;\u7531\u914d\u7f6e\u53c2\u6570\u00a0singleFactor,\u00a0multiFactor,\u00a0memoryFactor\u00a0\u51b3\u5b9a&#xff09;&#xff0c;\u6dd8\u6c70\u7b97\u6cd5\u4f1a\u6839\u636e\u6bcf\u4e2a\u4f18\u5148\u7ea7\u5206\u533a\u7684\u4f7f\u7528\u60c5\u51b5\u6765\u516c\u5e73\u5730\u8fdb\u884c\u6dd8\u6c70\u3002<\/p>\n<h3>\u5f02\u6b65\u6dd8\u6c70\u673a\u5236 (Eviction)<\/h3>\n<p>\u4e3a\u4e86\u4fdd\u8bc1\u00a0cacheBlock\u00a0\u548c\u00a0getBlock\u00a0\u65b9\u6cd5\u7684\u4f4e\u5ef6\u8fdf&#xff0c;\u6dd8\u6c70\u64cd\u4f5c\u5728\u4e00\u4e2a\u72ec\u7acb\u7684\u540e\u53f0\u7ebf\u7a0b (EvictionThread) \u4e2d\u6267\u884c\u3002<\/p>\n<ul>\n<li>\u89e6\u53d1\u65f6\u673a: \u5f53\u7f13\u5b58\u7684\u5f53\u524d\u5927\u5c0f\u00a0size\u00a0\u8d85\u8fc7\u4e86\u00a0maxSize * acceptableFactor&#xff08;\u9ed8\u8ba4 0.99&#xff09;\u65f6&#xff0c;\u4f1a\u89e6\u53d1\u6dd8\u6c70\u3002<\/li>\n<li>\u6dd8\u6c70\u76ee\u6807: \u6dd8\u6c70\u7ebf\u7a0b\u4f1a\u6301\u7eed\u5de5\u4f5c&#xff0c;\u76f4\u5230\u7f13\u5b58\u5927\u5c0f\u964d\u4f4e\u5230\u00a0maxSize * minFactor&#xff08;\u9ed8\u8ba4 0.95&#xff09;\u4ee5\u4e0b\u3002<\/li>\n<li>\u786c\u9650\u5236: \u8fd8\u6709\u4e00\u4e2a\u00a0hardCapacityLimitFactor&#xff08;\u9ed8\u8ba4 1.2&#xff09;&#xff0c;\u5f53\u7f13\u5b58\u5927\u5c0f\u8d85\u8fc7\u00a0maxSize * acceptableFactor * hardCapacityLimitFactor\u00a0\u65f6&#xff0c;\u65b0\u7684\u7f13\u5b58\u8bf7\u6c42\u4f1a\u88ab\u76f4\u63a5\u62d2\u7edd&#xff0c;\u9632\u6b62\u7f13\u5b58\u65e0\u9650\u81a8\u80c0\u3002<\/li>\n<\/ul>\n<h3>\u5173\u952e\u6210\u5458\u53d8\u91cf<\/h3>\n<p>LruBlockCache\u00a0\u5185\u90e8\u6709\u5927\u91cf\u7684\u6210\u5458\u53d8\u91cf\u6765\u7ef4\u62a4\u5176\u72b6\u6001\u548c\u914d\u7f6e\u3002<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  \/** Cache access count (sequential ID) *\/<br \/>\n  private final AtomicLong count;<\/p>\n<p>  \/** hard capacity limit *\/<br \/>\n  private float hardCapacityLimitFactor;<\/p>\n<p>  \/** Cache statistics *\/<br \/>\n  private final CacheStats stats;<\/p>\n<p>  \/** Maximum allowable size of cache (block put if size &gt; max, evict) *\/<br \/>\n  private long maxSize;<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  \/** Acceptable size of cache (no evictions if size &lt; acceptable) *\/<br \/>\n  private float acceptableFactor;<\/p>\n<p>  \/** Minimum threshold of cache (when evicting, evict until size &lt; min) *\/<br \/>\n  private float minFactor;<\/p>\n<p>  \/** Single access bucket size *\/<br \/>\n  private float singleFactor;<\/p>\n<p>  \/** Multiple access bucket size *\/<br \/>\n  private float multiFactor;<\/p>\n<p>  \/** In-memory bucket size *\/<br \/>\n  private float memoryFactor;<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  \/**<br \/>\n   * Where to send victims (blocks evicted\/missing from the cache). This is used only when we use an<br \/>\n   * external cache as L2. Note: See org.apache.hadoop.hbase.io.hfile.MemcachedBlockCache<br \/>\n   *\/<br \/>\n  private transient BlockCache victimHandler &#061; null;<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<ul>\n<li>map: \u6838\u5fc3\u5b58\u50a8\u3002<\/li>\n<li>size,\u00a0elements,\u00a0dataBlockSize,\u00a0indexBlockSize\u00a0\u7b49: \u4f7f\u7528\u00a0AtomicLong\u00a0\u548c\u00a0LongAdder\u00a0\u6765\u9ad8\u6548\u3001\u7ebf\u7a0b\u5b89\u5168\u5730\u8bb0\u5f55\u7f13\u5b58\u7684\u5404\u79cd\u7edf\u8ba1\u6307\u6807\u3002<\/li>\n<li>count: \u4e00\u4e2a\u539f\u5b50\u9012\u589e\u7684\u8ba1\u6570\u5668&#xff0c;\u7528\u4e8e\u4e3a\u6bcf\u4e2a\u00a0LruCachedBlock\u00a0\u751f\u6210\u552f\u4e00\u7684\u00a0accessTime\u3002<\/li>\n<li>maxSize,\u00a0acceptableFactor,\u00a0minFactor,\u00a0singleFactor\u00a0\u7b49: \u63a7\u5236\u7f13\u5b58\u884c\u4e3a\u548c\u6dd8\u6c70\u7b56\u7565\u7684\u5404\u79cd\u914d\u7f6e\u56e0\u5b50\u3002<\/li>\n<li>evictionInProgress: \u4e00\u4e2a\u00a0volatile\u00a0\u6807\u5fd7&#xff0c;\u7528\u4e8e\u8868\u793a\u6dd8\u6c70\u662f\u5426\u6b63\u5728\u8fdb\u884c\u3002<\/li>\n<li>evictionLock: \u4e00\u4e2a\u00a0ReentrantLock&#xff0c;\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u6dd8\u6c70\u4efb\u52a1\u5728\u6267\u884c\u3002<\/li>\n<li>victimHandler: \u6307\u5411 L2 \u7f13\u5b58\u7684\u5f15\u7528\u3002<\/li>\n<\/ul>\n<h3>cacheBlock(&#8230;)\u00a0&#8211; \u7f13\u5b58\u5757<\/h3>\n<p>\u8fd9\u662f\u5411\u7f13\u5b58\u4e2d\u6dfb\u52a0\u65b0\u5757\u7684\u5165\u53e3\u3002<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  &#064;Override<br \/>\n  public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory) {<br \/>\n    if (buf.heapSize() &gt; maxBlockSize) {<br \/>\n\/\/ &#8230; existing code &#8230;<br \/>\n      return;<br \/>\n    }<\/p>\n<p>    LruCachedBlock cb &#061; map.get(cacheKey);<br \/>\n    if (cb !&#061; null &amp;&amp; !BlockCacheUtil.shouldReplaceExistingCacheBlock(this, cacheKey, buf)) {<br \/>\n      return;<br \/>\n    }<br \/>\n    long currentSize &#061; size.get();<br \/>\n    long currentAcceptableSize &#061; acceptableSize();<br \/>\n    long hardLimitSize &#061; (long) (hardCapacityLimitFactor * currentAcceptableSize);<br \/>\n    if (currentSize &gt;&#061; hardLimitSize) {<br \/>\n      stats.failInsert();<br \/>\n\/\/ &#8230; existing code &#8230;<br \/>\n      if (!evictionInProgress) {<br \/>\n        runEviction();<br \/>\n      }<br \/>\n      return;<br \/>\n    }<br \/>\n    \/\/ Ensure that the block is an heap one.<br \/>\n    buf &#061; asReferencedHeapBlock(buf);<br \/>\n    cb &#061; new LruCachedBlock(cacheKey, buf, count.incrementAndGet(), inMemory);<br \/>\n    long newSize &#061; updateSizeMetrics(cb, false);<br \/>\n    map.put(cacheKey, cb);<br \/>\n\/\/ &#8230; existing code &#8230;<br \/>\n    if (newSize &gt; currentAcceptableSize &amp;&amp; !evictionInProgress) {<br \/>\n      runEviction();<br \/>\n    }<br \/>\n  }<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<p>\u5176\u6267\u884c\u6d41\u7a0b\u5982\u4e0b&#xff1a;<\/p>\n<li>\u5927\u5c0f\u68c0\u67e5: \u68c0\u67e5\u5757\u5927\u5c0f\u662f\u5426\u8d85\u8fc7\u00a0maxBlockSize\u3002<\/li>\n<li>\u5b58\u5728\u6027\u68c0\u67e5: \u68c0\u67e5\u5757\u662f\u5426\u5df2\u5b58\u5728&#xff0c;\u4ee5\u53ca\u662f\u5426\u9700\u8981\u66ff\u6362\u3002<\/li>\n<li>\u786c\u9650\u5236\u68c0\u67e5: \u68c0\u67e5\u5f53\u524d\u603b\u5927\u5c0f\u662f\u5426\u5df2\u8fbe\u5230\u786c\u9650\u5236\u3002\u5982\u679c\u8fbe\u5230&#xff0c;\u5219\u62d2\u7edd\u7f13\u5b58\u5e76\u5c1d\u8bd5\u89e6\u53d1\u6dd8\u6c70\u3002<\/li>\n<li>\u5185\u5b58\u7c7b\u578b\u8f6c\u6362: \u8c03\u7528\u00a0asReferencedHeapBlock\u00a0\u786e\u4fdd\u5b58\u5165\u7684\u5757\u662f\u5806\u5185\u5757&#xff0c;\u5e76\u6b63\u786e\u7ba1\u7406\u5176\u5f15\u7528\u8ba1\u6570\u3002<\/li>\n<li>\u521b\u5efa\u7f13\u5b58\u9879: \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u00a0LruCachedBlock\u00a0\u5b9e\u4f8b&#xff0c;\u4f20\u5165 key\u3001\u6570\u636e\u3001\u4ee5\u53ca\u901a\u8fc7\u00a0count.incrementAndGet()\u00a0\u83b7\u53d6\u7684\u6700\u65b0\u8bbf\u95ee\u65f6\u95f4\u3002<\/li>\n<li>\u653e\u5165\u7f13\u5b58: \u5c06\u65b0\u7684\u00a0LruCachedBlock\u00a0\u653e\u5165\u00a0map\u00a0\u4e2d\u3002<\/li>\n<li>\u66f4\u65b0\u7edf\u8ba1: \u66f4\u65b0\u7f13\u5b58\u7684\u603b\u5927\u5c0f\u548c\u5143\u7d20\u6570\u91cf\u7b49\u7edf\u8ba1\u4fe1\u606f\u3002<\/li>\n<li>\u89e6\u53d1\u6dd8\u6c70: \u68c0\u67e5\u66f4\u65b0\u540e\u7684\u5927\u5c0f\u662f\u5426\u8d85\u8fc7\u4e86\u00a0acceptableSize&#xff0c;\u5982\u679c\u662f&#xff0c;\u5219\u542f\u52a8\u6dd8\u6c70\u7ebf\u7a0b\u3002<\/li>\n<h3>getBlock(&#8230;)\u00a0&#8211; \u83b7\u53d6\u5757<\/h3>\n<p>\u8fd9\u662f\u4ece\u7f13\u5b58\u4e2d\u8bfb\u53d6\u5757\u7684\u5165\u53e3\u3002\u5b83\u5229\u7528\u4e86\u00a0map.computeIfPresent\u00a0\u6765\u539f\u5b50\u5730\u5b8c\u6210\u201c\u83b7\u53d6\u5e76\u66f4\u65b0\u201d\u64cd\u4f5c&#xff0c;\u4fdd\u8bc1\u4e86\u7ebf\u7a0b\u5b89\u5168\u548c\u9ad8\u6027\u80fd\u3002\u5982\u679c\u547d\u4e2d&#xff0c;\u5b83\u4f1a\u66f4\u65b0\u5757\u7684\u00a0accessTime\u00a0\u548c\u4f18\u5148\u7ea7&#xff0c;\u8fd9\u662f LRU \u903b\u8f91\u7684\u6838\u5fc3\u3002<\/p>\n<h3>evict()\u00a0&#8211; \u6dd8\u6c70\u903b\u8f91<\/h3>\n<p>evict()\u00a0\u65b9\u6cd5\u662f\u00a0LruBlockCache\u00a0\u4e2d\u8d1f\u8d23\u6267\u884c\u7f13\u5b58\u6dd8\u6c70\u7684\u6838\u5fc3\u51fd\u6570\u3002\u5b83\u7684\u4e3b\u8981\u76ee\u6807\u662f\u5f53\u7f13\u5b58\u5927\u5c0f\u8d85\u8fc7\u9884\u8bbe\u7684\u9608\u503c\u65f6&#xff0c;\u901a\u8fc7\u79fb\u9664\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u5757&#xff08;LRU&#xff09;&#xff0c;\u5c06\u7f13\u5b58\u5927\u5c0f\u964d\u4f4e\u5230\u4e00\u4e2a\u53ef\u63a5\u53d7\u7684\u6c34\u5e73\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u5f02\u6b65\u7684&#xff0c;\u901a\u5e38\u7531\u4e00\u4e2a\u4e13\u95e8\u7684\u00a0EvictionThread\u00a0\u7ebf\u7a0b\u6765\u8c03\u7528&#xff0c;\u4ee5\u907f\u514d\u963b\u585e\u6b63\u5e38\u7684\u7f13\u5b58\u8bfb\u5199\u64cd\u4f5c\u3002<\/p>\n<li>\u626b\u63cf\u4e0e\u5206\u6876: \u904d\u5386\u00a0map\u00a0\u4e2d\u7684\u6240\u6709\u00a0LruCachedBlock&#xff0c;\u6839\u636e\u5176\u00a0getPriority()\u00a0\u7684\u7ed3\u679c&#xff0c;\u5c06\u5b83\u4eec\u653e\u5165\u4e09\u4e2a\u4e0d\u540c\u7684\u00a0BlockBucket&#xff08;single,\u00a0multi,\u00a0memory&#xff09;\u4e2d\u3002<\/li>\n<li>\u516c\u5e73\u6dd8\u6c70:\u00a0BlockBucket\u00a0\u5185\u90e8\u4f7f\u7528\u00a0LruCachedBlockQueue&#xff08;\u57fa\u4e8e\u00a0MinMaxPriorityQueue&#xff09;\u6765\u7ef4\u62a4\u5757\u7684 LRU \u987a\u5e8f\u3002\u6dd8\u6c70\u7b97\u6cd5\u4f1a\u8ba1\u7b97\u6bcf\u4e2a\u6876\u7684\u201c\u6ea2\u51fa\u201d\u5927\u5c0f&#xff08;\u5373\u8d85\u51fa\u5176\u5e94\u5360\u6bd4\u4f8b\u7684\u5927\u5c0f&#xff09;&#xff0c;\u7136\u540e\u6309\u6bd4\u4f8b\u4ece\u6ea2\u51fa\u7684\u6876\u4e2d\u6dd8\u6c70\u6389\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u5757&#xff0c;\u76f4\u5230\u91ca\u653e\u8db3\u591f\u7684\u7a7a\u95f4\u3002<\/li>\n<li>\u5904\u7406\u6dd8\u6c70\u5757: \u88ab\u6dd8\u6c70\u7684\u5757\u4f1a\u4ece\u00a0map\u00a0\u4e2d\u79fb\u9664&#xff0c;\u5176\u5360\u7528\u7684\u5185\u5b58\u4f1a\u4ece\u603b\u5927\u5c0f\u4e2d\u51cf\u53bb\u3002\u5982\u679c\u914d\u7f6e\u4e86\u00a0victimHandler&#xff0c;\u88ab\u6dd8\u6c70\u7684\u5757\u4f1a\u88ab\u4f20\u9012\u7ed9 L2 \u7f13\u5b58\u3002<\/li>\n<p>\u6211\u4eec\u6765\u9010\u6bb5\u5206\u6790\u5b83\u7684\u5b9e\u73b0&#xff1a;<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  void evict() {<\/p>\n<p>    \/\/ Ensure only one eviction at a time<br \/>\n    if (!evictionLock.tryLock()) {<br \/>\n      return;<br \/>\n    }<\/p>\n<p>    try {<br \/>\n      evictionInProgress &#061; true;<br \/>\n      long currentSize &#061; this.size.get();<br \/>\n      long bytesToFree &#061; currentSize &#8211; minSize();<\/p>\n<p>      if (LOG.isTraceEnabled()) {<br \/>\n\/\/ &#8230; existing code &#8230;<br \/>\n      }<\/p>\n<p>      if (bytesToFree &lt;&#061; 0) {<br \/>\n        return;<br \/>\n      }<\/p>\n<p>      \/\/ Instantiate priority buckets<br \/>\n      BlockBucket bucketSingle &#061; new BlockBucket(&#034;single&#034;, bytesToFree, blockSize, singleSize());<br \/>\n      BlockBucket bucketMulti &#061; new BlockBucket(&#034;multi&#034;, bytesToFree, blockSize, multiSize());<br \/>\n      BlockBucket bucketMemory &#061; new BlockBucket(&#034;memory&#034;, bytesToFree, blockSize, memorySize());<\/p>\n<p>      \/\/ Scan entire map putting into appropriate buckets<br \/>\n      for (LruCachedBlock cachedBlock : map.values()) {<br \/>\n        switch (cachedBlock.getPriority()) {<br \/>\n          case SINGLE: {<br \/>\n            bucketSingle.add(cachedBlock);<br \/>\n            break;<br \/>\n          }<br \/>\n          case MULTI: {<br \/>\n            bucketMulti.add(cachedBlock);<br \/>\n            break;<br \/>\n          }<br \/>\n          case MEMORY: {<br \/>\n            bucketMemory.add(cachedBlock);<br \/>\n            break;<br \/>\n          }<br \/>\n        }<br \/>\n      }<\/p>\n<p>      long bytesFreed &#061; 0;<br \/>\n      if (forceInMemory || memoryFactor &gt; 0.999f) {<br \/>\n\/\/ &#8230; (special eviction logic) &#8230;<br \/>\n      } else {<br \/>\n        PriorityQueue&lt;BlockBucket&gt; bucketQueue &#061; new PriorityQueue&lt;&gt;(3);<\/p>\n<p>        bucketQueue.add(bucketSingle);<br \/>\n        bucketQueue.add(bucketMulti);<br \/>\n        bucketQueue.add(bucketMemory);<\/p>\n<p>        int remainingBuckets &#061; bucketQueue.size();<\/p>\n<p>        BlockBucket bucket;<br \/>\n        while ((bucket &#061; bucketQueue.poll()) !&#061; null) {<br \/>\n          long overflow &#061; bucket.overflow();<br \/>\n          if (overflow &gt; 0) {<br \/>\n            long bucketBytesToFree &#061;<br \/>\n              Math.min(overflow, (bytesToFree &#8211; bytesFreed) \/ remainingBuckets);<br \/>\n            bytesFreed &#043;&#061; bucket.free(bucketBytesToFree);<br \/>\n          }<br \/>\n          remainingBuckets&#8211;;<br \/>\n        }<br \/>\n      }<br \/>\n\/\/ &#8230; (logging) &#8230;<br \/>\n    } finally {<br \/>\n      stats.evict();<br \/>\n      evictionInProgress &#061; false;<br \/>\n      evictionLock.unlock();<br \/>\n    }<br \/>\n  }<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<h4>\u52a0\u9501\u4e0e\u72b6\u6001\u8bbe\u7f6e<\/h4>\n<p>\/\/ Ensure only one eviction at a time<br \/>\nif (!evictionLock.tryLock()) {<br \/>\n  return;<br \/>\n}<\/p>\n<p>try {<br \/>\n  evictionInProgress &#061; true;<br \/>\n  \/\/ &#8230;<br \/>\n} finally {<br \/>\n  \/\/ &#8230;<br \/>\n  evictionInProgress &#061; false;<br \/>\n  evictionLock.unlock();<br \/>\n}<\/p>\n<ul>\n<li>evictionLock.tryLock(): \u8fd9\u662f\u6574\u4e2a\u6dd8\u6c70\u8fc7\u7a0b\u7684\u5165\u53e3\u4fdd\u62a4\u3002evictionLock\u00a0\u662f\u4e00\u4e2a\u00a0ReentrantLock\u3002\u4f7f\u7528\u00a0tryLock()\u00a0\u800c\u4e0d\u662f\u00a0lock()\u00a0\u662f\u4e00\u4e2a\u975e\u963b\u585e\u7684\u5c1d\u8bd5\u3002\u5982\u679c\u9501\u5df2\u7ecf\u88ab\u5176\u4ed6\u7ebf\u7a0b&#xff08;\u6216\u5f53\u524d\u7ebf\u7a0b\u7684\u53e6\u4e00\u6b21\u8c03\u7528&#xff09;\u6301\u6709&#xff0c;tryLock()\u00a0\u4f1a\u7acb\u5373\u8fd4\u56de\u00a0false&#xff0c;\u65b9\u6cd5\u76f4\u63a5\u9000\u51fa\u3002\u8fd9\u786e\u4fdd\u4e86\u5728\u4efb\u4f55\u65f6\u523b&#xff0c;\u6700\u591a\u53ea\u6709\u4e00\u4e2a\u6dd8\u6c70\u8fc7\u7a0b\u5728\u6267\u884c&#xff0c;\u907f\u514d\u4e86\u5e76\u53d1\u6dd8\u6c70\u5e26\u6765\u7684\u7ade\u6001\u6761\u4ef6\u548c\u4e0d\u5fc5\u8981\u7684\u5f00\u9500\u3002<\/li>\n<li>evictionInProgress &#061; true: \u8fd9\u662f\u4e00\u4e2a\u00a0volatile\u00a0\u6807\u5fd7\u4f4d&#xff0c;\u7528\u4e8e\u5411\u5176\u4ed6\u7ebf\u7a0b&#xff08;\u5982\u00a0cacheBlock\u00a0\u65b9\u6cd5&#xff09;\u8868\u660e\u6dd8\u6c70\u6b63\u5728\u8fdb\u884c\u4e2d\u3002\u8fd9\u53ef\u4ee5\u7528\u6765\u9632\u6b62\u5728\u6dd8\u6c70\u671f\u95f4\u53d1\u751f\u67d0\u4e9b\u64cd\u4f5c&#xff0c;\u6216\u8005\u4f5c\u4e3a\u89e6\u53d1\u5176\u4ed6\u903b\u8f91\u7684\u4fe1\u53f7\u3002<\/li>\n<li>try&#8230;finally: \u8fd9\u662f\u4e00\u4e2a\u6807\u51c6\u7684\u3001\u5065\u58ee\u7684\u9501\u7ba1\u7406\u6a21\u5f0f\u3002\u65e0\u8bba\u6dd8\u6c70\u8fc7\u7a0b\u662f\u5426\u6210\u529f\u6216\u629b\u51fa\u5f02\u5e38&#xff0c;finally\u00a0\u5757\u4e2d\u7684\u4ee3\u7801\u90fd\u4f1a\u88ab\u6267\u884c&#xff0c;\u4fdd\u8bc1\u00a0evictionInProgress\u00a0\u6807\u5fd7\u88ab\u91cd\u7f6e&#xff0c;\u5e76\u4e14\u9501\u4e00\u5b9a\u4f1a\u88ab\u91ca\u653e&#xff0c;\u9632\u6b62\u6b7b\u9501\u3002<\/li>\n<\/ul>\n<p>\u8ba1\u7b97\u9700\u8981\u91ca\u653e\u7684\u7a7a\u95f4<\/p>\n<p>long currentSize &#061; this.size.get();<br \/>\nlong bytesToFree &#061; currentSize &#8211; minSize();<\/p>\n<p>if (bytesToFree &lt;&#061; 0) {<br \/>\n  return;<br \/>\n}<\/p>\n<ul>\n<li>currentSize\u00a0\u662f\u7f13\u5b58\u5f53\u524d\u7684\u603b\u5927\u5c0f\u3002<\/li>\n<li>minSize()\u00a0\u8ba1\u7b97\u7684\u662f\u6dd8\u6c70\u7684\u76ee\u6807\u5927\u5c0f&#xff0c;\u901a\u5e38\u662f\u00a0maxSize * minFactor\u00a0(\u9ed8\u8ba4 0.95)\u3002<\/li>\n<li>bytesToFree\u00a0\u5c31\u662f\u9700\u8981\u4ece\u7f13\u5b58\u4e2d\u79fb\u9664\u7684\u5757\u7684\u603b\u5927\u5c0f\u3002\u5982\u679c\u8fd9\u4e2a\u503c\u5c0f\u4e8e\u7b49\u4e8e0&#xff0c;\u8bf4\u660e\u5f53\u524d\u7f13\u5b58\u5927\u5c0f\u5df2\u7ecf\u5728\u5065\u5eb7\u8303\u56f4\u5185&#xff0c;\u65e0\u9700\u6dd8\u6c70&#xff0c;\u65b9\u6cd5\u76f4\u63a5\u8fd4\u56de\u3002<\/li>\n<\/ul>\n<h4>\u5206\u6876 (Bucketing)<\/h4>\n<p>\u8fd9\u662f\u5b9e\u73b0\u5e26\u4f18\u5148\u7ea7\u6dd8\u6c70\u7b56\u7565\u7684\u6838\u5fc3\u6b65\u9aa4\u3002<\/p>\n<p>\u6ce8\u610f\u8fd9\u91cc\u7684\u6784\u9020&#xff0c;\u6700\u5c0f\u6700\u5927\u5806\u7684 \u5bb9\u91cf \u5c31\u662f\u00a0bytesToFree&#xff0c;\u5373\u8981\u6dd8\u6c70\u7684\u6570\u91cf&#xff0c;\u662f\u4e00\u4e2atop K\u7528\u6cd5\u3002<\/p>\n<p>\/\/ Instantiate priority buckets<br \/>\nBlockBucket bucketSingle &#061; new BlockBucket(&#034;single&#034;, bytesToFree, blockSize, singleSize());<br \/>\nBlockBucket bucketMulti &#061; new BlockBucket(&#034;multi&#034;, bytesToFree, blockSize, multiSize());<br \/>\nBlockBucket bucketMemory &#061; new BlockBucket(&#034;memory&#034;, bytesToFree, blockSize, memorySize());<\/p>\n<p>\/\/ Scan entire map putting into appropriate buckets<br \/>\nfor (LruCachedBlock cachedBlock : map.values()) {<br \/>\n  switch (cachedBlock.getPriority()) {<br \/>\n    case SINGLE: {<br \/>\n      bucketSingle.add(cachedBlock);<br \/>\n      break;<br \/>\n    }<br \/>\n    \/\/ &#8230; other cases<br \/>\n  }<br \/>\n}<\/p>\n<ul>\n<li>\u521b\u5efa\u00a0BlockBucket: \u4ee3\u7801\u521b\u5efa\u4e86\u4e09\u4e2a\u00a0BlockBucket\u00a0\u5b9e\u4f8b&#xff0c;\u5206\u522b\u5bf9\u5e94\u00a0SINGLE\u3001MULTI\u00a0\u548c\u00a0MEMORY\u00a0\u4e09\u79cd\u4f18\u5148\u7ea7\u3002\u6bcf\u4e2a\u00a0BlockBucket\u00a0\u5728\u521d\u59cb\u5316\u65f6\u88ab\u544a\u77e5\u4e86\u5b83\u7684\u76ee\u6807\u5927\u5c0f&#xff08;\u5982\u00a0singleSize()&#xff09;\u3002<\/li>\n<li>\u904d\u5386\u548c\u5206\u7c7b: \u63a5\u7740&#xff0c;\u4ee3\u7801\u4f1a\u904d\u5386\u6574\u4e2a\u00a0ConcurrentHashMap\u00a0(map.values())\u3002\u8fd9\u662f\u4e00\u4e2a\u6602\u8d35\u7684\u64cd\u4f5c&#xff0c;\u4e5f\u662f\u4e3a\u4ec0\u4e48\u6dd8\u6c70\u9700\u8981\u5f02\u6b65\u6267\u884c\u7684\u4e3b\u8981\u539f\u56e0\u3002\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u00a0LruCachedBlock&#xff0c;\u5b83\u4f1a\u68c0\u67e5\u5176\u4f18\u5148\u7ea7&#xff0c;\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u76f8\u5e94\u7684\u00a0BlockBucket\u00a0\u4e2d\u3002<\/li>\n<\/ul>\n<p>BlockBucket.add(LruCachedBlock block)<\/p>\n<p>\/\/ in class BlockBucket<br \/>\npublic void add(LruCachedBlock block) {<br \/>\n  totalSize &#043;&#061; block.heapSize();<br \/>\n  queue.add(block);<br \/>\n}<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u5f88\u7b80\u5355&#xff0c;\u5b83\u7d2f\u52a0\u6876\u7684\u603b\u5927\u5c0f&#xff0c;\u5e76\u5c06\u00a0LruCachedBlock\u00a0\u6dfb\u52a0\u5230\u5185\u90e8\u7684\u00a0LruCachedBlockQueue\u00a0\u4e2d\u3002LruCachedBlockQueue\u00a0\u5185\u90e8\u662f\u4e00\u4e2a\u00a0MinMaxPriorityQueue&#xff0c;\u5b83\u4f1a\u6839\u636e\u00a0LruCachedBlock\u00a0\u7684\u00a0compareTo\u00a0\u65b9\u6cd5&#xff08;\u5373\u6bd4\u8f83\u00a0accessTime&#xff09;\u6765\u81ea\u52a8\u6392\u5e8f&#xff0c;\u786e\u4fdd\u8bbf\u95ee\u65f6\u95f4\u6700\u8001\u548c\u6700\u65b0\u7684\u5757\u603b\u662f\u5728\u961f\u5217\u7684\u4e24\u7aef&#xff0c;\u53ef\u4ee5\u88ab\u5feb\u901f\u8bbf\u95ee\u5230\u3002<\/p>\n<h4>\u6267\u884c\u6dd8\u6c70\u7b97\u6cd5<\/h4>\n<p>\u8fd9\u91cc\u6709\u4e24\u79cd\u4e3b\u8981\u7684\u6dd8\u6c70\u903b\u8f91&#xff1a;<\/p>\n<p>\u9ed8\u8ba4\u6dd8\u6c70\u903b\u8f91<\/p>\n<p>PriorityQueue&lt;BlockBucket&gt; bucketQueue &#061; new PriorityQueue&lt;&gt;(3);<\/p>\n<p>bucketQueue.add(bucketSingle);<br \/>\nbucketQueue.add(bucketMulti);<br \/>\nbucketQueue.add(bucketMemory);<\/p>\n<p>int remainingBuckets &#061; bucketQueue.size();<\/p>\n<p>BlockBucket bucket;<br \/>\nwhile ((bucket &#061; bucketQueue.poll()) !&#061; null) {<br \/>\n  long overflow &#061; bucket.overflow();<br \/>\n  if (overflow &gt; 0) {<br \/>\n    long bucketBytesToFree &#061;<br \/>\n      Math.min(overflow, (bytesToFree &#8211; bytesFreed) \/ remainingBuckets);<br \/>\n    bytesFreed &#043;&#061; bucket.free(bucketBytesToFree);<br \/>\n  }<br \/>\n  remainingBuckets&#8211;;<br \/>\n}<\/p>\n<p>\u8fd9\u662f\u6807\u51c6\u7684\u3001\u57fa\u4e8e\u201c\u6ea2\u51fa\u201d\u7684\u516c\u5e73\u6dd8\u6c70\u7b97\u6cd5\u3002<\/p>\n<li>\u521b\u5efa\u4f18\u5148\u961f\u5217: \u5c06\u4e09\u4e2a\u00a0BlockBucket\u00a0\u653e\u5165\u4e00\u4e2a\u00a0PriorityQueue\u3002BlockBucket\u00a0\u5b9e\u73b0\u4e86\u00a0Comparable\u00a0\u63a5\u53e3&#xff0c;\u5176\u00a0compareTo\u00a0\u65b9\u6cd5\u6bd4\u8f83\u7684\u662f\u00a0overflow()\u00a0\u7684\u5927\u5c0f\u3002overflow()\u00a0\u8ba1\u7b97\u7684\u662f\u00a0totalSize &#8211; bucketSize&#xff0c;\u5373\u5f53\u524d\u6876\u7684\u5927\u5c0f\u8d85\u51fa\u5176\u76ee\u6807\u5927\u5c0f\u7684\u90e8\u5206\u3002\u56e0\u6b64&#xff0c;\u6ea2\u51fa\u6700\u4e25\u91cd\u7684\u6876\u4f1a\u6392\u5728\u961f\u5217\u7684\u6700\u524d\u9762\u3002<\/li>\n<li>\u5faa\u73af\u5904\u7406: \u5faa\u73af\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u6ea2\u51fa\u6700\u4e25\u91cd\u7684\u6876\u3002<\/li>\n<li>\u8ba1\u7b97\u6dd8\u6c70\u91cf: \u8ba1\u7b97\u8fd9\u4e2a\u6876\u9700\u8981\u91ca\u653e\u7684\u5b57\u8282\u6570\u00a0bucketBytesToFree\u3002\u8fd9\u4e2a\u503c\u53d6\u00a0overflow\u00a0\u548c\u00a0(bytesToFree &#8211; bytesFreed) \/ remainingBuckets\u00a0\u4e2d\u7684\u8f83\u5c0f\u8005\u3002\u540e\u8005\u662f\u4e3a\u4e86\u5c06\u603b\u7684\u6dd8\u6c70\u4efb\u52a1\u5747\u5206\u7ed9\u5269\u4e0b\u7684\u6876&#xff0c;\u5b9e\u73b0\u516c\u5e73\u6027\u3002<\/li>\n<li>\u91ca\u653e\u7a7a\u95f4: \u8c03\u7528\u00a0bucket.free()\u00a0\u6765\u5b9e\u9645\u6267\u884c\u6dd8\u6c70\u3002<\/li>\n<li>\u8fd9\u4e2a\u8fc7\u7a0b\u4f1a\u4e00\u76f4\u6301\u7eed&#xff0c;\u76f4\u5230\u961f\u5217\u4e3a\u7a7a\u3002<\/li>\n<\/p>\n<p>BlockBucket.free(long toFree)<\/p>\n<p>\/\/ in class BlockBucket<br \/>\npublic long free(long toFree) {<br \/>\n  \/\/ &#8230; logging &#8230;<br \/>\n  LruCachedBlock cb;<br \/>\n  long freedBytes &#061; 0;<br \/>\n  while ((cb &#061; queue.pollLast()) !&#061; null) {<br \/>\n    freedBytes &#043;&#061; evictBlock(cb, true);<br \/>\n    if (freedBytes &gt;&#061; toFree) {<br \/>\n      return freedBytes;<br \/>\n    }<br \/>\n  }<br \/>\n  \/\/ &#8230; logging &#8230;<br \/>\n  return freedBytes;<br \/>\n}<\/p>\n<li>queue.pollLast(): \u4ece\u00a0LruCachedBlockQueue\u00a0\u4e2d\u53d6\u51fa\u5e76\u79fb\u9664\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u5757&#xff08;\u56e0\u4e3a\u00a0LruCachedBlock\u00a0\u7684\u6392\u5e8f\u89c4\u5219&#xff0c;accessTime\u00a0\u6700\u5c0f\u7684\u5757\u5728\u961f\u5217\u672b\u5c3e&#xff09;\u3002<\/li>\n<li>evictBlock(cb, true): \u8fd9\u662f\u771f\u6b63\u5c06\u5757\u4ece\u7f13\u5b58\u4e2d\u79fb\u9664\u7684\u5730\u65b9\u3002\u6211\u4eec\u7a0d\u540e\u4f1a\u6df1\u5165\u5206\u6790\u5b83\u3002\u5b83\u8fd4\u56de\u88ab\u79fb\u9664\u5757\u7684\u5927\u5c0f\u3002<\/li>\n<li>\u5faa\u73af: \u4e0d\u65ad\u5730\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u6700\u8001\u7684\u5757\u5e76\u79fb\u9664&#xff0c;\u76f4\u5230\u91ca\u653e\u7684\u5b57\u8282\u6570\u00a0freedBytes\u00a0\u8fbe\u5230\u4e86\u00a0toFree\u00a0\u7684\u8981\u6c42\u3002<\/li>\n<p>\u4f18\u5148\u4fdd\u62a4\u5185\u5b58\u4e2d&#xff08;in-memory&#xff09;\u6570\u636e\u5757\u7684\u7279\u6b8a\u6dd8\u6c70\u6a21\u5f0f\u3002<\/p>\n<p>\u8fd9\u4e2a\u903b\u8f91\u7684\u89e6\u53d1\u6761\u4ef6\u662f&#xff1a;<\/p>\n<p>if (forceInMemory || memoryFactor &gt; 0.999f)<\/p>\n<ul>\n<li>forceInMemory: \u8fd9\u662f\u4e00\u4e2a\u5e03\u5c14\u7c7b\u578b\u7684\u914d\u7f6e\u9879&#xff0c;\u5f53\u5b83\u4e3a\u00a0true\u00a0\u65f6&#xff0c;\u8868\u793a\u5f3a\u5236\u4f18\u5148\u4fdd\u7559\u5185\u5b58\u5757\u3002<\/li>\n<li>memoryFactor: \u8fd9\u662f\u4e00\u4e2a\u6d6e\u70b9\u6570\u914d\u7f6e\u9879&#xff0c;\u4ee3\u8868\u4e3a\u5185\u5b58\u5757&#xff08;in-memory blocks&#xff09;\u5206\u914d\u7684\u7f13\u5b58\u7a7a\u95f4\u6bd4\u4f8b\u3002\u5f53\u8fd9\u4e2a\u503c\u975e\u5e38\u63a5\u8fd11\u65f6&#xff08;&gt; 0.999f&#xff09;&#xff0c;\u610f\u5473\u7740\u51e0\u4e4e\u6240\u6709\u7684\u7f13\u5b58\u90fd\u5e94\u8be5\u7528\u4e8e\u5185\u5b58\u5757\u3002<\/li>\n<\/ul>\n<p>\u5f53\u6ee1\u8db3\u4ee5\u4e0a\u4efb\u4e00\u6761\u4ef6\u65f6&#xff0c;\u5c31\u4f1a\u8fdb\u5165\u8fd9\u4e2a\u7279\u6b8a\u7684\u6dd8\u6c70\u903b\u8f91&#xff0c;\u5176\u6838\u5fc3\u601d\u60f3\u662f&#xff1a;\u5c3d\u53ef\u80fd\u5730\u907f\u514d\u6dd8\u6c70 &#034;memory&#034; \u7c7b\u578b\u7684\u7f13\u5b58\u5757&#xff0c;\u4f18\u5148\u6dd8\u6c70 &#034;single-access&#034; \u548c &#034;multi-access&#034; \u7c7b\u578b\u7684\u7f13\u5b58\u5757\u3002<\/p>\n<p>\u8fd9\u4e2a\u903b\u8f91\u5185\u90e8\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5&#xff1a;<\/p>\n<li>\n<p>\u4e0d\u5f97\u4e0d\u6dd8\u6c70 &#034;memory&#034; \u5757\u7684\u60c5\u51b5<\/p>\n<p>if (bytesToFree &gt; (s &#043; m)) {<br \/>\n    \/\/ &#8230;<br \/>\n}<\/p>\n<ul>\n<li>bytesToFree: \u672c\u6b21\u9700\u8981\u91ca\u653e\u7684\u5b57\u8282\u6570\u3002<\/li>\n<li>s: &#034;single-access&#034; \u6876\u4e2d\u6240\u6709\u5757\u7684\u603b\u5927\u5c0f\u3002<\/li>\n<li>m: &#034;multi-access&#034; \u6876\u4e2d\u6240\u6709\u5757\u7684\u603b\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e2a\u00a0if\u00a0\u6761\u4ef6\u610f\u5473\u7740&#xff0c;\u5373\u4f7f\u628a &#034;single&#034; \u548c &#034;multi&#034; \u6876\u91cc\u7684\u5757\u5168\u90e8\u6e05\u7a7a&#xff0c;\u4e5f\u65e0\u6cd5\u6ee1\u8db3\u9700\u8981\u91ca\u653e\u7684\u7a7a\u95f4\u5927\u5c0f\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b&#xff0c;\u4ee3\u7801\u4f1a&#xff1a; a. \u9996\u5148\u6e05\u7a7a\u00a0bucketSingle\u00a0\u548c\u00a0bucketMulti\u3002 b. \u7136\u540e\u4ece\u00a0bucketMemory\u00a0\u4e2d\u6dd8\u6c70\u6389\u5269\u4f59\u9700\u8981\u91ca\u653e\u7684\u7a7a\u95f4\u3002 \u8fd9\u6e05\u6670\u5730\u4f53\u73b0\u4e86\u5bf9 &#034;memory&#034; \u5757\u7684\u4fdd\u62a4&#xff1a;\u53ea\u6709\u5728\u4e07\u4e0d\u5f97\u5df2\u65f6\u624d\u52a8\u5b83\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e0d\u9700\u8981\u6dd8\u6c70 &#034;memory&#034; \u5757\u7684\u60c5\u51b5<\/p>\n<p>\u8fd9\u79cd\u60c5\u51b5\u610f\u5473\u7740&#xff0c;\u4ec5\u4ece &#034;single&#034; \u548c &#034;multi&#034; \u6876\u4e2d\u6dd8\u6c70\u5757\u5c31\u8db3\u4ee5\u91ca\u653e\u6240\u9700\u7a7a\u95f4\u3002\u6b64\u65f6&#xff0c;\u4ee3\u7801\u7684\u76ee\u6807\u662f\u5728\u5b8c\u6210\u6dd8\u6c70\u540e&#xff0c;\u5c3d\u529b\u7ef4\u6301 &#034;single&#034; \u6876\u548c &#034;multi&#034; \u6876\u7684\u5927\u5c0f\u6bd4\u4f8b\u4e3a 1:2\u3002<\/p>\n<ul>\n<li>long bytesRemain &#061; s &#043; m &#8211; bytesToFree;: \u8ba1\u7b97\u51fa\u6dd8\u6c70\u540e &#034;single&#034; \u548c &#034;multi&#034; \u6876\u5e94\u8be5\u5269\u4f59\u7684\u603b\u5927\u5c0f\u3002<\/li>\n<li>\u968f\u540e\u7684\u00a0if-else if-else\u00a0\u903b\u8f91\u5c31\u662f\u6839\u636e\u00a0bytesRemain\u00a0\u6765\u8ba1\u7b97\u5206\u522b\u5e94\u8be5\u4ece\u4e24\u4e2a\u6876\u4e2d\u6dd8\u6c70\u591a\u5c11\u6570\u636e&#xff0c;\u4ece\u800c\u4f7f\u5f97\u5b83\u4eec\u5269\u4f59\u7684\u5927\u5c0f\u8d8b\u8fd1\u4e8e\u00a01:2\u00a0\u7684\u6bd4\u4f8b\u3002<\/li>\n<\/ul>\n<\/li>\n<p>evictBlock(LruCachedBlock block, boolean evictedByEvictionProcess)<\/p>\n<p>\/\/ in class LruBlockCache<br \/>\nprotected long evictBlock(LruCachedBlock block, boolean evictedByEvictionProcess) {<br \/>\n  final MutableBoolean evicted &#061; new MutableBoolean(false);<br \/>\n  map.computeIfPresent(block.getCacheKey(), (k, v) -&gt; {<br \/>\n    if (evictedByEvictionProcess &amp;&amp; victimHandler !&#061; null) {<br \/>\n      victimHandler.cacheBlock(k, v.getBuffer());<br \/>\n    }<br \/>\n    v.getBuffer().release();<br \/>\n    evicted.setTrue();<br \/>\n    return null;<br \/>\n  });<br \/>\n  if (evicted.isFalse()) {<br \/>\n    return 0;<br \/>\n  }<br \/>\n  \/\/ &#8230; update metrics &#8230;<br \/>\n  return block.heapSize();<br \/>\n}<\/p>\n<p>\u8fd9\u662f\u79fb\u9664\u5355\u4e2a\u5757\u7684\u6700\u7ec8\u5b9e\u73b0&#xff0c;\u975e\u5e38\u5173\u952e&#xff1a;<\/p>\n<li>map.computeIfPresent(&#8230;): \u4f7f\u7528\u00a0ConcurrentHashMap\u00a0\u7684\u539f\u5b50\u64cd\u4f5c\u6765\u67e5\u627e\u5e76\u79fb\u9664\u5757\u3002\u8fd9\u662f\u4e3a\u4e86\u4fdd\u8bc1\u5728\u79fb\u9664\u8fc7\u7a0b\u4e2d\u4e0d\u4f1a\u6709\u5176\u4ed6\u7ebf\u7a0b\u6b63\u5728\u8bbf\u95ee\u8fd9\u4e2a\u5757\u3002<\/li>\n<li>Victim Cache: \u5982\u679c\u5757\u662f\u7531\u6dd8\u6c70\u7ebf\u7a0b\u79fb\u9664\u7684&#xff08;evictedByEvictionProcess\u00a0\u4e3a\u00a0true&#xff09;&#xff0c;\u5e76\u4e14\u914d\u7f6e\u4e86 L2 \u7f13\u5b58&#xff08;victimHandler&#xff09;&#xff0c;\u90a3\u4e48\u5728\u79fb\u9664\u524d&#xff0c;\u4f1a\u5148\u5c1d\u8bd5\u5c06\u8fd9\u4e2a\u5757\u653e\u5165 L2 \u7f13\u5b58\u3002<\/li>\n<li>v.getBuffer().release(): \u51cf\u5c11\u5757\u7684\u5f15\u7528\u8ba1\u6570\u3002\u5982\u679c\u5f15\u7528\u8ba1\u6570\u964d\u4e3a0&#xff0c;\u5757\u7684\u5185\u5b58&#xff08;\u7279\u522b\u662f\u5806\u5916\u5185\u5b58&#xff09;\u5c06\u88ab\u91ca\u653e\u3002<\/li>\n<li>return null: \u5728\u00a0computeIfPresent\u00a0\u7684 lambda \u8868\u8fbe\u5f0f\u4e2d\u8fd4\u56de\u00a0null\u00a0\u4f1a\u5bfc\u81f4\u8be5\u952e\u503c\u5bf9\u4ece\u00a0ConcurrentHashMap\u00a0\u4e2d\u88ab\u539f\u5b50\u5730\u79fb\u9664\u3002<\/li>\n<li>\u66f4\u65b0\u7edf\u8ba1: \u5982\u679c\u79fb\u9664\u6210\u529f&#xff0c;\u4f1a\u66f4\u65b0\u7f13\u5b58\u7684\u603b\u5927\u5c0f\u3001\u5143\u7d20\u6570\u91cf\u7b49\u5404\u79cd\u7edf\u8ba1\u6307\u6807\u3002<\/li>\n<h4>\u6700\u7ec8\u6e05\u7406<\/h4>\n<p>} finally {<br \/>\n  stats.evict();<br \/>\n  evictionInProgress &#061; false;<br \/>\n  evictionLock.unlock();<br \/>\n}<\/p>\n<p>\u5728\u00a0finally\u00a0\u5757\u4e2d&#xff1a;<\/p>\n<ul>\n<li>stats.evict(): \u66f4\u65b0\u6dd8\u6c70\u6b21\u6570\u7684\u7edf\u8ba1\u3002<\/li>\n<li>evictionInProgress &#061; false: \u91cd\u7f6e\u72b6\u6001\u6807\u5fd7\u3002<\/li>\n<li>evictionLock.unlock():\u00a0\u91ca\u653e\u9501&#xff0c;\u5141\u8bb8\u4e0b\u4e00\u6b21\u6dd8\u6c70\u64cd\u4f5c\u53ef\u4ee5\u8fdb\u884c\u3002<\/li>\n<\/ul>\n<h4><\/h4>\n<p>LruBlockCache.evict()\u00a0\u662f\u4e00\u4e2a\u8bbe\u8ba1\u7cbe\u826f\u3001\u903b\u8f91\u590d\u6742\u7684\u51fd\u6570&#xff0c;\u5b83\u5b8c\u7f8e\u5730\u4f53\u73b0\u4e86\u8be5\u7f13\u5b58\u7cfb\u7edf\u7684\u6838\u5fc3\u601d\u60f3&#xff1a;<\/p>\n<li>\u5e76\u53d1\u5b89\u5168: \u901a\u8fc7\u00a0ReentrantLock\u00a0\u548c\u00a0ConcurrentHashMap\u00a0\u7684\u539f\u5b50\u64cd\u4f5c&#xff0c;\u786e\u4fdd\u4e86\u6dd8\u6c70\u8fc7\u7a0b\u7684\u7ebf\u7a0b\u5b89\u5168\u3002<\/li>\n<li>\u5f02\u6b65\u6267\u884c: \u6574\u4e2a\u00a0evict()\u00a0\u65b9\u6cd5\u88ab\u8bbe\u8ba1\u4e3a\u7531\u540e\u53f0\u7ebf\u7a0b\u8c03\u7528&#xff0c;\u907f\u514d\u963b\u585e\u5173\u952e\u7684\u8bfb\u5199\u8def\u5f84\u3002<\/li>\n<li>\u5e26\u4f18\u5148\u7ea7\u7684LRU: \u901a\u8fc7\u5206\u6876\u7b56\u7565&#xff0c;\u5b9e\u73b0\u4e86\u6bd4\u7b80\u5355LRU\u66f4\u667a\u80fd\u7684\u6dd8\u6c70\u673a\u5236&#xff0c;\u6709\u6548\u9632\u6b62\u4e86\u7f13\u5b58\u6c61\u67d3\u3002<\/li>\n<li>\u516c\u5e73\u6027: \u6dd8\u6c70\u7b97\u6cd5\u4f1a\u6839\u636e\u6bcf\u4e2a\u4f18\u5148\u7ea7\u6876\u7684\u201c\u6ea2\u51fa\u201d\u60c5\u51b5&#xff0c;\u6309\u6bd4\u4f8b\u516c\u5e73\u5730\u5206\u914d\u6dd8\u6c70\u4efb\u52a1\u3002<\/li>\n<li>\u5206\u5c42\u7f13\u5b58\u652f\u6301: \u4e0e Victim Cache&#xff08;L2 \u7f13\u5b58&#xff09;\u65e0\u7f1d\u96c6\u6210&#xff0c;\u88ab\u6dd8\u6c70\u7684\u5757\u53ef\u4ee5\u5e73\u6ed1\u5730\u79fb\u52a8\u5230\u4e0b\u4e00\u7ea7\u7f13\u5b58\u3002<\/li>\n<p>\u6574\u4e2a\u6d41\u7a0b\u4ece\u5b8f\u89c2\u7684\u52a0\u9501\u3001\u8ba1\u7b97&#xff0c;\u5230\u4e2d\u89c2\u7684\u5206\u6876\u3001\u6392\u5e8f&#xff0c;\u518d\u5230\u5fae\u89c2\u7684\u539f\u5b50\u79fb\u9664\u548c\u5f15\u7528\u8ba1\u6570\u7ba1\u7406&#xff0c;\u5c42\u5c42\u9012\u8fdb&#xff0c;\u903b\u8f91\u4e25\u8c28&#xff0c;\u662f\u7406\u89e3 HBase \u9ad8\u6027\u80fd\u7f13\u5b58\u5b9e\u73b0\u7684\u5173\u952e\u3002<\/p>\n<h3>LruBlockCache\u00a0\u4e2d block \u7684\u4f18\u5148\u7ea7<\/h3>\n<p>\u5728\u00a0single&#xff08;\u5355\u6b21\u8bbf\u95ee&#xff09;\u3001multi&#xff08;\u591a\u6b21\u8bbf\u95ee&#xff09;\u548c\u00a0in-memory&#xff08;\u5185\u5b58\u4e2d&#xff09;\u4e4b\u95f4\u8f6c\u6362\u7684\u3002<\/p>\n<p>\u8fd9\u4e2a\u8f6c\u6362\u903b\u8f91\u4e3b\u8981\u7531\u00a0LruBlockCache\u00a0\u7684\u8bbe\u8ba1\u51b3\u5b9a&#xff0c;\u5176\u76ee\u7684\u662f\u4e3a\u4e86\u5728\u7f13\u5b58\u4e2d\u66f4\u597d\u5730\u533a\u5206\u4e0d\u540c\u8bbf\u95ee\u6a21\u5f0f\u7684\u6570\u636e&#xff0c;\u9632\u6b62\u5076\u7136\u7684\u5927\u91cf\u626b\u63cf&#xff08;scan&#xff09;\u64cd\u4f5c\u6c61\u67d3\u6574\u4e2a\u7f13\u5b58&#xff0c;\u4ece\u800c\u63d0\u9ad8\u7f13\u5b58\u547d\u4e2d\u7387\u3002<\/p>\n<h4><\/h4>\n<p>\u5f53\u4e00\u4e2a block \u9996\u6b21\u88ab\u653e\u5165\u7f13\u5b58\u65f6&#xff0c;\u5b83\u7684\u521d\u59cb\u4f18\u5148\u7ea7\u5c31\u88ab\u786e\u5b9a\u4e86\u3002\u8fd9\u4e3b\u8981\u53d1\u751f\u5728\u00a0cacheBlock\u00a0\u65b9\u6cd5\u4e2d\u3002<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  &#064;Override<br \/>\n  public void cacheBlock(BlockCacheKey cacheKey, Cacheable buf, boolean inMemory) {<br \/>\n    if (buf.heapSize() &gt; maxBlockSize) {<br \/>\n\/\/ &#8230; existing code &#8230;<br \/>\n      return;<br \/>\n    }<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n    \/\/ Ensure that the block is an heap one.<br \/>\n    buf &#061; asReferencedHeapBlock(buf);<br \/>\n    cb &#061; new LruCachedBlock(cacheKey, buf, count.incrementAndGet(), inMemory);<br \/>\n    long newSize &#061; updateSizeMetrics(cb, false);<br \/>\n    map.put(cacheKey, cb);<br \/>\n\/\/ &#8230; existing code &#8230;<br \/>\n  }<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<p>\u4ece\u00a0cacheBlock\u00a0\u65b9\u6cd5\u7684\u7b7e\u540d\u548c\u5b9e\u73b0\u53ef\u4ee5\u770b\u51fa&#xff1a;<\/p>\n<ul>\n<li>in-memory\u00a0\u4f18\u5148\u7ea7: \u5982\u679c\u00a0cacheBlock\u00a0\u88ab\u8c03\u7528\u65f6&#xff0c;inMemory\u00a0\u53c2\u6570\u4e3a\u00a0true&#xff08;\u8fd9\u901a\u5e38\u610f\u5473\u7740\u8be5 block \u6240\u5c5e\u7684\u5217\u65cf\u88ab\u914d\u7f6e\u4e3a\u00a0inMemory&#061;true&#xff09;&#xff0c;\u90a3\u4e48\u65b0\u521b\u5efa\u7684\u00a0LruCachedBlock\u00a0\u4f1a\u88ab\u8d4b\u4e88\u00a0MEMORY\u00a0\u4f18\u5148\u7ea7\u3002\u8fd9\u7c7b block \u4f1a\u88ab\u4f18\u5148\u4fdd\u7559\u5728\u7f13\u5b58\u4e2d\u3002<\/li>\n<li>single-access\u00a0\u4f18\u5148\u7ea7: \u5982\u679c\u00a0inMemory\u00a0\u53c2\u6570\u4e3a\u00a0false&#xff0c;\u90a3\u4e48\u8be5 block \u5728\u9996\u6b21\u88ab\u7f13\u5b58\u65f6&#xff0c;\u4f1a\u88ab\u8d4b\u4e88\u00a0SINGLE\u00a0\u4f18\u5148\u7ea7\u3002<\/li>\n<\/ul>\n<h4>\u4ece\u00a0single\u00a0\u5230\u00a0multi\u00a0\u7684\u8f6c\u6362<\/h4>\n<p>\u5f53\u4e00\u4e2a\u5df2\u7ecf\u662f\u00a0SINGLE\u00a0\u4f18\u5148\u7ea7\u7684 block \u88ab\u518d\u6b21\u8bbf\u95ee\u65f6&#xff0c;\u5b83\u7684\u4f18\u5148\u7ea7\u4f1a\u88ab\u63d0\u5347\u4e3a\u00a0MULTI\u3002\u8fd9\u4e2a\u903b\u8f91\u53d1\u751f\u5728\u00a0getBlock\u00a0\u65b9\u6cd5\u4e2d\u3002<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  &#064;Override<br \/>\n  public Cacheable getBlock(BlockCacheKey cacheKey, boolean caching, boolean repeat,<br \/>\n    boolean updateCacheMetrics) {<br \/>\n    \/\/ Note: &#039;map&#039; must be a ConcurrentHashMap or the supplier may be invoked more than once.<br \/>\n    LruCachedBlock cb &#061; map.computeIfPresent(cacheKey, (key, val) -&gt; {<br \/>\n\/\/ &#8230; existing code &#8230;<br \/>\n      val.getBuffer().retain();<br \/>\n      return val;<br \/>\n    });<br \/>\n    if (cb &#061;&#061; null) {<br \/>\n\/\/ &#8230; existing code &#8230;<br \/>\n      return null;<br \/>\n    }<br \/>\n    if (updateCacheMetrics) {<br \/>\n      stats.hit(caching, cacheKey.isPrimary(), cacheKey.getBlockType());<br \/>\n    }<br \/>\n    cb.access(count.incrementAndGet());<br \/>\n    return cb.getBuffer();<br \/>\n  }<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<p>\u5f53\u00a0getBlock\u00a0\u65b9\u6cd5\u6210\u529f\u5728\u7f13\u5b58\u4e2d\u627e\u5230\u4e00\u4e2a block&#xff08;\u5373\u7f13\u5b58\u547d\u4e2d&#xff09;\u65f6&#xff0c;\u4f1a\u8c03\u7528\u00a0cb.access(&#8230;)\u00a0\u65b9\u6cd5\u3002\u8fd9\u4e2a\u00a0access\u00a0\u65b9\u6cd5&#xff08;\u5728\u00a0LruCachedBlock\u00a0\u5185\u90e8\u5b9e\u73b0&#xff0c;\u5176\u884c\u4e3a\u5728\u00a0LruBlockCache\u00a0\u7684\u7c7b\u6ce8\u91ca\u4e2d\u6709\u8be6\u7ec6\u63cf\u8ff0&#xff09;\u4f1a\u68c0\u67e5\u5f53\u524d block \u7684\u4f18\u5148\u7ea7&#xff1a;<\/p>\n<ul>\n<li>\u5982\u679c\u5f53\u524d\u4f18\u5148\u7ea7\u662f\u00a0SINGLE&#xff0c;\u5b83\u4f1a\u5c06\u5176\u63d0\u5347\u4e3a\u00a0MULTI\u3002<\/li>\n<li>\u5982\u679c\u5f53\u524d\u4f18\u5148\u7ea7\u5df2\u7ecf\u662f\u00a0MULTI\u00a0\u6216\u00a0MEMORY&#xff0c;\u5219\u4fdd\u6301\u4e0d\u53d8\u3002<\/li>\n<\/ul>\n<p>  \/**<br \/>\n   * Block has been accessed.<br \/>\n   * &#064;param accessTime Last access; this is actually a incremented sequence number rather than an<br \/>\n   *                   actual time.<br \/>\n   *\/<br \/>\n  public void access(long accessTime) {<br \/>\n    this.accessTime &#061; accessTime;<br \/>\n    if (this.priority &#061;&#061; BlockPriority.SINGLE) {<br \/>\n      this.priority &#061; BlockPriority.MULTI;<br \/>\n    }<br \/>\n  }<\/p>\n<p>block \u7684\u4f18\u5148\u7ea7\u8f6c\u6362\u8def\u5f84\u662f\u5355\u5411\u7684&#xff1a;<\/p>\n<li>\n<p>\u9996\u6b21\u7f13\u5b58:<\/p>\n<ul>\n<li>inMemory&#061;true\u00a0-&gt;\u00a0MEMORY<\/li>\n<li>inMemory&#061;false\u00a0-&gt;\u00a0SINGLE<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u540e\u7eed\u8bbf\u95ee:<\/p>\n<ul>\n<li>SINGLE\u00a0-&gt;\u00a0MULTI\u00a0(\u5728\u7b2c\u4e00\u6b21\u7f13\u5b58\u547d\u4e2d\u540e)<\/li>\n<li>MULTI\u00a0-&gt;\u00a0MULTI\u00a0(\u4fdd\u6301\u4e0d\u53d8)<\/li>\n<li>MEMORY\u00a0-&gt;\u00a0MEMORY\u00a0(\u4fdd\u6301\u4e0d\u53d8)<\/li>\n<\/ul>\n<\/li>\n<p>\u4e00\u65e6\u4e00\u4e2a block \u7684\u4f18\u5148\u7ea7\u53d8\u4e3a\u00a0MULTI\u00a0\u6216\u00a0MEMORY&#xff0c;\u5b83\u5c31\u4e0d\u4f1a\u518d\u964d\u7ea7\u3002\u8fd9\u79cd\u673a\u5236\u786e\u4fdd\u4e86\u9891\u7e41\u8bbf\u95ee\u7684\u201c\u70ed\u201d\u6570\u636e\u548c\u914d\u7f6e\u4e3a\u5e38\u9a7b\u5185\u5b58\u7684\u6570\u636e\u80fd\u591f\u6709\u66f4\u9ad8\u7684\u6982\u7387\u4fdd\u7559\u5728\u7f13\u5b58\u4e2d&#xff0c;\u800c\u53ea\u88ab\u8bbf\u95ee\u4e00\u6b21\u7684\u201c\u51b7\u201d\u6570\u636e&#xff08;\u5982\u5168\u8868\u626b\u63cf\u4ea7\u751f\u7684\u6570\u636e&#xff09;\u5219\u66f4\u5bb9\u6613\u88ab\u6dd8\u6c70\u3002<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>LruBlockCache\u00a0\u662f\u4e00\u4e2a\u9ad8\u5ea6\u4f18\u5316\u7684\u3001\u751f\u4ea7\u7ea7\u7684\u7f13\u5b58\u5b9e\u73b0\u3002\u5b83\u901a\u8fc7\u4ee5\u4e0b\u8bbe\u8ba1\u5b9e\u73b0\u4e86\u9ad8\u6027\u80fd\u548c\u9ad8\u6548\u7387&#xff1a;<\/p>\n<ul>\n<li>\u5e76\u53d1\u6570\u636e\u7ed3\u6784: \u4f7f\u7528\u00a0ConcurrentHashMap\u00a0\u907f\u514d\u4e86\u663e\u5f0f\u9501&#xff0c;\u63d0\u4f9b\u4e86\u9ad8\u5e76\u53d1\u8bbf\u95ee\u80fd\u529b\u3002<\/li>\n<li>\u5f02\u6b65\u6dd8\u6c70: \u5c06\u8017\u65f6\u7684\u6dd8\u6c70\u64cd\u4f5c\u653e\u5728\u540e\u53f0\u7ebf\u7a0b&#xff0c;\u4fdd\u8bc1\u4e86\u524d\u53f0\u8bfb\u5199\u8bf7\u6c42\u7684\u4f4e\u5ef6\u8fdf\u3002<\/li>\n<li>\u4f18\u5148\u7ea7\u5206\u533a: \u901a\u8fc7\u00a0SINGLE,\u00a0MULTI,\u00a0MEMORY\u00a0\u4e09\u4e2a\u4f18\u5148\u7ea7&#xff0c;\u6709\u6548\u62b5\u6297\u4e86\u626b\u63cf\u64cd\u4f5c\u5bf9\u7f13\u5b58\u7684\u6c61\u67d3&#xff0c;\u63d0\u9ad8\u4e86\u70ed\u70b9\u6570\u636e\u7684\u547d\u4e2d\u7387\u3002<\/li>\n<li>\u7cbe\u7ec6\u5316\u5185\u5b58\u7ba1\u7406: \u901a\u8fc7\u00a0HeapSize\u00a0\u63a5\u53e3\u548c\u8be6\u7ec6\u7684\u7edf\u8ba1&#xff0c;\u7cbe\u786e\u63a7\u5236 JVM \u5806\u5185\u5b58\u7684\u4f7f\u7528\u3002<\/li>\n<li>\u53ef\u6269\u5c55\u6027: \u901a\u8fc7\u00a0FirstLevelBlockCache\u00a0\u548c\u00a0victimHandler\u00a0\u673a\u5236&#xff0c;\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e0e L2 \u7f13\u5b58&#xff08;\u5982\u00a0BucketCache&#xff09;\u7ec4\u5408&#xff0c;\u6784\u5efa\u66f4\u5f3a\u5927\u7684\u5206\u5c42\u7f13\u5b58\u7cfb\u7edf\u3002<\/li>\n<\/ul>\n<p>\u7406\u89e3\u00a0LruBlockCache\u00a0\u7684\u5de5\u4f5c\u539f\u7406\u5bf9\u4e8e HBase \u7684\u6027\u80fd\u8c03\u4f18\u548c\u95ee\u9898\u8bca\u65ad\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h2>LruCachedBlockQueue\u00a0<\/h2>\n<p>LruCachedBlockQueue\u00a0\u662f HBase \u4e2d\u00a0LruBlockCache\u00a0\u6dd8\u6c70\u673a\u5236\u7684\u4e00\u4e2a\u5173\u952e\u8f85\u52a9\u7c7b\u3002\u5b83\u5e76\u4e0d\u662f\u4e00\u4e2a\u901a\u7528\u7684\u961f\u5217&#xff0c;\u800c\u662f\u4e3a\u4e00\u4e2a\u975e\u5e38\u7279\u5b9a\u7684\u573a\u666f\u8bbe\u8ba1\u7684&#xff1a;\u5728\u5185\u5b58\u5927\u5c0f\u53d7\u9650\u7684\u60c5\u51b5\u4e0b&#xff0c;\u7ef4\u62a4\u4e00\u7ec4\u201c\u6700\u4f18\u201d\u7684\u5143\u7d20\u3002\u5728\u00a0LruBlockCache\u00a0\u7684\u4e0a\u4e0b\u6587\u4e2d&#xff0c;\u8fd9\u4e2a\u201c\u6700\u4f18\u201d\u6307\u7684\u662f \u8bbf\u95ee\u65f6\u95f4\u6700\u65b0&#xff08;accessTime \u6700\u5927&#xff09; \u7684\u00a0LruCachedBlock\u00a0\u96c6\u5408\u3002<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u4ece\u5b83\u7684\u8bbe\u8ba1\u76ee\u6807\u3001\u6838\u5fc3\u6570\u636e\u7ed3\u6784\u3001\u6784\u9020\u51fd\u6570\u548c\u5173\u952e\u65b9\u6cd5\u7b49\u65b9\u9762\u6765\u6df1\u5165\u5256\u6790\u3002<\/p>\n<p>&#064;InterfaceAudience.Private<br \/>\npublic class LruCachedBlockQueue implements HeapSize {<br \/>\n\/\/&#8230;<br \/>\n}<\/p>\n<ul>\n<li>implements HeapSize: \u8fd9\u4e2a\u63a5\u53e3\u8868\u660e\u00a0LruCachedBlockQueue\u00a0\u5b9e\u4f8b\u53ef\u4ee5\u62a5\u544a\u5176\u5185\u90e8\u6240\u6709\u5143\u7d20\u5360\u7528\u7684\u603b\u5806\u5185\u5b58\u5927\u5c0f\u3002\u8fd9\u5bf9\u4e8e\u4e0a\u5c42\u8c03\u7528\u8005&#xff08;BlockBucket&#xff09;\u7edf\u8ba1\u5185\u5b58\u4f7f\u7528\u975e\u5e38\u91cd\u8981\u3002<\/li>\n<\/ul>\n<p>\u5b83\u7684\u6838\u5fc3\u8bbe\u8ba1\u76ee\u6807\u5728\u7c7b\u7684\u6ce8\u91ca\u4e2d\u6709\u6e05\u6670\u7684\u63cf\u8ff0&#xff1a;<\/p>\n<p>A memory-bound queue that will grow until an element brings total size &gt;&#061; maxSize. From then on, only entries that are sorted larger than the smallest current entry will be inserted\/replaced.<\/p>\n<p>\u7ffb\u8bd1\u8fc7\u6765\u5c31\u662f&#xff1a;\u4e00\u4e2a\u53d7\u5185\u5b58\u9650\u5236\u7684\u961f\u5217\u3002\u5b83\u4f1a\u4e00\u76f4\u589e\u957f&#xff0c;\u76f4\u5230\u5185\u90e8\u6240\u6709\u5143\u7d20\u7684\u603b\u5927\u5c0f&#xff08;heapSize&#xff09;\u8d85\u8fc7\u4e86\u8bbe\u5b9a\u7684\u00a0maxSize\u3002\u4ece\u90a3\u4e00\u523b\u8d77&#xff0c;\u53ea\u6709\u5f53\u4e00\u4e2a\u65b0\u5143\u7d20\u7684\u6392\u5e8f&#xff08;compareTo\u00a0\u7684\u7ed3\u679c&#xff09;\u6bd4\u961f\u5217\u4e2d\u5f53\u524d\u6700\u5c0f\u7684\u5143\u7d20\u8fd8\u8981\u5927\u65f6&#xff0c;\u624d\u4f1a\u88ab\u8003\u8651\u52a0\u5165\u961f\u5217&#xff08;\u5e76\u53ef\u80fd\u66ff\u6362\u6389\u6700\u5c0f\u7684\u5143\u7d20&#xff09;\u3002<\/p>\n<p>\u8fd9\u4e2a\u7c7b\u7684\u4f5c\u7528\u662f\u5728\u00a0LruBlockCache\u00a0\u7684\u00a0evict()\u00a0\u8fc7\u7a0b\u4e2d&#xff0c;\u4e3a\u6bcf\u4e2a\u4f18\u5148\u7ea7&#xff08;SINGLE,\u00a0MULTI,\u00a0MEMORY&#xff09;\u7684\u00a0BlockBucket\u00a0\u4e34\u65f6\u5b58\u653e\u8be5\u4f18\u5148\u7ea7\u7684\u5168\u90e8\u00a0LruCachedBlock&#xff0c;\u5e76\u80fd\u5feb\u901f\u5730\u4ece\u4e2d\u627e\u5230\u5e76\u79fb\u9664\u6700\u8fd1\u6700\u5c11\u4f7f\u7528&#xff08;accessTime\u00a0\u6700\u5c0f&#xff09;\u7684\u5757\u3002<\/p>\n<h3>\u6838\u5fc3\u6210\u5458\u53d8\u91cf<\/h3>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  private MinMaxPriorityQueue&lt;LruCachedBlock&gt; queue;<\/p>\n<p>  private long heapSize;<br \/>\n  private long maxSize;<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<ul>\n<li>queue\u00a0(MinMaxPriorityQueue&lt;LruCachedBlock&gt;): \u8fd9\u662f\u5b9e\u73b0\u00a0LruCachedBlockQueue\u00a0\u529f\u80fd\u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\u3002MinMaxPriorityQueue\u00a0\u662f Google Guava \u5e93\u63d0\u4f9b\u7684\u4e00\u79cd\u7279\u6b8a\u7684\u4f18\u5148\u961f\u5217\u3002\u4e0e\u6807\u51c6\u7684\u00a0PriorityQueue&#xff08;\u53ea\u80fd\u9ad8\u6548\u8bbf\u95ee\u6700\u5c0f\u5143\u7d20&#xff09;\u4e0d\u540c&#xff0c;MinMaxPriorityQueue\u00a0\u53ef\u4ee5\u5728 O(1) \u65f6\u95f4\u5185\u8bbf\u95ee\u5230\u6700\u5c0f\u548c\u6700\u5927\u7684\u5143\u7d20&#xff0c;\u5e76\u5728 O(log n) \u65f6\u95f4\u5185\u79fb\u9664\u5b83\u4eec\u3002\n<ul>\n<li>\u5728\u8fd9\u4e2a\u573a\u666f\u4e0b&#xff0c;LruCachedBlock\u00a0\u7684\u00a0compareTo\u00a0\u65b9\u6cd5\u5b9a\u4e49\u4e86\u00a0accessTime\u00a0\u8d8a\u5927&#xff08;\u8d8a\u65b0&#xff09;\u7684\u5757\u6392\u5e8f\u8d8a\u9760\u524d&#xff08;\u88ab\u8ba4\u4e3a\u662f\u201c\u66f4\u5927\u201d\u7684\u5143\u7d20&#xff09;\u3002<\/li>\n<li>\u56e0\u6b64&#xff0c;queue.peek()\u00a0\u6216\u00a0queue.poll()\u00a0\u4f1a\u8fd4\u56de**accessTime\u00a0\u6700\u5c0f**\u7684\u5757&#xff08;\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684&#xff09;\u3002<\/li>\n<li>queue.peekLast()\u00a0\u6216\u00a0queue.pollLast()\u00a0\u4f1a\u8fd4\u56de**accessTime\u00a0\u6700\u5927**\u7684\u5757&#xff08;\u6700\u8fd1\u521a\u4f7f\u7528\u7684&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>heapSize\u00a0(long): \u8bb0\u5f55\u5f53\u524d\u961f\u5217\u4e2d\u6240\u6709\u00a0LruCachedBlock\u00a0\u7684\u00a0heapSize()\u00a0\u4e4b\u548c\u3002<\/li>\n<li>maxSize\u00a0(long): \u961f\u5217\u7684\u76ee\u6807\u5185\u5b58\u5927\u5c0f\u4e0a\u9650\u3002\u8fd9\u4e2a\u503c\u5728\u6784\u9020\u65f6\u4f20\u5165&#xff0c;\u901a\u5e38\u662f\u00a0LruBlockCache.evict()\u00a0\u65b9\u6cd5\u4e2d\u8ba1\u7b97\u51fa\u7684\u00a0bytesToFree\u3002<\/li>\n<\/ul>\n<h3>\u6784\u9020\u51fd\u6570<\/h3>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  public LruCachedBlockQueue(long maxSize, long blockSize) {<br \/>\n    Preconditions.checkArgument(blockSize &gt; 0, &#034;negative blockSize %s&#034;, blockSize);<br \/>\n    Preconditions.checkArgument(maxSize &gt; 0, &#034;negative maxSize %s&#034;, maxSize);<br \/>\n    int initialSize &#061; (int) (maxSize \/ blockSize);<br \/>\n    if (initialSize &#061;&#061; 0) {<br \/>\n      initialSize&#043;&#043;;<br \/>\n    }<br \/>\n    queue &#061; MinMaxPriorityQueue.expectedSize(initialSize).create();<br \/>\n    heapSize &#061; 0;<br \/>\n    this.maxSize &#061; maxSize;<br \/>\n  }<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<p>\u6784\u9020\u51fd\u6570\u4e3b\u8981\u505a\u521d\u59cb\u5316\u5de5\u4f5c&#xff1a;<\/p>\n<li>\u63a5\u6536\u00a0maxSize\u00a0\u548c\u00a0blockSize&#xff08;\u9884\u671f\u7684\u5e73\u5747\u5757\u5927\u5c0f&#xff09;\u4f5c\u4e3a\u53c2\u6570\u3002<\/li>\n<li>\u901a\u8fc7\u00a0maxSize \/ blockSize\u00a0\u4f30\u7b97\u51fa\u961f\u5217\u4e2d\u53ef\u80fd\u5305\u542b\u7684\u5143\u7d20\u6570\u91cf&#xff0c;\u5e76\u4ee5\u6b64\u4f5c\u4e3a\u00a0MinMaxPriorityQueue\u00a0\u7684\u521d\u59cb\u5bb9\u91cf&#xff08;expectedSize&#xff09;\u3002\u8fd9\u662f\u4e00\u79cd\u4f18\u5316&#xff0c;\u53ef\u4ee5\u51cf\u5c11\u961f\u5217\u5728\u589e\u957f\u8fc7\u7a0b\u4e2d\u5185\u90e8\u6570\u7ec4\u7684\u91cd\u65b0\u5206\u914d\u548c\u590d\u5236\u6b21\u6570\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u00a0MinMaxPriorityQueue\u00a0\u5b9e\u4f8b\u3002<\/li>\n<li>\u521d\u59cb\u5316\u00a0heapSize\u00a0\u4e3a 0&#xff0c;\u5e76\u4fdd\u5b58\u00a0maxSize\u3002<\/li>\n<h4><\/h4>\n<h3>add(LruCachedBlock cb)<\/h3>\n<p>\u8fd9\u662f\u8fd9\u4e2a\u7c7b\u4e2d\u6700\u6838\u5fc3\u3001\u903b\u8f91\u6700\u590d\u6742\u7684\u65b9\u6cd5\u3002\u5b83\u5b9a\u4e49\u4e86\u5143\u7d20\u5982\u4f55\u88ab\u6dfb\u52a0\u5230\u8fd9\u4e2a\u53d7\u9650\u961f\u5217\u4e2d\u3002<\/p>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  public void add(LruCachedBlock cb) {<br \/>\n    if (heapSize &lt; maxSize) {<br \/>\n      queue.add(cb);<br \/>\n      heapSize &#043;&#061; cb.heapSize();<br \/>\n    } else {<br \/>\n      LruCachedBlock head &#061; queue.peek();<br \/>\n      if (cb.compareTo(head) &gt; 0) {<br \/>\n        heapSize &#043;&#061; cb.heapSize();<br \/>\n        heapSize -&#061; head.heapSize();<br \/>\n        if (heapSize &gt; maxSize) {<br \/>\n          queue.poll();<br \/>\n        } else {<br \/>\n          heapSize &#043;&#061; head.heapSize();<br \/>\n        }<br \/>\n        queue.add(cb);<br \/>\n      }<br \/>\n    }<br \/>\n  }<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<p>\u8be5\u65b9\u6cd5\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5&#xff1a;<\/p>\n<li>\n<p>heapSize &lt; maxSize\u00a0(\u961f\u5217\u672a\u6ee1):<\/p>\n<ul>\n<li>\u8fd9\u662f\u961f\u5217\u7684\u201c\u589e\u957f\u9636\u6bb5\u201d\u3002<\/li>\n<li>\u76f4\u63a5\u5c06\u65b0\u7684\u00a0LruCachedBlock\u00a0(cb) \u6dfb\u52a0\u5230\u00a0queue\u00a0\u4e2d\u3002<\/li>\n<li>\u66f4\u65b0\u00a0heapSize&#xff0c;\u5c06\u5176\u589e\u52a0\u00a0cb.heapSize()\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>heapSize &gt;&#061; maxSize\u00a0(\u961f\u5217\u5df2\u6ee1\u6216\u8d85\u9650):<\/p>\n<ul>\n<li>\u8fd9\u662f\u961f\u5217\u7684\u201c\u66ff\u6362\u9636\u6bb5\u201d\u3002\u6b64\u65f6&#xff0c;\u4e0d\u662f\u6240\u6709\u65b0\u5143\u7d20\u90fd\u80fd\u88ab\u52a0\u5165\u3002<\/li>\n<li>LruCachedBlock head &#061; queue.peek();: \u83b7\u53d6\u5f53\u524d\u961f\u5217\u4e2d\u6700\u5c0f\u7684\u5143\u7d20&#xff0c;\u4e5f\u5c31\u662f\u00a0accessTime\u00a0\u6700\u8001\u7684\u90a3\u4e2a\u5757\u3002<\/li>\n<li>if (cb.compareTo(head) &gt; 0): \u6bd4\u8f83\u65b0\u5757\u00a0cb\u00a0\u548c\u6700\u8001\u7684\u5757\u00a0head\u3002\u6839\u636e\u00a0LruCachedBlock\u00a0\u7684\u00a0compareTo\u00a0\u5b9e\u73b0&#xff0c;\u8fd9\u4e2a\u6761\u4ef6\u7b49\u4ef7\u4e8e\u00a0cb.accessTime &gt; head.accessTime\u3002\u53ea\u6709\u5f53\u65b0\u5757\u6bd4\u961f\u5217\u4e2d\u6700\u8001\u7684\u5757\u8fd8\u8981\u201c\u65b0\u201d\u65f6&#xff0c;\u624d\u8003\u8651\u63a5\u7eb3\u5b83\u3002\u5982\u679c\u65b0\u5757\u4e0d\u591f\u201c\u65b0\u201d&#xff0c;\u5b83\u5c31\u4f1a\u88ab\u76f4\u63a5\u5ffd\u7565&#xff0c;\u65b9\u6cd5\u7ed3\u675f\u3002<\/li>\n<li>\u5982\u679c\u65b0\u5757\u591f\u201c\u65b0\u201d&#xff0c;\u5219\u6267\u884c\u66ff\u6362\u903b\u8f91&#xff1a;\n<ul>\n<li>heapSize &#043;&#061; cb.heapSize(); heapSize -&#061; head.heapSize();: \u9884\u5148\u8ba1\u7b97\u66ff\u6362\u540e\u7684\u00a0heapSize\u3002<\/li>\n<li>if (heapSize &gt; maxSize): \u68c0\u67e5\u66ff\u6362\u540e\u7684\u5927\u5c0f\u662f\u5426\u4ecd\u7136\u8d85\u8fc7\u00a0maxSize\u3002\n<ul>\n<li>\u5982\u679c\u662f&#xff0c;\u5c31\u8c03\u7528\u00a0queue.poll()&#xff0c;\u6b63\u5f0f\u5c06\u6700\u8001\u7684\u5757\u00a0head\u00a0\u4ece\u961f\u5217\u4e2d\u79fb\u9664\u3002<\/li>\n<li>\u5982\u679c\u4e0d\u662f&#xff08;\u610f\u5473\u7740\u65b0\u5757\u6bd4\u88ab\u66ff\u6362\u7684\u5757\u5c0f&#xff09;&#xff0c;\u5219\u4e0d\u79fb\u9664\u00a0head&#xff0c;\u5e76\u5c06\u4e4b\u524d\u51cf\u53bb\u7684\u00a0head.heapSize()\u00a0\u52a0\u56de\u6765\u3002<\/li>\n<\/ul>\n<\/li>\n<li>queue.add(cb): \u5c06\u65b0\u5757\u52a0\u5165\u961f\u5217\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<p>\u6ce8\u610f:\u00a0add\u00a0\u65b9\u6cd5\u7684\u903b\u8f91\u5728\u00a0LruBlockCache\u00a0\u7684\u00a0evict\u00a0\u573a\u666f\u4e0b\u662f\u6b63\u786e\u7684&#xff0c;\u56e0\u4e3a\u00a0evict\u00a0\u53ea\u662f\u7528\u8fd9\u4e2a\u961f\u5217\u6765\u7ef4\u62a4top K \u5f85\u6dd8\u6c70\u5143\u7d20&#xff0c;\u4e4b\u540e\u4f1a\u901a\u8fc7\u00a0pollLast\u00a0\u6765\u4e3b\u52a8\u79fb\u9664\u5143\u7d20\u3002<\/p>\n<p>\u8fd9\u662f\u5728\u6784\u5efa\u6dd8\u6c70\u5019\u9009\u6c60&#xff0c;\u5982\u679c\u6c60\u5b50\u6ee1\u4e86&#xff0c;\u6bcf\u5f53\u9047\u5230\u4e00\u4e2a\u65b0\u5757&#xff0c;\u5c31\u548c\u6c60\u91cc\u6700\u5e74\u8f7b\u7684\u90a3\u4e2a\u5019\u9009\u8005&#xff08;head&#xff09;\u6bd4\u8f83\u3002\u5982\u679c\u65b0\u5757\u6bd4\u5b83\u8fd8\u8001&#xff0c;\u5c31\u8bf4\u660e\u65b0\u5757\u66f4\u5e94\u8be5\u88ab\u6dd8\u6c70&#xff0c;\u4e8e\u662f\u7528\u65b0\u5757\u6362\u6389\u90a3\u4e2a\u6700\u5e74\u8f7b\u7684\u5019\u9009\u8005\u3002\u8fd9\u4e2a\u201c\u6362\u6389\u201d\u7684\u64cd\u4f5c&#xff0c;\u5c31\u662f\u901a\u8fc7 poll() \u79fb\u9664 head&#xff0c;\u518d add(cb) \u5b9e\u73b0\u7684\u3002<\/p>\n<\/p>\n<h3>poll()\u00a0\u548c\u00a0pollLast()<\/h3>\n<p>\/\/ &#8230; existing code &#8230;<br \/>\n  \/** Returns The next element in this queue, or {&#064;code null} if the queue is empty. *\/<br \/>\n  public LruCachedBlock poll() {<br \/>\n    return queue.poll();<br \/>\n  }<\/p>\n<p>  \/** Returns The last element in this queue, or {&#064;code null} if the queue is empty. *\/<br \/>\n  public LruCachedBlock pollLast() {<br \/>\n    return queue.pollLast();<br \/>\n  }<br \/>\n\/\/ &#8230; existing code &#8230;<\/p>\n<ul>\n<li>poll(): \u79fb\u9664\u5e76\u8fd4\u56de\u961f\u5217\u4e2d\u6700\u5c0f\u7684\u5143\u7d20&#xff08;accessTime\u00a0\u6700\u8001\u7684\u5757&#xff09;\u3002<\/li>\n<li>pollLast(): \u79fb\u9664\u5e76\u8fd4\u56de\u961f\u5217\u4e2d\u6700\u5927\u7684\u5143\u7d20&#xff08;accessTime\u00a0\u6700\u65b0\u7684\u5757&#xff09;\u3002\u5728\u00a0LruBlockCache.BlockBucket.free()\u00a0\u65b9\u6cd5\u4e2d&#xff0c;\u6b63\u662f\u901a\u8fc7\u4e0d\u65ad\u8c03\u7528\u00a0pollLast()\u00a0\u6765\u83b7\u53d6\u5e76\u6dd8\u6c70\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7684\u5757\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u91cd\u8981\u7684\u7ec6\u8282&#xff1a;LruCachedBlock\u00a0\u7684\u00a0compareTo\u00a0\u8fd4\u56de\u00a01\u00a0\u8868\u793a\u00a0this\u00a0\u6bd4\u00a0that\u00a0\u8001&#xff0c;\u6240\u4ee5\u6392\u5e8f\u540e&#xff0c;accessTime\u00a0\u5c0f\u7684&#xff08;\u8001\u7684&#xff09;\u88ab\u8ba4\u4e3a\u662f\u201c\u5927\u201d\u5143\u7d20&#xff0c;accessTime\u00a0\u5927\u7684&#xff08;\u65b0\u7684&#xff09;\u88ab\u8ba4\u4e3a\u662f\u201c\u5c0f\u201d\u5143\u7d20\u3002\u56e0\u6b64&#xff0c;poll()\u00a0\u4f1a\u79fb\u9664\u6700\u65b0\u7684\u5757&#xff0c;\u800c\u00a0pollLast()\u00a0\u4f1a\u79fb\u9664\u6700\u8001\u7684\u5757\u3002\u8fd9\u4e0e\u00a0MinMaxPriorityQueue\u00a0\u7684\u901a\u7528\u8bed\u4e49\u6709\u4e9b\u53cd\u76f4\u89c9&#xff0c;\u4f46\u5b8c\u5168\u53d6\u51b3\u4e8e\u00a0LruCachedBlock\u00a0\u7684\u00a0compareTo\u00a0\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<p>\u8ba9\u6211\u4eec\u518d\u6b21\u786e\u8ba4\u00a0LruCachedBlock.compareTo\u00a0\u7684\u5b9e\u73b0&#xff1a;<\/p>\n<p>public int compareTo(LruCachedBlock that) {<br \/>\n  if (this.accessTime &#061;&#061; that.accessTime) return 0;<br \/>\n  return this.accessTime &lt; that.accessTime ? 1 : -1;<br \/>\n}<\/p>\n<p>this.accessTime &lt; that.accessTime\u00a0(this\u66f4\u8001) -&gt; \u8fd4\u56de 1\u3002\u5728\u00a0PriorityQueue\u00a0\u4e2d&#xff0c;\u8fd4\u56de\u503c\u5927\u4e8e0\u610f\u5473\u7740\u00a0this\u00a0\u7684\u4f18\u5148\u7ea7\u66f4\u4f4e&#xff0c;\u4f1a\u6392\u5728\u540e\u9762\u3002\u6240\u4ee5\u00a0poll()\u00a0\u4f1a\u53d6\u51fa\u00a0accessTime\u00a0\u6700\u5927\u7684&#xff08;\u6700\u65b0\u7684&#xff09;&#xff0c;pollLast()\u00a0\u4f1a\u53d6\u51fa\u00a0accessTime\u00a0\u6700\u5c0f\u7684&#xff08;\u6700\u8001\u7684&#xff09;\u3002BlockBucket.free()\u00a0\u4e2d\u8c03\u7528\u00a0queue.pollLast()\u00a0\u662f\u6b63\u786e\u7684&#xff0c;\u5b83\u786e\u5b9e\u5728\u6dd8\u6c70\u6700\u8001\u7684\u5757\u3002<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>LruCachedBlockQueue\u00a0\u662f\u4e00\u4e2a\u9ad8\u5ea6\u7279\u5316\u7684\u6570\u636e\u7ed3\u6784&#xff0c;\u5b83\u5de7\u5999\u5730\u5229\u7528\u4e86 Guava \u7684\u00a0MinMaxPriorityQueue\u00a0\u6765\u6ee1\u8db3\u00a0LruBlockCache\u00a0\u6dd8\u6c70\u7b97\u6cd5\u7684\u9700\u6c42\u3002<\/p>\n<ul>\n<li>\u6838\u5fc3\u4f5c\u7528: \u5728\u6dd8\u6c70\u8fc7\u7a0b\u4e2d&#xff0c;\u4e3a\u6bcf\u4e2a\u4f18\u5148\u7ea7\u6876\u4e34\u65f6\u5b58\u50a8\u6240\u6709\u7684\u5757&#xff0c;\u5e76\u63d0\u4f9b\u9ad8\u6548\u5730\u8bbf\u95ee\u548c\u79fb\u9664\u201c\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u201d\u5757\u7684\u80fd\u529b\u3002<\/li>\n<li>\u5173\u952e\u6570\u636e\u7ed3\u6784:\u00a0MinMaxPriorityQueue\u00a0\u63d0\u4f9b\u4e86 O(log n) \u65f6\u95f4\u590d\u6742\u5ea6\u7684\u00a0pollLast()\u00a0\u64cd\u4f5c&#xff0c;\u8fd9\u5bf9\u4e8e\u4ece\u5927\u91cf\u5757\u4e2d\u627e\u51fa\u6700\u8001\u7684\u5757\u8fdb\u884c\u6dd8\u6c70\u81f3\u5173\u91cd\u8981\u3002<\/li>\n<li>\u5185\u5b58\u611f\u77e5: \u901a\u8fc7\u5b9e\u73b0\u00a0HeapSize\u00a0\u5e76\u7ef4\u62a4\u00a0heapSize\u00a0\u5b57\u6bb5&#xff0c;\u5b83\u4f7f\u5176\u5185\u90e8\u72b6\u6001\u5bf9\u4e0a\u5c42\u8c03\u7528\u8005\u900f\u660e\u3002<\/li>\n<li>\u7279\u5316\u7684\u00a0add\u00a0\u903b\u8f91:\u00a0add\u00a0\u65b9\u6cd5\u7684\u884c\u4e3a\u662f\u4e3a\u201c\u4fdd\u7559\u6700\u4f18\u5143\u7d20\u201d\u8fd9\u4e2a\u7279\u5b9a\u76ee\u6807\u8bbe\u8ba1\u7684&#xff0c;\u800c\u4e0d\u662f\u4e00\u4e2a\u901a\u7528\u7684\u961f\u5217\u6dfb\u52a0\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p>\u603b\u800c\u8a00\u4e4b&#xff0c;LruCachedBlockQueue\u00a0\u662f\u00a0LruBlockCache\u00a0\u5b9e\u73b0\u5176\u9ad8\u6548\u3001\u5e26\u4f18\u5148\u7ea7\u7684\u6dd8\u6c70\u7b56\u7565\u7684\u4e00\u4e2a\u4e0d\u53ef\u6216\u7f3a\u7684\u5e95\u5c42\u7ec4\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e34\u6b21\uff0c\u6536\u85cf13\u6b21\u3002HBase\u7684BlockCache\u91c7\u7528\u7075\u6d3b\u7684\u4e8c\u7ea7\u7f13\u5b58\u67b6\u6784\uff08L1+L2\uff09\uff0c\u5176\u4e2dLruBlockCache\u4f5c\u4e3a\u5806\u5185\u4e00\u7ea7\u7f13\u5b58\uff0cBucketCache\u4f5c\u4e3a\u5806\u5916\u4e8c\u7ea7\u7f13\u5b58\u3002LruBlockCache\u901a\u8fc7\u4e09\u7ea7\u4f18\u5148\u7ea7\uff08SINGLE\/MULTI\/MEMORY\uff09\u548c\u5f02\u6b65\u6dd8\u6c70\u673a\u5236\u4f18\u5316\u7f13\u5b58\u547d\u4e2d\u7387\uff0c\u91c7\u7528ConcurrentHashMap\u4fdd\u8bc1\u9ad8\u5e76\u53d1\u8bfb\u5199\u6027\u80fd\u3002\u4e0eCaffeine\u7b49\u73b0\u4ee3\u7f13\u5b58\u5e93\u4e0d\u540c\uff0cLruBlockCache\u901a\u8fc7\u727a\u7272\u4f4e\u9891\u6dd8\u6c70\u64cd\u4f5c\u6027\u80fd\u6362\u53d6\u9ad8\u9891\u8bfb\u64cd\u4f5c\u7684\u6781\u81f4\u6027\u80fd\uff0c\u4f53\u73b0\u4e86\u7279\u5b9a\u573a\u666f\u4e0b\u7684\u8bbe\u8ba1\u6743\u8861\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5707,99,664,323,100,1813,427],"topic":[],"class_list":["post-55457","post","type-post","status-publish","format-standard","hentry","category-server","tag-hbase","tag-java","tag-664","tag-323","tag-100","tag-1813","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>HBase BlockCache\uff1aLRU Cache - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/55457.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"HBase BlockCache\uff1aLRU Cache - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e34\u6b21\uff0c\u6536\u85cf13\u6b21\u3002HBase\u7684BlockCache\u91c7\u7528\u7075\u6d3b\u7684\u4e8c\u7ea7\u7f13\u5b58\u67b6\u6784\uff08L1+L2\uff09\uff0c\u5176\u4e2dLruBlockCache\u4f5c\u4e3a\u5806\u5185\u4e00\u7ea7\u7f13\u5b58\uff0cBucketCache\u4f5c\u4e3a\u5806\u5916\u4e8c\u7ea7\u7f13\u5b58\u3002LruBlockCache\u901a\u8fc7\u4e09\u7ea7\u4f18\u5148\u7ea7\uff08SINGLE\/MULTI\/MEMORY\uff09\u548c\u5f02\u6b65\u6dd8\u6c70\u673a\u5236\u4f18\u5316\u7f13\u5b58\u547d\u4e2d\u7387\uff0c\u91c7\u7528ConcurrentHashMap\u4fdd\u8bc1\u9ad8\u5e76\u53d1\u8bfb\u5199\u6027\u80fd\u3002\u4e0eCaffeine\u7b49\u73b0\u4ee3\u7f13\u5b58\u5e93\u4e0d\u540c\uff0cLruBlockCache\u901a\u8fc7\u727a\u7272\u4f4e\u9891\u6dd8\u6c70\u64cd\u4f5c\u6027\u80fd\u6362\u53d6\u9ad8\u9891\u8bfb\u64cd\u4f5c\u7684\u6781\u81f4\u6027\u80fd\uff0c\u4f53\u73b0\u4e86\u7279\u5b9a\u573a\u666f\u4e0b\u7684\u8bbe\u8ba1\u6743\u8861\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/55457.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-13T14:54:33+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/55457.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/55457.html\",\"name\":\"HBase BlockCache\uff1aLRU Cache - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-13T14:54:33+00:00\",\"dateModified\":\"2025-08-13T14:54:33+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/55457.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/55457.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/55457.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"HBase BlockCache\uff1aLRU Cache\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"HBase BlockCache\uff1aLRU Cache - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/55457.html","og_locale":"zh_CN","og_type":"article","og_title":"HBase BlockCache\uff1aLRU Cache - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e34\u6b21\uff0c\u6536\u85cf13\u6b21\u3002HBase\u7684BlockCache\u91c7\u7528\u7075\u6d3b\u7684\u4e8c\u7ea7\u7f13\u5b58\u67b6\u6784\uff08L1+L2\uff09\uff0c\u5176\u4e2dLruBlockCache\u4f5c\u4e3a\u5806\u5185\u4e00\u7ea7\u7f13\u5b58\uff0cBucketCache\u4f5c\u4e3a\u5806\u5916\u4e8c\u7ea7\u7f13\u5b58\u3002LruBlockCache\u901a\u8fc7\u4e09\u7ea7\u4f18\u5148\u7ea7\uff08SINGLE\/MULTI\/MEMORY\uff09\u548c\u5f02\u6b65\u6dd8\u6c70\u673a\u5236\u4f18\u5316\u7f13\u5b58\u547d\u4e2d\u7387\uff0c\u91c7\u7528ConcurrentHashMap\u4fdd\u8bc1\u9ad8\u5e76\u53d1\u8bfb\u5199\u6027\u80fd\u3002\u4e0eCaffeine\u7b49\u73b0\u4ee3\u7f13\u5b58\u5e93\u4e0d\u540c\uff0cLruBlockCache\u901a\u8fc7\u727a\u7272\u4f4e\u9891\u6dd8\u6c70\u64cd\u4f5c\u6027\u80fd\u6362\u53d6\u9ad8\u9891\u8bfb\u64cd\u4f5c\u7684\u6781\u81f4\u6027\u80fd\uff0c\u4f53\u73b0\u4e86\u7279\u5b9a\u573a\u666f\u4e0b\u7684\u8bbe\u8ba1\u6743\u8861\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/55457.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-13T14:54:33+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"17 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/55457.html","url":"https:\/\/www.wsisp.com\/helps\/55457.html","name":"HBase BlockCache\uff1aLRU Cache - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-13T14:54:33+00:00","dateModified":"2025-08-13T14:54:33+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/55457.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/55457.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/55457.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"HBase BlockCache\uff1aLRU Cache"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/55457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=55457"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/55457\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=55457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=55457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=55457"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=55457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}