{"id":4957,"date":"2020-04-10T18:22:59","date_gmt":"2020-04-10T10:22:59","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=4957"},"modified":"2020-04-10T18:22:59","modified_gmt":"2020-04-10T10:22:59","slug":"go-%e8%af%ad%e8%a8%80%e5%ae%9e%e7%8e%b0-lfu-%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=4957","title":{"rendered":"Go \u8bed\u8a00\u5b9e\u73b0 LFU \u7b97\u6cd5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">LFU\uff08Least Frequently Used \uff0c\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7b97\u6cd5\uff09\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7f13\u5b58\u7b97\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u987e\u540d\u601d\u4e49\uff0cLFU\u7b97\u6cd5\u7684\u601d\u60f3\u662f\uff1a\u5982\u679c\u4e00\u4e2a\u6570\u636e\u5728\u6700\u8fd1\u4e00\u6bb5\u65f6\u95f4\u5f88\u5c11\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\u5c0f\u9891\u7387\u8bbf\u95ee\u7684\u6570\u636e\u6700\u5148\u88ab\u6dd8\u6c70\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">LFU \u7b97\u6cd5\u7684\u63cf\u8ff0\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\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<ul 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\u8bbf\u95ee\u9891\u7387\u6700\u4f4e\u7684\u6570\u636e\u7f6e\u6362\u6389\u3002<\/li><li>get(key)\uff1a\u8fd4\u56dekey\u5bf9\u5e94\u7684value\u503c\u3002<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b97\u6cd5\u5b9e\u73b0\u7b56\u7565\uff1a\u8003\u8651\u5230 LFU \u4f1a\u6dd8\u6c70\u8bbf\u95ee\u9891\u7387\u6700\u5c0f\u7684\u6570\u636e\uff0c\u6211\u4eec\u9700\u8981\u4e00\u79cd\u5408\u9002\u7684\u65b9\u6cd5\u6309\u5927\u5c0f\u987a\u5e8f\u7ef4\u62a4\u6570\u636e\u8bbf\u95ee\u7684\u9891\u7387\u3002LFU \u7b97\u6cd5\u672c\u8d28\u4e0a\u53ef\u4ee5\u770b\u505a\u662f\u4e00\u4e2a top K \u95ee\u9898(K = 1)\uff0c\u5373\u9009\u51fa\u9891\u7387\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u56e0\u6b64\u6211\u4eec\u5f88\u5bb9\u6613\u60f3\u5230\u53ef\u4ee5\u7528\u4e8c\u9879\u5806\u6765\u9009\u62e9\u9891\u7387\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u8fd9\u6837\u7684\u5b9e\u73b0\u6bd4\u8f83\u9ad8\u6548\u3002\u6700\u7ec8\u5b9e\u73b0\u7b56\u7565\u4e3a\u5c0f\u9876\u5806+\u54c8\u5e0c\u8868\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/gitee.com\/xmtblog\/img\/raw\/master\/go_lfu.png\" alt=\"\" \/><figcaption>LFU golang \u5b9e\u73b0<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>package problem0460\n\n\/\/ LRU: Least Recently Used\uff0c\u7f13\u5b58\u6ee1\u7684\u65f6\u5019\uff0c\u5220\u9664\u7f13\u5b58\u91cc\u6700\u4e45\u672a\u4f7f\u7528\u7684\u6570\u636e\uff0c\u7136\u540e\u653e\u5165\u65b0\u5143\u7d20\n\/\/ LFU: Least Frequently Used\uff0c\u7f13\u5b58\u6ee1\u7684\u65f6\u5019\uff0c\u5220\u9664\u7f13\u5b58\u91cc\u4f7f\u7528\u6b21\u6570\u6700\u5c11\u7684\u5143\u7d20\uff0c\u7136\u540e\u653e\u5165\u65b0\u5143\u7d20\uff0c\u5982\u679c\u4f7f\u7528\u9891\u7387\u4e00\u6837\uff0c\u5220\u9664\u7f13\u5b58\u6700\u4e45\u7684\u5143\u7d20\n\n\/\/ \u8282\u70b9\uff1a\u5305\u542bkey, value, frequent\u8bbf\u95ee\u6b21\u6570, pre\u524d\u9a71\u6307\u9488, next\u540e\u7ee7\u6307\u9488\ntype Node struct {\n  key, value, frequent int\n  pre, next            *Node\n}\n\n\/\/ \u53cc\u5411\u94fe\u8868\uff1a\u5305\u542bhead\u5934\u6307\u9488, tail\u5c3e\u6307\u9488, size\u5c3a\u5bf8\ntype ListNode struct {\n  head, tail *Node\n  size       int\n}\n\n\/\/ \u53cc\u5411\u94fe\u8868\u8f85\u52a9\u51fd\u6570\uff1a\u6dfb\u52a0\u4e00\u4e2a\u8282\u70b9\u5230\u5934\u8282\u70b9\u540e\nfunc (this *ListNode) addNode(node *Node) {\n  head := this.head\n  node.next = head.next\n  node.next.pre = node\n  node.pre = head\n  head.next = node\n}\n\n\/\/ \u53cc\u5411\u94fe\u8868\u8f85\u52a9\u51fd\u6570\uff0c\u5220\u9664\u4e00\u4e2a\u8282\u70b9\nfunc (this *ListNode) removeNode(node *Node) {\n  node.pre.next = node.next\n  node.next.pre = node.pre\n}\n\n\/\/ LFUCache\u7ed3\u6784\uff1a\u5305\u542bcapacity\u5bb9\u91cf, size\u5f53\u524d\u5bb9\u91cf, minFrequent\u5f53\u524d\u6700\u5c11\u8bbf\u95ee\u9891\u6b21, cacheMap\u7f13\u5b58\u54c8\u5e0c\u8868, frequentMap\u9891\u6b21\u54c8\u5e0c\u8868\n\/\/ minFrequent\u5f53\u524d\u6700\u5c11\u8bbf\u95ee\u9891\u6b21\uff1a\n\/\/ 1. \u63d2\u5165\u4e00\u4e2a\u65b0\u8282\u70b9\u65f6\uff0c\u4e4b\u524d\u80af\u5b9a\u6ca1\u8bbf\u95ee\u8fc7\uff0cminFrequent = 1\n\/\/ 2. put\u548cget\u65f6\uff0c\u5982\u679c\u79fb\u9664\u540e\u53cc\u5411\u94fe\u8868\u8282\u70b9\u4e2a\u6570\u4e3a0\uff0c\u4e14\u6070\u597d\u662f\u6700\u5c0f\u8bbf\u95ee\u94fe\u8868, minFrequent++\ntype LFUCache struct {\n  capacity, size, minFrequent int\n  cacheMap                    map[int]*Node\n  frequentMap                 map[int]*ListNode\n}\n\nfunc Constructor(capacity int) LFUCache {\n  return LFUCache{\n    capacity:    capacity,\n    size:        0,\n    minFrequent: 0,\n    cacheMap:    make(map[int]*Node),\n    frequentMap: make(map[int]*ListNode),\n  }\n}\n\n\/\/ LFUCache\u8f85\u52a9\u51fd\u6570\uff1a\u5c06\u8282\u70b9\u4ece\u5bf9\u5e94\u7684\u9891\u6b21\u53cc\u5411\u94fe\u8868\u4e2d\u5220\u9664\nfunc (this *LFUCache) remove(node *Node) {\n  this.frequentMap[node.frequent].removeNode(node)\n  this.frequentMap[node.frequent].size--\n}\n\n\/\/ LFUCache\u8f85\u52a9\u51fd\u6570\uff1a\u5c06\u8282\u70b9\u6dfb\u52a0\u8fdb\u5bf9\u5e94\u7684\u9891\u6b21\u53cc\u5411\u94fe\u8868\uff0c\u6ca1\u6709\u5219\u521b\u5efa\nfunc (this *LFUCache) add(node *Node) {\n  if listNode, exist := this.frequentMap[node.frequent]; exist {\n    listNode.addNode(node)\n    listNode.size++\n  } else {\n    listNode = &amp;ListNode{&amp;Node{}, &amp;Node{}, 0}\n    listNode.head.next = listNode.tail\n    listNode.tail.pre = listNode.head\n    listNode.addNode(node)\n    listNode.size++\n    this.frequentMap[node.frequent] = listNode\n  }\n}\n\n\/\/ LFUCache\u8f85\u52a9\u51fd\u6570\uff1a\u79fb\u9664\u4e00\u4e2akey\nfunc (this *LFUCache) evictNode() {\n  listNode := this.frequentMap[this.minFrequent]\n  delete(this.cacheMap, listNode.tail.pre.key)\n  listNode.removeNode(listNode.tail.pre)\n  listNode.size--\n}\n\n\/\/ LFUCache\u8f85\u52a9\u51fd\u6570\uff1a\u83b7\u53d6\u4e00\u4e2akey\u548c\u4fee\u6539\u4e00\u4e2akey\u90fd\u4f1a\u589e\u52a0\u5bf9\u5e94key\u7684\u8bbf\u95ee\u9891\u6b21\uff0c\u53ef\u4ee5\u72ec\u7acb\u4e3a\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5b8c\u6210\u5982\u4e0b\u4efb\u52a1\uff1a\n\/\/ 1. \u5c06\u5bf9\u5e94node\u4ece\u9891\u6b21\u5217\u8868\u4e2d\u79fb\u51fa\n\/\/ 2. \u7ef4\u62a4minFrequent\n\/\/ 3. \u8be5\u8282\u70b9\u8bbf\u95ee\u9891\u6b21++\uff0c\u79fb\u52a8\u8fdb\u4e0b\u4e00\u4e2a\u8bbf\u95ee\u9891\u6b21\u94fe\u8868\nfunc (this *LFUCache) triggerVisit(node *Node) {\n  this.remove(node)\n  if node.frequent == this.minFrequent &amp;&amp; this.frequentMap[node.frequent].size == 0 {\n    this.minFrequent++\n  }\n  node.frequent++\n  this.add(node)\n}\n\nfunc (this *LFUCache) Get(key int) int {\n  if node, exist := this.cacheMap[key]; exist {\n    this.triggerVisit(node)\n    return node.value\n  }\n  return -1\n}\n\nfunc (this *LFUCache) Put(key int, value int) {\n  if this.capacity == 0 {\n    return\n  }\n  if node, exist := this.cacheMap[key]; exist {\n    this.triggerVisit(node)\n    this.cacheMap[key].value = value\n  } else {\n    newNode := &amp;Node{key, value, 1, nil, nil}\n    if this.size &lt; this.capacity {\n      this.add(newNode)\n      this.size++\n      this.minFrequent = 1\n    } else {\n      this.evictNode()\n      this.add(newNode)\n      this.minFrequent = 1\n    }\n    this.cacheMap[key] = newNode\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee3\u7801\u52a0\u6ce8\u91ca\uff0c\u76f8\u4fe1\u5927\u5bb6\u90fd\u80fd\u4e00\u76ee\u4e86\u7136\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=4957\">Go \u8bed\u8a00\u5b9e\u73b0 LFU \u7b97\u6cd5<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>LFU\uff08Least Frequently Used \uff0c\u6700\u8fd1\u6700\u5c11\u4f7f\u7528\u7b97\u6cd5\uff09\u4e5f\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7f13\u5b58\u7b97\u6cd5\u3002 \u987e\u540d\u601d\u4e49\uff0cL [&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":[1942,1654,75,849,584],"class_list":["post-4957","post","type-post","status-publish","format-standard","hentry","category-yyzt","tag-goaccess","tag-go","tag-75","tag-849","tag-584"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4957","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=4957"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4957\/revisions"}],"predecessor-version":[{"id":4958,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/4957\/revisions\/4958"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}