{"id":167773,"date":"2024-11-06T05:45:25","date_gmt":"2024-11-05T21:45:25","guid":{"rendered":"https:\/\/server.hk\/cnblog\/167773\/"},"modified":"2024-11-06T05:45:26","modified_gmt":"2024-11-05T21:45:26","slug":"%e5%88%a9%e7%94%a8redis%e7%aa%81%e7%a0%b4%e7%b3%bb%e7%b5%b1%e9%ab%98%e4%b8%a6%e7%99%bc%e7%93%b6%e9%a0%b8%ef%bc%88redis%e8%a7%a3%e6%b1%ba%e7%b3%bb%e7%b5%b1%e9%ab%98%e4%b8%a6%e7%99%bc%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/167773\/","title":{"rendered":"\u5229\u7528Redis\u7a81\u7834\u7cfb\u7d71\u9ad8\u4e26\u767c\u74f6\u9838\uff08redis\u89e3\u6c7a\u7cfb\u7d71\u9ad8\u4e26\u767c\uff09"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8redis%e7%aa%81%e7%a0%b4%e7%b3%bb%e7%b5%b1%e9%ab%98%e4%b8%a6%e7%99%bc%e7%93%b6%e9%a0%b8-CcxghpCOFf\">\u5229\u7528Redis\u7a81\u7834\u7cfb\u7d71\u9ad8\u4e26\u767c\u74f6\u9838<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u7cfb\u7d71\u7684\u9ad8\u4e26\u767c\u8655\u7406\u80fd\u529b\u5c0d\u65bc\u4f01\u696d\u7684\u6210\u529f\u81f3\u95dc\u91cd\u8981\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\u548c\u6578\u64da\u91cf\u7684\u6fc0\u589e\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u548c\u8655\u7406\u5927\u91cf\u8acb\u6c42\u6210\u70ba\u4e86\u4e00\u500b\u6311\u6230\u3002Redis\uff0c\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u89e3\u6c7a\u65b9\u6848\u4f86\u61c9\u5c0d\u9019\u4e00\u6311\u6230\u3002\u672c\u6587\u5c07\u63a2\u8a0eRedis\u5982\u4f55\u5e6b\u52a9\u7cfb\u7d71\u7a81\u7834\u9ad8\u4e26\u767c\u74f6\u9838\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-CcxghpCOFf\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\uff08Remote Dictionary Server\uff09\u662f\u4e00\u500b\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u6578\u64da\u5eab\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u548c\u6709\u5e8f\u96c6\u5408\u3002\u7531\u65bc\u5176\u5728\u5167\u5b58\u4e2d\u64cd\u4f5c\u6578\u64da\u7684\u7279\u6027\uff0cRedis\u80fd\u5920\u63d0\u4f9b\u6975\u5feb\u7684\u8b80\u5beb\u901f\u5ea6\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u9ad8\u4e26\u767c\u61c9\u7528\u7684\u7406\u60f3\u9078\u64c7\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e9%ab%98%e4%b8%a6%e7%99%bc%e7%89%b9%e6%80%a7-CcxghpCOFf\">Redis\u7684\u9ad8\u4e26\u767c\u7279\u6027<\/h2>\n<p>Redis\u7684\u9ad8\u4e26\u767c\u8655\u7406\u80fd\u529b\u4e3b\u8981\u9ad4\u73fe\u5728\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li><strong>\u55ae\u7dda\u7a0b\u67b6\u69cb\uff1a<\/strong>Redis\u4f7f\u7528\u55ae\u7dda\u7a0b\u6a21\u578b\u4f86\u8655\u7406\u8acb\u6c42\uff0c\u9019\u610f\u5473\u8457\u5b83\u4e0d\u6703\u56e0\u70ba\u4e0a\u4e0b\u6587\u5207\u63db\u800c\u5c0e\u81f4\u6027\u80fd\u640d\u5931\u3002\u9019\u7a2e\u8a2d\u8a08\u4f7f\u5f97Redis\u80fd\u5920\u5728\u9ad8\u4e26\u767c\u60c5\u6cc1\u4e0b\u4fdd\u6301\u7a69\u5b9a\u7684\u6027\u80fd\u3002<\/li>\n<li><strong>\u5167\u5b58\u5b58\u5132\uff1a<\/strong>\u6240\u6709\u6578\u64da\u90fd\u5b58\u5132\u5728\u5167\u5b58\u4e2d\uff0c\u9019\u4f7f\u5f97\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u6975\u5feb\u3002\u6839\u64da\u5b98\u65b9\u6587\u6a94\uff0cRedis\u7684\u8b80\u53d6\u5ef6\u9072\u901a\u5e38\u5728\u5fae\u79d2\u7d1a\u5225\u3002<\/li>\n<li><strong>\u9ad8\u6548\u7684\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684\u6578\u64da\u7d50\u69cb\u4f86\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e7%9a%84%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-CcxghpCOFf\">Redis\u7684\u61c9\u7528\u5834\u666f<\/h2>\n<p>Redis\u5728\u9ad8\u4e26\u767c\u5834\u666f\u4e2d\u7684\u61c9\u7528\u975e\u5e38\u5ee3\u6cdb\uff0c\u4ee5\u4e0b\u662f\u5e7e\u500b\u5178\u578b\u7684\u4f8b\u5b50\uff1a<\/p>\n<h3 id=\"1-%e7%b7%a9%e5%ad%98%e7%b3%bb%e7%b5%b1-CcxghpCOFf\">1. \u7de9\u5b58\u7cfb\u7d71<\/h3>\n<p>\u5728\u9ad8\u4e26\u767c\u7684\u74b0\u5883\u4e2d\uff0c\u4f7f\u7528Redis\u4f5c\u70ba\u7de9\u5b58\u7cfb\u7d71\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u6578\u64da\u5eab\u7684\u8b80\u53d6\u58d3\u529b\u3002\u901a\u904e\u5c07\u983b\u7e41\u8a2a\u554f\u7684\u6578\u64da\u7de9\u5b58\u5728Redis\u4e2d\uff0c\u7cfb\u7d71\u53ef\u4ee5\u5feb\u901f\u97ff\u61c9\u7528\u6236\u8acb\u6c42\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002<\/p>\n<pre><code>SET user:1000 {\"name\": \"John\", \"age\": 30}\nGET user:1000\n<\/code><\/pre>\n<h3 id=\"2-%e8%a8%88%e6%95%b8%e5%99%a8-CcxghpCOFf\">2. \u8a08\u6578\u5668<\/h3>\n<p>Redis\u7684\u539f\u5b50\u64cd\u4f5c\u7279\u6027\u4f7f\u5176\u975e\u5e38\u9069\u5408\u7528\u4f5c\u8a08\u6578\u5668\u3002\u4f8b\u5982\uff0c\u7db2\u7ad9\u7684\u9ede\u64ca\u91cf\u3001\u8a2a\u554f\u91cf\u7b49\u90fd\u53ef\u4ee5\u4f7f\u7528Redis\u4f86\u5be6\u73fe\u9ad8\u6548\u7684\u8a08\u6578\u3002<\/p>\n<pre><code>INCR page:view:home\n<\/code><\/pre>\n<h3 id=\"3-%e6%b6%88%e6%81%af%e9%9a%8a%e5%88%97-CcxghpCOFf\">3. \u6d88\u606f\u968a\u5217<\/h3>\n<p>Redis\u7684\u5217\u8868\u7d50\u69cb\u53ef\u4ee5\u7528\u4f86\u5be6\u73fe\u6d88\u606f\u968a\u5217\uff0c\u652f\u6301\u9ad8\u4e26\u767c\u7684\u6d88\u606f\u8655\u7406\u3002\u9019\u5c0d\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u8acb\u6c42\u7684\u61c9\u7528\u4f86\u8aaa\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<pre><code>LPUSH task_queue \"task1\"\nRPOP task_queue\n<\/code><\/pre>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%84%aa%e5%8c%96redis%e6%80%a7%e8%83%bd-CcxghpCOFf\">\u5982\u4f55\u512a\u5316Redis\u6027\u80fd<\/h2>\n<p>\u5118\u7ba1Redis\u672c\u8eab\u5df2\u7d93\u975e\u5e38\u9ad8\u6548\uff0c\u4f46\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u4ecd\u7136\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u65b9\u5f0f\u9032\u4e00\u6b65\u512a\u5316\u6027\u80fd\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u6301\u4e45\u5316\uff1a<\/strong>\u6839\u64da\u9700\u6c42\u9078\u64c7RDB\u6216AOF\u6301\u4e45\u5316\u7b56\u7565\uff0c\u4ee5\u5e73\u8861\u6027\u80fd\u548c\u6578\u64da\u5b89\u5168\u6027\u3002<\/li>\n<li><strong>\u5408\u7406\u8a2d\u7f6e\u5167\u5b58\u9650\u5236\uff1a<\/strong>\u901a\u904e\u914d\u7f6emaxmemory\u4f86\u9650\u5236Redis\u7684\u5167\u5b58\u4f7f\u7528\uff0c\u4e26\u9078\u64c7\u5408\u9069\u7684\u9a45\u9010\u7b56\u7565\u3002<\/li>\n<li><strong>\u4f7f\u7528\u96c6\u7fa4\u6a21\u5f0f\uff1a<\/strong>\u5c0d\u65bc\u8d85\u5927\u898f\u6a21\u7684\u61c9\u7528\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528Redis\u96c6\u7fa4\u4f86\u5206\u6563\u8ca0\u8f09\uff0c\u63d0\u9ad8\u53ef\u7528\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-CcxghpCOFf\">\u7e3d\u7d50<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u80fd\u5920\u6709\u6548\u5730\u89e3\u6c7a\u7cfb\u7d71\u5728\u9ad8\u4e26\u767c\u60c5\u6cc1\u4e0b\u7684\u74f6\u9838\u554f\u984c\u3002\u901a\u904e\u5408\u7406\u7684\u61c9\u7528\u548c\u512a\u5316\uff0cRedis\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u7cfb\u7d71\u7684\u6027\u80fd\u548c\u97ff\u61c9\u901f\u5ea6\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u4e26\u767c\u8655\u7406\u7684\u4f01\u696d\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684\u6280\u8853\u67b6\u69cb\u81f3\u95dc\u91cd\u8981\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u4f86\u652f\u6301\u60a8\u7684Redis\u90e8\u7f72\uff0c\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u60a8\u7684\u696d\u52d9\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5229\u7528Redis\u6280\u8853\u7a81\u7834\u7cfb\u7d71\u9ad8\u4e26\u767c\u74f6\u9838\uff0c\u63d0\u5347\u6027\u80fd\u8207\u6548\u7387\uff0c\u5be6\u73fe\u6d41\u66a2\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-167773","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=167773"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167773\/revisions"}],"predecessor-version":[{"id":167774,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167773\/revisions\/167774"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=167773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=167773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=167773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}