{"id":127,"date":"2021-04-30T09:07:03","date_gmt":"2021-04-30T01:07:03","guid":{"rendered":"https:\/\/blog.king011.com\/?p=127"},"modified":"2021-04-30T09:07:06","modified_gmt":"2021-04-30T01:07:06","slug":"%e5%be%9e%e9%9b%b6%e9%96%8b%e5%a7%8b%e7%9a%84%e7%b7%a9%e5%ad%98%e7%94%9f%e6%b4%bb-lruk","status":"publish","type":"post","link":"https:\/\/blog.king011.com\/?p=127","title":{"rendered":"\u5f9e\u96f6\u958b\u59cb\u7684\u7de9\u5b58\u751f\u6d3b-LRUK"},"content":{"rendered":"\n<p>\u524d\u9762\u6587\u7ae0\u8ac7\u5230 LRU \u7b97\u6cd5\u5728\u9762\u5c0d\u6279\u91cf\u64cd\u4f5c\u6642\uff0c\u5f88\u5bb9\u6613\u8b93\u71b1\u9ede\u6578\u64da\u88ab\u6279\u91cf\u6578\u64da\u66ff\u63db\u51fa\u7de9\u5b58\uff0c\u7232\u6b64\u884d\u751f\u51fa\u4e86 LRUK \u7b97\u6cd5\u3002\u6279\u91cf\u64cd\u4f5c\u5f80\u5f80\u90fd\u662f\u8a2a\u554f\u4e00\u6b21\u5f8c\u4fbf\u4e0d\u518d\u9700\u8981\u6578\u64da\u4e86\uff0cLRUK \u6b63\u662f\u5229\u7528\u9019\u4e00\u9ede\uff0cLRUK \u7684 K \u662f\u4e00\u500b\u8a08\u6578\uff0c\u7576\u6578\u64da\u88ab\u8a2a\u554f\u4e86\u81f3\u5c11 k \u6b21\uff0c\u624d\u5c07\u6578\u64da\u52a0\u5165\u5230 LRU \u7de9\u5b58\u3002\u6240\u4ee5 LRU \u5176\u5be6\u53ef\u4ee5\u7b97\u662f LRU-1\u3002<\/p>\n\n\n\n<p>LRUK \u9700\u8981\u4e00\u500b \u6b77\u53f2\u968a\u5217 history \u65b0\u7684\u7de9\u5b58\u90fd\u5b58\u5132\u5728 history \u4e2d\uff0c\u5728\u5176\u4e2d\u4fdd\u5b58\u7740\u6578\u64da\u88ab\u8a2a\u554f\u7684\u6b21\u6578\uff0c\u53e6\u5916\u9700\u8981\u4e00\u500b lru \u7de9\u5b58\uff0c\u7576 history \u4e2d\u6578\u64da\u5230\u9054 k \u6b21\u5c31\u5c07\u5176\u653e\u5165\u5230 lru \u968a\u5217\u3002 history \u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u7d50\u69cb\u5be6\u73fe\u4f8b\u5982\u4e5f\u53ef\u4ee5\u662f\u4e00\u500b lru \u7de9\u5b58\u968a\u5217\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\u901a\u5e38\u6703\u4f7f\u7528 LRU-2\uff0c\u56e0\u7232\u5982\u679c k \u6578\u5b57\u592a\u5927 lru \u7de9\u5b58\u4e2d\u7684\u6578\u64da\u5f88\u96e3\u88ab\u65b0\u6578\u64da\u66ff\u63db\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5be6\u73fe LRUK<\/h2>\n\n\n\n<p>\u4f86\u8003\u616e\u9700\u8981\u7684\u7d50\u69cb \u9700\u8981\u5b9a\u7fa9\u4e00\u500b\u65b0\u985e\u578b\uff0c\u5728 history \u4fdd\u5b58\u7de9\u5b58\u689d\u76ee\uff0c\u548c\u4e4b\u524d\u7684 cacheValue \u985e\u4f3c \u4f46\u9700\u8981\u591a\u4e00\u500b\u5c6c\u6027 k \u8a18\u9304\u6578\u64da\u8a2a\u554f\u6b21\u6578\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type lrukValue struct {\n\tKey   interface{}\n\tValue interface{}\n\tK     int\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u5f8c\u662f\u7232 LRUK \u5b9a\u7fa9\u4e00\u500b\u985e\u578b\uff0c\u5169\u500b\u7de9\u5b58\u968a\u5217\u5373\u53ef\uff0c\u4e00\u500b\u662f history \u968a\u5217\uff0c\u4e00\u500b\u662f lru \u968a\u5217(\u76f4\u63a5\u4f7f\u7528 \u524d\u9762\u6587\u7ae0\u5be6\u73fe\u7684\u7b97\u6cd5\u5373\u53ef)\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ LRU \u7232\u7de9\u5b58\u5b9a\u7fa9\u4e00\u500b\u81ea\u5b9a\u7fa9\u985e\u578b\ntype LRUK struct {\n\t\/\/ lru \u7de9\u5b58\n\tlru *LRU\n\t\/\/ \u8a2a\u554f\u6b77\u53f2\u8a18\u9304\n\thistory Cache\n\t\/\/ \u8a2a\u554f\u5230\u9054\u591a\u5c11\u6b21\u52a0\u5165 lru\n\tk int\n}\n\n\/\/ \u56e0\u7232golang\u6c92\u6709\u69cb\u9020\u51fd\u6578 \u63d0\u4f9b\u4e00\u500b New \u51fd\u6578 \u5275\u5efa\u7de9\u5b58\nfunc NewLRUK(lru *LRU, history Cache, k int) Cache {\n\tif lru == nil {\n\t\tpanic(`lru not supported nil`)\n\t}\n\tif k &lt; 1 {\n\t\tpanic(`lruk's k must &gt; 0`)\n\t} else if k == 1 {\n\t\treturn lru\n\t}\n\tif history == nil {\n\t\tpanic(`history not supported nil`)\n\t}\n\n\treturn &amp;LRUK{\n\t\tlru:     lru,\n\t\thistory: history,\n\t\tk:       k,\n\t}\n}<\/code><\/pre>\n\n\n\n<p>\u9996\u5148\u5be6\u73fe Delete \u9700\u8981\u5f9e\u5169\u500b\u968a\u5217\u4e2d\u90fd\u522a\u9664\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (c *LRUK) Delete(key interface{}) {\n\tc.lru.Delete(key)\n\tc.history.Delete(key)\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u5f8c\u662f Get \u63a5\u53e3\uff0c\u9996\u5148\u67e5\u8a62 lru \u968a\u5217\u4e2d\u662f\u5426\u5b58\u5728\u5b58\u5728\u5c31\u8fd4\u56de\u7de9\u5b58\u503c\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5c31\u67e5\u8a62 history \u63a5\u53e3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (c *LRUK) Get(key interface{}) (value interface{}, exists bool) {\n\tvalue, exists = c.lru.Get(key)\n\tif exists {\n\t\t\/\/ \u5b58\u5728 \u8fd4\u56de \u7de9\u5b58\n\t\treturn\n\t}\n\t\/\/ \u4e0d\u5b58\u5728 \u67e5\u8a62 \u662f\u5426\u6709\u6b77\u53f2\u8a18\u9304\n\thk, exists := c.history.Get(key)\n\tif !exists {\n\t\t\/\/ \u4e0d\u5b58\u5728\u76f4\u63a5\u8fd4\u56de\n\t\treturn\n\t}\n\t\/\/ \u5b58\u5728\u8a18\u9304 \u8a2d\u7f6e\u8fd4\u56de\u503c\n\tv := hk.(*lrukValue)\n\tvalue = v.Value\n\n\t\/\/ \u589e\u52a0\u8a2a\u554f\u8a18\u9304\n\tv.K++\n\tif v.K == c.k {\n\t\t\/\/ \u5230\u9054 \u8a08\u6578 k \u5c07\u7de9\u5b58\u79fb\u52d5\u5230 lru\n\t\tc.lru.Put(key, v.Value)\n\t\tc.history.Delete(key)\n\t}\n\treturn\n}<\/code><\/pre>\n\n\n\n<p>\u6700\u5f8c\u5be6\u73fe Put \u63a5\u53e3\uff0c\u540c\u6a23\u8981\u6ce8\u610f\u5169\u500b\u968a\u5217\u7684\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (c *LRUK) Put(key, value interface{}) {\n\t_, exists := c.lru.Get(key)\n\tif exists { \/\/\u5df2\u7d93\u5b58\u5728\u7de9\u5b58\u4e2d \u66f4\u65b0\u503c\u5373\u53ef\n\t\tc.lru.Put(key, value)\n\t\treturn\n\t}\n\thv, exists := c.history.Get(key)\n\tif exists { \/\/ \u5b58\u5728\u6b77\u53f2 \u66f4\u65b0\u6b77\u53f2\n\t\tv := hv.(*lrukValue)\n\t\tv.Value = value\n\t\tv.K++\n\t\tif v.K == c.k {\n\t\t\t\/\/ \u5230\u9054 \u8a08\u6578 k \u5c07\u7de9\u5b58\u79fb\u52d5\u5230 lru\n\t\t\tc.lru.Put(key, v.Value)\n\t\t\tc.history.Delete(key)\n\t\t}\n\t\treturn\n\t}\n\n\t\/\/ \u4e0d\u5728\u6b77\u53f2 \u6dfb\u52a0\u5230\u6b77\u53f2\u4e2d\n\tc.history.Put(key, &amp;lrukValue{\n\t\tKey:   key,\n\t\tValue: value,\n\t\tK:     1,\n\t})\n}<\/code><\/pre>\n\n\n\n<p>\u6163\u4f8b\u5beb\u500b\u7c21\u55ae\u7684\u6e2c\u8a66\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package cache\n\nimport \"testing\"\n\nfunc TestLRUK2(t *testing.T) {\n\t\/\/ \u5b9a\u7fa9\u4e00\u500b\u8f14\u52a9\u51fd\u6578\u6e2c\u8a66\u7de9\u5b58\u4e2d\u5b58\u5728\u7684 key\n\tassertExists := func(t *testing.T, cache *LRU, key ...interface{}) {\n\t\tfor _, k := range key {\n\t\t\t_, exists := cache.keys&#91;k]\n\t\t\tif !exists {\n\t\t\t\tt.Fatalf(\"key not exists : %v\", k)\n\t\t\t}\n\t\t}\n\t}\n\n\tlru := NewLRU(3)\n\thistory := NewLRU(3)\n\tlruk2 := NewLRUK(lru, history, 2)\n\t\/\/ \u63d2\u5165 3 \u500b\u7de9\u5b58\n\t\/\/ history &#91;0,1,2]\n\t\/\/ lru &#91;]\n\tfor i := 0; i &lt; 3; i++ {\n\t\tlruk2.Put(i, i)\n\t}\n\t\/\/ \u9a57\u8b49 \u7de9\u5b58\u5b58\u5728\n\tassertExists(t, history, 0, 1, 2)\n\n\t\/\/ \u8a2a\u554f 1 \u6b64\u5f8c key 1 \u8a08\u6578\u5230\u9054 k \u88ab\u79fb\u52d5\u5230 lru\n\t\/\/ history &#91;0,2]\n\t\/\/ lru &#91;1]\n\tlruk2.Get(1)\n\tassertExists(t, history, 0, 2)\n\tassertExists(t, lru, 1)\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b8c\u6574\u4ee3\u78bc<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>package cache\n\n\/\/ \u7232\u7de9\u5b58\u6578\u64da\u5b9a\u7fa9\u4e00\u500b\u985e\u578b\ntype lrukValue struct {\n\tKey   interface{}\n\tValue interface{}\n\tK     int\n}\n\n\/\/ LRU \u7232\u7de9\u5b58\u5b9a\u7fa9\u4e00\u500b\u81ea\u5b9a\u7fa9\u985e\u578b\ntype LRUK struct {\n\t\/\/ lru \u7de9\u5b58\n\tlru *LRU\n\t\/\/ \u8a2a\u554f\u6b77\u53f2\u8a18\u9304\n\thistory Cache\n\t\/\/ \u8a2a\u554f\u5230\u9054\u591a\u5c11\u6b21\u52a0\u5165 lru\n\tk int\n}\n\n\/\/ \u56e0\u7232golang\u6c92\u6709\u69cb\u9020\u51fd\u6578 \u63d0\u4f9b\u4e00\u500b New \u51fd\u6578 \u5275\u5efa\u7de9\u5b58\nfunc NewLRUK(lru *LRU, history Cache, k int) Cache {\n\tif lru == nil {\n\t\tpanic(`lru not supported nil`)\n\t}\n\tif k &lt; 1 {\n\t\tpanic(`lruk's k must &gt; 0`)\n\t} else if k == 1 {\n\t\treturn lru\n\t}\n\tif history == nil {\n\t\tpanic(`history not supported nil`)\n\t}\n\n\treturn &amp;LRUK{\n\t\tlru:     lru,\n\t\thistory: history,\n\t\tk:       k,\n\t}\n}\nfunc (c *LRUK) Delete(key interface{}) {\n\tc.lru.Delete(key)\n\tc.history.Delete(key)\n}\nfunc (c *LRUK) Get(key interface{}) (value interface{}, exists bool) {\n\tvalue, exists = c.lru.Get(key)\n\tif exists {\n\t\t\/\/ \u5b58\u5728 \u8fd4\u56de \u7de9\u5b58\n\t\treturn\n\t}\n\t\/\/ \u4e0d\u5b58\u5728 \u67e5\u8a62 \u662f\u5426\u6709\u6b77\u53f2\u8a18\u9304\n\thk, exists := c.history.Get(key)\n\tif !exists {\n\t\t\/\/ \u4e0d\u5b58\u5728\u76f4\u63a5\u8fd4\u56de\n\t\treturn\n\t}\n\t\/\/ \u5b58\u5728\u8a18\u9304 \u8a2d\u7f6e\u8fd4\u56de\u503c\n\tv := hk.(*lrukValue)\n\tvalue = v.Value\n\n\t\/\/ \u589e\u52a0\u8a2a\u554f\u8a18\u9304\n\tv.K++\n\tif v.K == c.k {\n\t\t\/\/ \u5230\u9054 \u8a08\u6578 k \u5c07\u7de9\u5b58\u79fb\u52d5\u5230 lru\n\t\tc.lru.Put(key, v.Value)\n\t\tc.history.Delete(key)\n\t}\n\treturn\n}\nfunc (c *LRUK) Put(key, value interface{}) {\n\t_, exists := c.lru.Get(key)\n\tif exists { \/\/\u5df2\u7d93\u5b58\u5728\u7de9\u5b58\u4e2d \u66f4\u65b0\u503c\u5373\u53ef\n\t\tc.lru.Put(key, value)\n\t\treturn\n\t}\n\thv, exists := c.history.Get(key)\n\tif exists { \/\/ \u5b58\u5728\u6b77\u53f2 \u66f4\u65b0\u6b77\u53f2\n\t\tv := hv.(*lrukValue)\n\t\tv.Value = value\n\t\tv.K++\n\t\tif v.K == c.k {\n\t\t\t\/\/ \u5230\u9054 \u8a08\u6578 k \u5c07\u7de9\u5b58\u79fb\u52d5\u5230 lru\n\t\t\tc.lru.Put(key, v.Value)\n\t\t\tc.history.Delete(key)\n\t\t}\n\t\treturn\n\t}\n\n\t\/\/ \u4e0d\u5728\u6b77\u53f2 \u6dfb\u52a0\u5230\u6b77\u53f2\u4e2d\n\tc.history.Put(key, &amp;lrukValue{\n\t\tKey:   key,\n\t\tValue: value,\n\t\tK:     1,\n\t})\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2q<\/h2>\n\n\n\n<p>2q \u7b97\u6cd5\u5176\u5be6\u662f lru-2 \u7684\u4e00\u7a2e\uff0c\u6240\u4ee5\u4e0a\u8ff0\u6e2c\u8a66\u4ee3\u78bc\u5176\u5be6\u5c31\u7b97\u662f 2q\uff0c2q \u53732\u500b\u968a\u5217\u7684\u610f\u601d\uff0c\u6578\u64da\u9996\u6b21\u51fa\u73fe\u5c07\u5176\u7de9\u5b58\u5230\u968a\u52171\u4e2d\uff0c\u7576\u6578\u64da\u518d\u6b21\u88ab\u4f7f\u7528\u5c07\u5176\u5f9e\u968a\u52171\u79fb\u52d5\u5230\u968a\u52172\u4e2d\uff0c\u5169\u500b\u968a\u5217\u6309\u7167\u5404\u81ea\u7684\u5be6\u73fe\u6dfb\u52a0\u548c\u6dd8\u6c70\u7de9\u5b58\u3002\u6240\u4ee5\u7576 lruk \u7684 k \u7232 2 \u6642\u5c31\u662f\u4e00\u7a2e 2q \u7b97\u6cd5\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\u5982\u679c\u4e0d\u4f7f\u7528 lruk \u55ae\u7368\u5be6\u73fe 2q \u7684\u8a71\uff0c\u7de9\u5b58\u689d\u76ee\u4e0d\u9700\u8981\u591a\u4e00\u500b\u5c6c\u6027\u4fdd\u5b58\u6b77\u53f2\u8a2a\u554f\u6b21\u6578\uff0c\u53ef\u4ee5\u7bc0\u7701\u4e00\u9ede\u5167\u5b58\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">lruk \u548c 2q \u7684\u554f\u984c<\/h2>\n\n\n\n<p>lruk \u548c 2q \u9762\u81e8\u540c\u6a23\u7684\u554f\u984c\uff0c\u5373\u73fe\u5728\u6709\u5169\u500b\u968a\u5217\uff0c\u6240\u4ee5\u9700\u8981\u7232\u5169\u500b\u968a\u5217\u914d\u7f6e\u5bb9\u91cf\uff0c\u6bcf\u500b\u968a\u5217\u5bb9\u91cf\u591a\u5c11\u5c07\u5f88\u96e3\u628a\u63e1\u3002\u6b64\u5916\u6bcf\u6b21 Get Put Delete \u9700\u8981\u8abf\u6574\u5169\u500b\u968a\u5217\u4e5f\u6bd4\u53ea\u4f7f\u7528\u4e00\u500b\u968a\u5217\u7684\u7d50\u69cb\u591a\u8017\u8cbb\u5c11\u8a31\u8cc7\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>lruk \u7de9\u5b58\u7b97\u6cd5 \u4ecb\u7d39 \u4ee5\u53ca golang \u5be6\u73fe\u793a\u4f8b<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[11,14,2,13],"tags":[23,21,12,19,20],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-golang","category-re0-cache","category-code","category-algorithm","tag-2q","tag-cache","tag-golang","tag-lru-k","tag-lruk"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":6}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.king011.com\/index.php?rest_route=\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.king011.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.king011.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.king011.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.king011.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=127"}],"version-history":[{"count":7,"href":"https:\/\/blog.king011.com\/index.php?rest_route=\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":138,"href":"https:\/\/blog.king011.com\/index.php?rest_route=\/wp\/v2\/posts\/127\/revisions\/138"}],"wp:attachment":[{"href":"https:\/\/blog.king011.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.king011.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.king011.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}