{"id":4967,"date":"2020-04-13T18:13:12","date_gmt":"2020-04-13T10:13:12","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=4967"},"modified":"2020-04-13T18:13:12","modified_gmt":"2020-04-13T10:13:12","slug":"go-%e8%af%ad%e8%a8%80%e5%ae%9e%e7%8e%b0-lru-%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4967","title":{"rendered":"Go \u8bed\u8a00\u5b9e\u73b0 LRU \u7b97\u6cd5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">LRU\uff08The Least Recently Used\uff0c\u6700\u8fd1\u6700\u4e45\u672a\u4f7f\u7528\u7b97\u6cd5\uff09\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7f13\u5b58\u7b97\u6cd5\uff0c\u5728\u5f88\u591a\u5206\u5e03\u5f0f\u7f13\u5b58\u7cfb\u7edf\uff08\u5982Redis, Memcached\uff09\u4e2d\u90fd\u6709\u5e7f\u6cdb\u4f7f\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">LRU\u7b97\u6cd5\u7684\u601d\u60f3\u662f\uff1a\u5982\u679c\u4e00\u4e2a\u6570\u636e\u5728\u6700\u8fd1\u4e00\u6bb5\u65f6\u95f4\u6ca1\u6709\u88ab\u8bbf\u95ee\u5230\uff0c\u90a3\u4e48\u53ef\u4ee5\u8ba4\u4e3a\u5728\u5c06\u6765\u5b83\u88ab\u8bbf\u95ee\u7684\u53ef\u80fd\u6027\u4e5f\u5f88\u5c0f\u3002\u56e0\u6b64\uff0c\u5f53\u7a7a\u95f4\u6ee1\u65f6\uff0c\u6700\u4e45\u6ca1\u6709\u8bbf\u95ee\u7684\u6570\u636e\u6700\u5148\u88ab\u7f6e\u6362\uff08\u6dd8\u6c70\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">LRU\u7b97\u6cd5\u7684\u63cf\u8ff0\uff1a \u8bbe\u8ba1\u4e00\u79cd\u7f13\u5b58\u7ed3\u6784\uff0c\u8be5\u7ed3\u6784\u5728\u6784\u9020\u65f6\u786e\u5b9a\u5927\u5c0f\uff0c\u5047\u8bbe\u5927\u5c0f\u4e3a K\uff0c\u5e76\u6709\u4e24\u4e2a\u529f\u80fd\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>set(key,value)\uff1a\u5c06\u8bb0\u5f55(key,value)\u63d2\u5165\u8be5\u7ed3\u6784\u3002\u5f53\u7f13\u5b58\u6ee1\u65f6\uff0c\u5c06\u6700\u4e45\u672a\u4f7f\u7528\u7684\u6570\u636e\u7f6e\u6362\u6389\u3002<\/li><li>get(key)\uff1a\u8fd4\u56dekey\u5bf9\u5e94\u7684value\u503c\u3002<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Java \u5b9e\u73b0\uff1a\u6700\u6734\u7d20\u7684\u601d\u60f3\u5c31\u662f\u7528\u6570\u7ec4+\u65f6\u95f4\u6233\u7684\u65b9\u5f0f\uff0c\u4e0d\u8fc7\u8fd9\u6837\u505a\u6548\u7387\u8f83\u4f4e\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u53cc\u5411\u94fe\u8868\uff08LinkedList\uff09+\u54c8\u5e0c\u8868\uff08HashMap\uff09\u5b9e\u73b0\uff08\u94fe\u8868\u7528\u6765\u8868\u793a\u4f4d\u7f6e\uff0c\u54c8\u5e0c\u8868\u7528\u6765\u5b58\u50a8\u548c\u67e5\u627e\uff09\uff0c\u5728Java\u91cc\u6709\u5bf9\u5e94\u7684\u6570\u636e\u7ed3\u6784LinkedHashMap\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f\u9488\u5bf9 Go \u8bed\u8a00\u7684 LRU \u7b97\u6cd5\u5b9e\u73b0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package problem0146\n\n\/\/ \u53cc\u5411\u94fe\u8868\u7ed3\u6784\ntype LinkNode struct {\n\tkey, value int\n\tpre, next  *LinkNode\n}\n\n\/\/ LRU\u7ed3\u6784\ntype LRUCache struct {\n\tm          map[int]*LinkNode\n\tcap        int\n\thead, tail *LinkNode\n}\n\nfunc Constructor(capacity int) LRUCache {\n\thead := &amp;LinkNode{0, 0, nil, nil}\n\ttail := &amp;LinkNode{0, 0, nil, nil}\n\thead.next = tail\n\ttail.pre = head\n\treturn LRUCache{make(map[int]*LinkNode, capacity), capacity, head, tail}\n}\n\nfunc (this *LRUCache) moveToHead(node *LinkNode) {\n\tthis.removeNode(node)\n\tthis.addNode(node)\n}\n\nfunc (this *LRUCache) removeNode(node *LinkNode) {\n\tnode.pre.next = node.next\n\tnode.next.pre = node.pre\n}\n\nfunc (this *LRUCache) addNode(node *LinkNode) {\n\thead := this.head\n\tnode.next = head.next\n\tnode.next.pre = node\n\tnode.pre = head\n\thead.next = node\n}\n\n\/\/ \u5982\u679c\u6709\uff0c\u5c06\u8fd9\u4e2a\u5143\u7d20\u79fb\u52a8\u5230\u9996\u4f4d\u7f6e\uff0c\u8fd4\u56de\u503c\n\/\/ \u5982\u679c\u6ca1\u6709\uff0c\u8fd4\u56de-1\nfunc (this *LRUCache) Get(key int) int {\n\tif v, exist := this.m[key]; exist {\n\t\tthis.moveToHead(v)\n\t\treturn v.value\n\t} else {\n\t\treturn -1\n\t}\n}\n\n\/\/ \u5982\u679c\u5143\u7d20\u5b58\u5728\uff0c\u5c06\u5176\u79fb\u52a8\u5230\u6700\u524d\u9762\uff0c\u5e76\u66f4\u65b0\u503c\n\/\/ \u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\uff0c\u63d2\u5165\u5230\u5143\u7d20\u9996\uff0c\u653e\u5165map\uff08\u5224\u65ad\u5bb9\u91cf\uff09\nfunc (this *LRUCache) Put(key int, value int) {\n\ttail := this.tail\n\tcache := this.m\n\tif v, exist := cache[key]; exist {\n\t\tv.value = value\n\t\tthis.moveToHead(v)\n\t} else {\n\t\tv := &amp;LinkNode{key, value, nil, nil}\n\t\tif len(this.m) == this.cap {\n\t\t\tdelete(cache, tail.pre.key)\n\t\t\tthis.removeNode(tail.pre)\n\t\t}\n\t\tthis.addNode(v)\n\t\tcache[key] = v\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u601d\u8def\u6bd4\u4ee3\u7801\u66f4\u91cd\u8981\uff0c\u60f3\u901a\u539f\u7406\uff0c\u5b9e\u73b0\u8d77\u6765 so easy\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=4967\">Go \u8bed\u8a00\u5b9e\u73b0 LRU \u7b97\u6cd5<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>LRU\uff08The Least Recently Used\uff0c\u6700\u8fd1\u6700\u4e45\u672a\u4f7f\u7528\u7b97\u6cd5\uff09\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7f13\u5b58\u7b97\u6cd5\uff0c\u5728\u5f88\u591a\u5206\u5e03\u5f0f [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[2102,1942,1654,2046,1578],"class_list":["post-4967","post","type-post","status-publish","format-standard","hentry","category-yyzt","tag-go","tag-goaccess","tag-mongod","tag-mongodb"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4967"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4967\/revisions"}],"predecessor-version":[{"id":4968,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4967\/revisions\/4968"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}