{"id":189120,"date":"2024-11-12T04:48:20","date_gmt":"2024-11-11T20:48:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/189120\/"},"modified":"2024-11-12T04:48:21","modified_gmt":"2024-11-11T20:48:21","slug":"%e6%8f%90%e5%8d%87%e6%95%b8%e6%93%9a%e5%ba%ab%e6%80%a7%e8%83%bdredis%e5%bf%ab%e5%8f%96%e5%8a%a0%e9%80%9f%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%8f%96%e7%9a%84%e7%a7%98%e8%a8%a3%ef%bc%88redis-%e8%ae%80","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/189120\/","title":{"rendered":"\u63d0\u5347\u6578\u64da\u5eab\u6027\u80fdRedis\u5feb\u53d6\u52a0\u901f\u6578\u64da\u5eab\u8b80\u53d6\u7684\u79d8\u8a23\uff08redis \u8b80\u53d6\u53ef\u4ee5\uff09"},"content":{"rendered":"<h1 id=\"%e6%8f%90%e5%8d%87%e6%95%b8%e6%93%9a%e5%ba%ab%e6%80%a7%e8%83%bd%ef%bc%9aredis%e5%bf%ab%e5%8f%96%e5%8a%a0%e9%80%9f%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%8f%96%e7%9a%84%e7%a7%98%e8%a8%a3-jWJNqVirNm\">\u63d0\u5347\u6578\u64da\u5eab\u6027\u80fd\uff1aRedis\u5feb\u53d6\u52a0\u901f\u6578\u64da\u5eab\u8b80\u53d6\u7684\u79d8\u8a23<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u6027\u80fd\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6574\u9ad4\u6548\u7387\u81f3\u95dc\u91cd\u8981\u3002\u96a8\u8457\u7528\u6236\u9700\u6c42\u7684\u589e\u52a0\uff0c\u5982\u4f55\u63d0\u5347\u6578\u64da\u5eab\u7684\u8b80\u53d6\u901f\u5ea6\u6210\u70ba\u4e86\u8a31\u591a\u958b\u767c\u8005\u548c\u7cfb\u7d71\u7ba1\u7406\u54e1\u9762\u81e8\u7684\u6311\u6230\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u6578\u64da\u5eab\u7684\u8b80\u53d6\u6027\u80fd\u3002\u672c\u6587\u5c07\u63a2\u8a0eRedis\u5feb\u53d6\u7684\u5de5\u4f5c\u539f\u7406\u53ca\u5176\u5728\u52a0\u901f\u6578\u64da\u5eab\u8b80\u53d6\u65b9\u9762\u7684\u61c9\u7528\u79d8\u8a23\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-jWJNqVirNm\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u901a\u5e38\u7528\u4f5c\u6578\u64da\u5eab\u3001\u5feb\u53d6\u548c\u6d88\u606f\u4ee3\u7406\u3002\u5b83\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\uff0c\u4e26\u4e14\u63d0\u4f9b\u4e86\u6301\u4e45\u5316\u529f\u80fd\uff0c\u80fd\u5920\u5c07\u6578\u64da\u4fdd\u5b58\u5728\u78c1\u789f\u4e0a\u3002\u7531\u65bc\u5176\u9ad8\u6548\u7684\u5167\u5b58\u64cd\u4f5c\uff0cRedis\u5728\u8655\u7406\u5927\u91cf\u8acb\u6c42\u6642\u8868\u73fe\u51fa\u8272\uff0c\u7279\u5225\u9069\u5408\u7528\u65bc\u5feb\u53d6\u5c64\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%bd%bf%e7%94%a8redis%e4%bd%9c%e7%82%ba%e5%bf%ab%e5%8f%96%ef%bc%9f-jWJNqVirNm\">\u70ba\u4ec0\u9ebc\u4f7f\u7528Redis\u4f5c\u70ba\u5feb\u53d6\uff1f<\/h2>\n<p>\u4f7f\u7528Redis\u4f5c\u70ba\u5feb\u53d6\u7684\u4e3b\u8981\u539f\u56e0\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u80fd\u5920\u5728\u5167\u5b58\u4e2d\u5feb\u901f\u8b80\u53d6\u548c\u5beb\u5165\u6578\u64da\uff0c\u9019\u4f7f\u5f97\u5b83\u7684\u8b80\u53d6\u901f\u5ea6\u6bd4\u50b3\u7d71\u6578\u64da\u5eab\u5feb\u5f97\u591a\u3002<\/li>\n<li><strong>\u6e1b\u5c11\u6578\u64da\u5eab\u8ca0\u64d4\uff1a<\/strong>\u901a\u904e\u5c07\u983b\u7e41\u8a2a\u554f\u7684\u6578\u64da\u5b58\u5132\u5728Redis\u4e2d\uff0c\u53ef\u4ee5\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u67e5\u8a62\u6b21\u6578\uff0c\u5f9e\u800c\u964d\u4f4e\u6578\u64da\u5eab\u7684\u8ca0\u64d4\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>Redis\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u6700\u5408\u9069\u7684\u6578\u64da\u5b58\u5132\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e5%bf%ab%e5%8f%96%e7%9a%84%e5%af%a6%e7%8f%be%e6%ad%a5%e9%a9%9f-jWJNqVirNm\">Redis\u5feb\u53d6\u7684\u5be6\u73fe\u6b65\u9a5f<\/h2>\n<p>\u8981\u6709\u6548\u5730\u4f7f\u7528Redis\u4f5c\u70ba\u6578\u64da\u5eab\u7684\u5feb\u53d6\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u95dc\u9375\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e7%a2%ba%e5%ae%9a%e5%bf%ab%e5%8f%96%e7%ad%96%e7%95%a5-jWJNqVirNm\">1. \u78ba\u5b9a\u5feb\u53d6\u7b56\u7565<\/h3>\n<p>\u5728\u5be6\u65bdRedis\u5feb\u53d6\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u78ba\u5b9a\u54ea\u4e9b\u6578\u64da\u61c9\u8a72\u88ab\u5feb\u53d6\u3002\u901a\u5e38\uff0c\u4ee5\u4e0b\u985e\u578b\u7684\u6578\u64da\u9069\u5408\u5feb\u53d6\uff1a<\/p>\n<ul>\n<li>\u983b\u7e41\u8a2a\u554f\u7684\u6578\u64da\uff0c\u4f8b\u5982\u7528\u6236\u8cc7\u6599\u3001\u7522\u54c1\u4fe1\u606f\u7b49\u3002<\/li>\n<li>\u8a08\u7b97\u6210\u672c\u9ad8\u7684\u67e5\u8a62\u7d50\u679c\uff0c\u4f8b\u5982\u805a\u5408\u67e5\u8a62\u3002<\/li>\n<li>\u4e0d\u7d93\u5e38\u8b8a\u66f4\u7684\u975c\u614b\u6578\u64da\u3002<\/li>\n<\/ul>\n<h3 id=\"2-%e8%a8%ad%e7%bd%aeredis%e5%bf%ab%e5%8f%96-jWJNqVirNm\">2. \u8a2d\u7f6eRedis\u5feb\u53d6<\/h3>\n<p>\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u96c6\u6210Redis\u5feb\u53d6\uff0c\u901a\u5e38\u9700\u8981\u5b89\u88dd\u76f8\u61c9\u7684Redis\u5ba2\u6236\u7aef\u5eab\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Python\u7684\u7c21\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230Redis\u670d\u52d9\u5668\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u8a2d\u7f6e\u5feb\u53d6\nr.set('user:1000', 'John Doe')\n\n# \u7372\u53d6\u5feb\u53d6\nuser = r.get('user:1000')\nprint(user.decode('utf-8'))  # \u8f38\u51fa: John Doe\n<\/code><\/pre>\n<h3 id=\"3-%e8%a8%ad%e5%ae%9a%e9%81%8e%e6%9c%9f%e6%99%82%e9%96%93-jWJNqVirNm\">3. \u8a2d\u5b9a\u904e\u671f\u6642\u9593<\/h3>\n<p>\u70ba\u4e86\u907f\u514d\u5feb\u53d6\u4e2d\u7684\u6578\u64da\u904e\u6642\uff0c\u61c9\u8a72\u70ba\u5feb\u53d6\u7684\u6578\u64da\u8a2d\u5b9a\u904e\u671f\u6642\u9593\u3002\u9019\u53ef\u4ee5\u901a\u904eRedis\u7684EXPIRE\u547d\u4ee4\u4f86\u5be6\u73fe\uff1a<\/p>\n<pre><code># \u8a2d\u5b9a\u904e\u671f\u6642\u9593\u70ba60\u79d2\nr.expire('user:1000', 60)\n<\/code><\/pre>\n<h3 id=\"4-%e7%9b%a3%e6%8e%a7%e5%92%8c%e8%aa%bf%e6%95%b4-jWJNqVirNm\">4. \u76e3\u63a7\u548c\u8abf\u6574<\/h3>\n<p>\u5b9a\u671f\u76e3\u63a7Redis\u7684\u6027\u80fd\u548c\u4f7f\u7528\u60c5\u6cc1\uff0c\u6839\u64da\u5be6\u969b\u9700\u6c42\u8abf\u6574\u5feb\u53d6\u7b56\u7565\u548c\u914d\u7f6e\uff0c\u4ee5\u78ba\u4fdd\u6700\u4f73\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-jWJNqVirNm\">\u7d50\u8ad6<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u986f\u8457\u63d0\u5347\u6578\u64da\u5eab\u7684\u8b80\u53d6\u6027\u80fd\u3002\u901a\u904e\u5408\u7406\u7684\u5feb\u53d6\u7b56\u7565\u3001\u6709\u6548\u7684\u6578\u64da\u5b58\u5132\u548c\u5b9a\u671f\u7684\u76e3\u63a7\u8abf\u6574\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5145\u5206\u767c\u63eeRedis\u7684\u512a\u52e2\uff0c\u6e1b\u5c11\u6578\u64da\u5eab\u7684\u8ca0\u64d4\uff0c\u63d0\u5347\u61c9\u7528\u7a0b\u5e8f\u7684\u97ff\u61c9\u901f\u5ea6\u3002\u5c0d\u65bc\u5e0c\u671b\u5728\u9999\u6e2f\u5c0b\u627e\u9ad8\u6548\u80fd\u89e3\u6c7a\u65b9\u6848\u7684\u7528\u6236\uff0c<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u548c<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u63d0\u4f9b\u4e86\u7406\u60f3\u7684\u74b0\u5883\u4f86\u5be6\u73fe\u9019\u4e9b\u6280\u8853\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63d0\u5347\u6578\u64da\u5eab\u6027\u80fd\uff0c\u63a2\u7d22Redis\u5feb\u53d6\u52a0\u901f\u6578\u64da\u5eab\u8b80\u53d6\u7684\u79d8\u8a23\uff0c\u8b93\u6578\u64da\u5b58\u53d6\u66f4\u8fc5\u901f\u9ad8\u6548\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-189120","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189120","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=189120"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189120\/revisions"}],"predecessor-version":[{"id":189121,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/189120\/revisions\/189121"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=189120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=189120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=189120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}