{"id":159022,"date":"2020-09-22T09:35:27","date_gmt":"2020-09-22T01:35:27","guid":{"rendered":"http:\/\/4563.org\/?p=159022"},"modified":"2020-09-22T09:35:27","modified_gmt":"2020-09-22T01:35:27","slug":"go-%e8%b6%85%e7%ba%a7%e8%90%8c%e6%96%b0%ef%bc%8c%e6%b1%82%e9%97%ae-go-cache-%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=159022","title":{"rendered":"go \u8d85\u7ea7\u840c\u65b0\uff0c\u6c42\u95ee go-cache \u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  go \u8d85\u7ea7\u840c\u65b0\uff0c\u6c42\u95ee go-cache \u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hbolive <\/span>  <span><i><\/i> 2<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        \u80cc\u666f\uff1a\u521a\u8fc7\u5b8c go \u8bed\u6cd5\uff0c\u8fd8\u6ca1\u5165\u95e8\u90a3\u79cd\uff0c\u73b0\u5728\u6709\u4e2a go-cache \u7684\u4f8b\u5b50\uff0c\u6709 3 \u4e2a\u95ee\u9898\u8bf7\u6559\u5927\u5bb6<br \/>\u76ee\u7684\uff1a\u8bbe\u7f6e\u7f13\u5b58\uff0c\u5728\u7f13\u5b58\u672a\u8fc7\u671f\u65f6\u4ece\u7f13\u5b58\u8bfb\u53d6\u6570\u636e\uff0c\u5982\u679c\u8bfb\u53d6\u5931\u8d25\uff0c\u5219\u5c06\u7f13\u5b58\u5185\u5bb9\u5199\u5165\u7f13\u5b58\uff1b\u6700\u540e\u5c06\u5185\u5bb9\u6253\u5370\u51fa\u6765\u3002<\/p>\n<p>package main<\/p>\n<p>import (<br \/> &#8220;fmt&#8221;<br \/> &#8220;time&#8221;<\/p>\n<p> &#8220;github.com\/patrickmn\/go-cache&#8221;<br \/>)<\/p>\n<p>func main() {<br \/> var id cateId = 20<br \/> data := id.getCache()<br \/> fmt.Println(data)<br \/>}<\/p>\n<p>type cateId int<\/p>\n<p>func (id cateId) getCache() string {<br \/> cacheName := &#8220;cache_20&#8221;<br \/> \/\/cacheName := &#8220;cache_&#8221; + string(id)<br \/> c := cache.New(30*time.Second, 20*time.Second)<br \/> value, found := c.Get(cacheName)<br \/> if found {<br \/> return value.(string)<br \/> } else {<br \/> c.Set(cacheName, &#8220;testdata_20&#8221;, cache.DefaultExpiration)<br \/> return &#8220;testdata_200&#8221;<br \/> \/\/return &#8220;testdata_&#8221; + string(id) \/\/\u59cb\u7ec8\u8f93\u51fa testdata_<br \/> }<br \/>}<\/p>\n<p>\u95ee\u9898 1\uff1a\u7f16\u8bd1\u540e\u6267\u884c\u5f97\u5230\u7684\u7ed3\u679c\u59cb\u7ec8\u662f testdata_200\uff0c\u7f13\u5b58 30 \u79d2\u8fc7\u671f\uff0c\u7b2c\u4e00\u6b21\u6267\u884c\u5f97\u5230 testdata_200 \u6b63\u5e38\uff0c\u4e3a\u4ec0\u4e48\u7b2c\u4e8c\u6b21\u6267\u884c\u5f97\u5230\u7684\u4e0d\u662f\u7f13\u5b58\u6570\u636e testdata_20 \u5462\uff1f\u83ab\u975e go-cache \u5728\u7a0b\u5e8f\u8fd0\u884c\u7ed3\u675f\uff0c\u7f13\u5b58\u4e5f\u6d88\u5931\u4e86\uff1f<\/p>\n<p>\u95ee\u9898 2\uff1a\/\/return &#8220;testdata_&#8221; + string(id) \/\/\u59cb\u7ec8\u8f93\u51fa testdata_\uff0c\u800c\u4e0d\u662f\u8f93\u51fa testdata_20 \uff1f\u5728 windows \u547d\u4ee4\u884c\u7a97\u53e3 20 \u662f\u4e2a\u4e71\u7801\uff0c\u5176\u4ed6\u7ec8\u7aef\u4e0b\u8fd0\u884c\u76f4\u63a5\u6ca1\u6709<\/p>\n<p>\u95ee\u9898 3\uff1a\u6211\u8fd9\u6837\u5199\u7b26\u5408\u5927\u4f17\u4e60\u60ef\u5417\uff1f\u6bd5\u7adf\u8fd9\u4e2a\u5199\u6cd5\u8ddf\u6211\u4ee5\u524d\u7528 PHP\/Python \u5dee\u522b\u633a\u5927\u7684\u3002\u3002<\/p>\n<p>PHP \u4e3a\u4f8b\uff1a<br \/>$data = $this-&gt;_getCache(20);<\/p>\n<p>protected _getCache(cateId)<br \/>{<br \/> $data = Cache-&gt;get(&#8216;cacheName&#8217;);<br \/> if( !$data ){<br \/> $data = &#8216;\u4e00\u4e9b\u6570\u636e&#8217;<br \/> Cache-&gt;set(&#8216;cacheName&#8217;, $data);<br \/> }<br \/> return $data;<br \/>}<\/p>\n<p>\u5148\u8c22\u8c22\u5927\u5bb6\uff01      <\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3415905\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : rimutuyuan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. cache \u5b58\u50a8\u5728\u5185\u5b58\uff0c\u7a0b\u5e8f\u9000\u51fa\u540e\u5185\u5b58\u90fd\u91ca\u653e\u6389\u4e86\uff0ckey \u4e5f\u4e0d\u5b58\u5728\u4e86<br \/>\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u5b9e\u73b0\u4f60\u671f\u671b\u7684\u7ed3\u679c<br \/>data2 := id.getCache()<br \/>fmt.Println(data2)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3415906\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : hbolive <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @rimutuyuan \u8c22\u8c22\uff0c\u5df2\u7ecf\u641e\u6e05\u695a\u4e86\u4e86\uff0c\u7f13\u5b58\u968f\u7740\u8fd0\u884c\u7ed3\u675f\uff0c\u4e5f\u4f1a\u91ca\u653e\u6389\u3002<br \/>\u73b0\u5728\u52a0\u4e86\u4e2a for \u5faa\u73af\uff0c\u8ba9\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\uff0c\u4f46\u662f\u8fd8\u662f\u4e00\u76f4\u8f93\u51fa testdata_200\uff0c\u800c\u4e0d\u662f testdata_20\uff0c\u5c31\u50cf\u4e3b\u8d34\u91cc\u4e00\u6837\uff0c\u5f88\u5947\u602a\u3002<br \/>\u73b0\u5728 append \u4e86\u4e00\u4e0b\uff0c\u60f3\u7f13\u5b58\u540d\u5b57\u4e0e id \u5173\u8054\u8d77\u6765\uff0c\u7ed3\u679c CMD \u4e0b\u6267\u884c\uff0c\u8f93\u51fa\u7684\u5c31\u662f testdata_\u4e71\u7801\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3415907\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xkeyideal <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u65e2\u7136\u662f\u840c\u65b0\u5c31\u4e0d\u8bf4\u4e86\uff0c\u8fd9\u4e2a\u6c99\u96d5 cache \u5e93\u522b\u7528\uff0c\u6d41\u91cf\u5927\u4e86\uff0c\u4f60\u5c31\u53ef\u4ee5\u6536\u62fe\u5305\u88f9\u6eda\u86cb\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3415908\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : CoolSpring <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e71\u7801\u662f\u56e0\u4e3a string() \u5e76\u4e0d\u662f\u628a\u6570\u5b57\u8f6c\u6362\u6210\u5bf9\u5e94\u5b57\u7b26\u7684\u5f62\u5f0f\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u628a\u8fd9\u4e2a\u6570\u5b57\u5f53\u4f5c UTF-8 \u503c\u8f6c\u6362\u5230\u5bf9\u5e94\u7684\u5b57\u7b26<br \/>\u53ef\u4ee5\u7528 strconv.Itoa() \u6216\u8005 fmt.Sprintf()                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3415909\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : scukmh <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xkeyideal \u6709\u5148\u4f8b\u5417\uff1f\u5df2\u7ecf\u7528\u4e86\uff0c\u6709\u70b9\u5bb3\u6015\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3415910\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xkeyideal <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @scukmh \u8981\u5565\u5148\u4f8b\uff0c\u4e00\u4e2a cache \u90fd\u53bb\u627e\u5f00\u6e90\u7684\uff0c\u627e\u5c31\u627e\u4e86\uff0c\u4e5f\u770b\u770b\u6e90\u7801\u5440\uff0c\u8fd9\u4e2a\u88e4\u5b50\u7684\u8fc7\u671f\u64cd\u4f5c\u7b80\u76f4\u662f\u667a\u969c\uff0c\u6293\u7d27\u6362\u4e86\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3415911\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : hbolive <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @CoolSpring \u62b1\u6b49\uff0c\u72af\u4e86\u4e2a\u4f4e\u7ea7\u9519\u8bef\uff0c\u770b\u6765\u8fd8\u5f97\u8fc7\u51e0\u904d\u8bed\u6cd5<br \/>\u518d\u6b21 append \u4e86\uff0c\u5c31\u662f\u65e0\u6cd5\u83b7\u53d6\u7f13\u5b58\u6570\u636e                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>go \u8d85\u7ea7\u840c\u65b0\uff0c\u6c42\u95ee go-cac&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/159022"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=159022"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/159022\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=159022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=159022"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=159022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}