{"id":6346,"date":"2024-07-18T19:42:26","date_gmt":"2024-07-18T11:42:26","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6346"},"modified":"2024-07-18T19:42:26","modified_gmt":"2024-07-18T11:42:26","slug":"redis-%e4%bd%bf%e7%94%a8%e5%b8%b8%e8%a7%81%e9%94%99%e8%af%af%e5%8f%8a%e6%ad%a3%e7%a1%ae%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6346","title":{"rendered":"Redis \u4f7f\u7528\u5e38\u89c1\u9519\u8bef\u53ca\u6b63\u786e\u65b9\u6cd5"},"content":{"rendered":"<p>\u5728\u4e1a\u52a1\u4e2d\u4f7f\u7528 Redis \u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u548c\u6b63\u786e\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<\/p>\n<h3>1. \u6570\u636e\u6301\u4e45\u5316<\/h3>\n<h4>\u9519\u8bef<\/h4>\n<ul>\n<li><strong>\u672a\u5f00\u542f\u6570\u636e\u6301\u4e45\u5316<\/strong>\uff1a\u5f88\u591a\u7528\u6237\u5728\u4f7f\u7528 Redis \u65f6\uff0c\u5ffd\u7565\u4e86\u6570\u636e\u6301\u4e45\u5316\u8bbe\u7f6e\uff0c\u5bfc\u81f4\u7cfb\u7edf\u91cd\u542f\u6216\u5d29\u6e83\u65f6\u6570\u636e\u4e22\u5931\u3002<\/li>\n<li><strong>\u9519\u8bef\u7684\u6301\u4e45\u5316\u914d\u7f6e<\/strong>\uff1a\u4ec5\u4f9d\u8d56 RDB\uff08Redis DataBase\uff09\u5feb\u7167\u8fdb\u884c\u6301\u4e45\u5316\uff0c\u800c\u5ffd\u7565 AOF\uff08Append-Only File\uff09\u7684\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<h4>\u6b63\u786e\u65b9\u6cd5<\/h4>\n<ul>\n<li><strong>\u5f00\u542f\u5e76\u914d\u7f6e\u6570\u636e\u6301\u4e45\u5316<\/strong>\uff1a\n<ul>\n<li>\u542f\u7528 AOF \u6301\u4e45\u5316\u65b9\u5f0f\uff0c\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u8bb0\u5f55\u6bcf\u6b21\u5199\u64cd\u4f5c\uff0c\u786e\u4fdd\u6570\u636e\u7684\u9ad8\u53ef\u7528\u6027\u3002<\/li>\n<li>\u540c\u65f6\u914d\u7f6e RDB \u5feb\u7167\u4f5c\u4e3a\u5907\u4efd\u7b56\u7565\uff0c\u786e\u4fdd\u5728\u4e0d\u540c\u7684\u65f6\u95f4\u70b9\u4e0a\u6709\u6570\u636e\u5907\u4efd\u3002<\/li>\n<li>\u5728 <code>redis.conf<\/code> \u4e2d\u8fdb\u884c\u5982\u4e0b\u914d\u7f6e\uff1a<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"language-plaintext\">save 900 1\nsave 300 10\nsave 60 10000\nappendonly yes\nappendfilename &quot;appendonly.aof&quot;<\/code><\/pre>\n<h3>2. \u5185\u5b58\u7ba1\u7406<\/h3>\n<h4>\u9519\u8bef<\/h4>\n<ul>\n<li><strong>\u5185\u5b58\u6ea2\u51fa<\/strong>\uff1a\u6ca1\u6709\u9650\u5236 Redis \u4f7f\u7528\u7684\u6700\u5927\u5185\u5b58\uff0c\u5bfc\u81f4\u5185\u5b58\u8017\u5c3d\u3002<\/li>\n<li><strong>\u4e0d\u5408\u7406\u7684\u5185\u5b58\u56de\u6536\u7b56\u7565<\/strong>\uff1a\u9ed8\u8ba4\u914d\u7f6e\u6ca1\u6709\u9009\u62e9\u5408\u9002\u7684\u5185\u5b58\u56de\u6536\u7b56\u7565\uff08eviction policy\uff09\uff0c\u5bfc\u81f4\u6570\u636e\u88ab\u4e0d\u5408\u7406\u5730\u5220\u9664\u3002<\/li>\n<\/ul>\n<h4>\u6b63\u786e\u65b9\u6cd5<\/h4>\n<ul>\n<li><strong>\u8bbe\u7f6e\u6700\u5927\u5185\u5b58\u9650\u5236<\/strong>\uff1a\u786e\u4fdd Redis \u4e0d\u4f1a\u4f7f\u7528\u8d85\u8fc7\u6307\u5b9a\u7684\u5185\u5b58\u3002\u53ef\u4ee5\u5728 <code>redis.conf<\/code> \u4e2d\u914d\u7f6e <code>maxmemory<\/code>\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-plaintext\">maxmemory 2gb<\/code><\/pre>\n<ul>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u5185\u5b58\u56de\u6536\u7b56\u7565<\/strong>\uff1a\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5185\u5b58\u56de\u6536\u7b56\u7565\uff0c\u5982 <code>volatile-lru<\/code>\u3001<code>allkeys-lru<\/code> \u7b49\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-plaintext\">maxmemory-policy allkeys-lru<\/code><\/pre>\n<h3>3. \u6570\u636e\u5206\u7247\u548c\u96c6\u7fa4<\/h3>\n<h4>\u9519\u8bef<\/h4>\n<ul>\n<li><strong>\u5355\u8282\u70b9\u90e8\u7f72<\/strong>\uff1a\u5728\u9ad8\u5e76\u53d1\u548c\u5927\u6570\u636e\u91cf\u573a\u666f\u4e0b\uff0c\u4f7f\u7528\u5355\u8282\u70b9 Redis\uff0c\u5bfc\u81f4\u6027\u80fd\u74f6\u9888\u548c\u5355\u70b9\u6545\u969c\u3002<\/li>\n<li><strong>\u4e0d\u5408\u7406\u7684\u5206\u7247\u7b56\u7565<\/strong>\uff1a\u6570\u636e\u5206\u7247\u4e0d\u5747\u5300\uff0c\u5bfc\u81f4\u67d0\u4e9b\u8282\u70b9\u8d1f\u8f7d\u8fc7\u9ad8\u3002<\/li>\n<\/ul>\n<h4>\u6b63\u786e\u65b9\u6cd5<\/h4>\n<ul>\n<li><strong>\u4f7f\u7528 Redis \u96c6\u7fa4<\/strong>\uff1a\u5728\u9ad8\u5e76\u53d1\u548c\u5927\u6570\u636e\u91cf\u573a\u666f\u4e0b\uff0c\u4f7f\u7528 Redis \u96c6\u7fa4\u8fdb\u884c\u6570\u636e\u5206\u7247\u548c\u8d1f\u8f7d\u5747\u8861\u3002<\/li>\n<li><strong>\u5408\u7406\u7684\u5206\u7247\u7b56\u7565<\/strong>\uff1a\u786e\u4fdd\u6570\u636e\u5747\u5300\u5206\u5e03\u5728\u5404\u4e2a\u8282\u70b9\u4e0a\uff0c\u907f\u514d\u67d0\u4e9b\u8282\u70b9\u8d1f\u8f7d\u8fc7\u9ad8\u3002<\/li>\n<\/ul>\n<h3>4. \u9501\u673a\u5236<\/h3>\n<h4>\u9519\u8bef<\/h4>\n<ul>\n<li><strong>\u9519\u8bef\u4f7f\u7528\u5206\u5e03\u5f0f\u9501<\/strong>\uff1a\u5728\u5206\u5e03\u5f0f\u73af\u5883\u4e0b\u9519\u8bef\u5730\u4f7f\u7528 Redis \u9501\uff0c\u5bfc\u81f4\u9501\u673a\u5236\u5931\u6548\u3002<\/li>\n<li><strong>\u672a\u8003\u8651\u9501\u7684\u8d85\u65f6\u673a\u5236<\/strong>\uff1a\u4f7f\u7528\u9501\u65f6\uff0c\u6ca1\u6709\u8bbe\u7f6e\u9501\u7684\u8d85\u65f6\u673a\u5236\uff0c\u5bfc\u81f4\u6b7b\u9501\u3002<\/li>\n<\/ul>\n<h4>\u6b63\u786e\u65b9\u6cd5<\/h4>\n<ul>\n<li><strong>\u4f7f\u7528 Redlock \u7b97\u6cd5<\/strong>\uff1aRedlock \u662f Redis \u5b98\u65b9\u63a8\u8350\u7684\u5206\u5e03\u5f0f\u9501\u7b97\u6cd5\uff0c\u80fd\u66f4\u597d\u5730\u4fdd\u8bc1\u9501\u7684\u6709\u6548\u6027\u548c\u5b89\u5168\u6027\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u9501\u7684\u8d85\u65f6\u65f6\u95f4<\/strong>\uff1a\u786e\u4fdd\u5728\u83b7\u53d6\u9501\u65f6\u8bbe\u7f6e\u5408\u7406\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u907f\u514d\u6b7b\u9501\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<h3>5. \u6570\u636e\u7ed3\u6784\u9009\u62e9<\/h3>\n<h4>\u9519\u8bef<\/h4>\n<ul>\n<li><strong>\u4f7f\u7528\u4e0d\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784<\/strong>\uff1a\u6ca1\u6709\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5bfc\u81f4\u5185\u5b58\u6d6a\u8d39\u548c\u6027\u80fd\u4e0b\u964d\u3002<\/li>\n<\/ul>\n<h4>\u6b63\u786e\u65b9\u6cd5<\/h4>\n<ul>\n<li><strong>\u6839\u636e\u9700\u6c42\u9009\u62e9\u6570\u636e\u7ed3\u6784<\/strong>\uff1a\u4e86\u89e3 Redis \u63d0\u4f9b\u7684\u5404\u79cd\u6570\u636e\u7ed3\u6784\uff08\u5982 String\u3001List\u3001Set\u3001Hash\u3001ZSet\uff09\uff0c\u6839\u636e\u5177\u4f53\u7684\u4e1a\u52a1\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<\/ul>\n<h3>6. \u70ed\u70b9\u6570\u636e<\/h3>\n<h4>\u9519\u8bef<\/h4>\n<ul>\n<li><strong>\u6ca1\u6709\u5904\u7406\u70ed\u70b9\u6570\u636e<\/strong>\uff1a\u67d0\u4e9b\u6570\u636e\u88ab\u9891\u7e41\u8bbf\u95ee\u6216\u66f4\u65b0\uff0c\u5bfc\u81f4\u8be5\u6570\u636e\u6240\u5728\u7684\u8282\u70b9\u8d1f\u8f7d\u8fc7\u9ad8\u3002<\/li>\n<\/ul>\n<h4>\u6b63\u786e\u65b9\u6cd5<\/h4>\n<ul>\n<li><strong>\u5408\u7406\u7f13\u5b58\u548c\u5206\u7247<\/strong>\uff1a\u5bf9\u70ed\u70b9\u6570\u636e\u8fdb\u884c\u5408\u7406\u7684\u7f13\u5b58\u548c\u5206\u7247\uff0c\u5fc5\u8981\u65f6\u53ef\u4ee5\u4f7f\u7528\u591a\u7ea7\u7f13\u5b58\u7b56\u7565\u3002<\/li>\n<li><strong>\u6570\u636e\u9884\u70ed<\/strong>\uff1a\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\uff0c\u5bf9\u70ed\u70b9\u6570\u636e\u8fdb\u884c\u9884\u70ed\uff0c\u907f\u514d\u9ad8\u5cf0\u671f\u5927\u91cf\u8bf7\u6c42\u76f4\u63a5\u6253\u5230\u6570\u636e\u5e93\u3002<\/li>\n<\/ul>\n<h3>7. \u76d1\u63a7\u548c\u62a5\u8b66<\/h3>\n<h4>\u9519\u8bef<\/h4>\n<ul>\n<li><strong>\u7f3a\u4e4f\u76d1\u63a7<\/strong>\uff1a\u6ca1\u6709\u5bf9 Redis \u8fdb\u884c\u5b9e\u65f6\u76d1\u63a7\uff0c\u5bfc\u81f4\u95ee\u9898\u65e0\u6cd5\u53ca\u65f6\u53d1\u73b0\u548c\u89e3\u51b3\u3002<\/li>\n<\/ul>\n<h4>\u6b63\u786e\u65b9\u6cd5<\/h4>\n<ul>\n<li><strong>\u542f\u7528\u76d1\u63a7\u548c\u62a5\u8b66<\/strong>\uff1a\u4f7f\u7528\u76d1\u63a7\u5de5\u5177\uff08\u5982 Prometheus\u3001Grafana\u3001Redis Sentinel \u7b49\uff09\u5bf9 Redis \u8fdb\u884c\u5b9e\u65f6\u76d1\u63a7\uff0c\u8bbe\u7f6e\u5408\u7406\u7684\u62a5\u8b66\u7b56\u7565\uff0c\u786e\u4fdd\u95ee\u9898\u80fd\u53ca\u65f6\u53d1\u73b0\u548c\u89e3\u51b3\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u907f\u514d\u4ee5\u4e0a\u5e38\u89c1\u9519\u8bef\u5e76\u91c7\u53d6\u76f8\u5e94\u7684\u6b63\u786e\u65b9\u6cd5\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8 Redis \u5728\u4e1a\u52a1\u4e2d\u7684\u53ef\u9760\u6027\u548c\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4e1a\u52a1\u4e2d\u4f7f\u7528 Redis \u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u548c\u6b63\u786e\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a 1. \u6570\u636e\u6301\u4e45\u5316 \u9519\u8bef \u672a\u5f00\u542f\u6570\u636e&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6346\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[122],"class_list":["post-6346","post","type-post","status-publish","format-standard","hentry","category-123","tag-redis"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6346"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6346\/revisions"}],"predecessor-version":[{"id":6347,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6346\/revisions\/6347"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}