{"id":185652,"date":"2024-11-10T23:22:19","date_gmt":"2024-11-10T15:22:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/185652\/"},"modified":"2024-11-10T23:22:20","modified_gmt":"2024-11-10T15:22:20","slug":"%e6%89%8b%e6%8a%8a%e6%89%8b%e6%95%99%e4%bd%a0%e4%bd%bf%e7%94%a8redis%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%88redis%e9%80%a3%e6%8e%a5%e6%b1%a0%e6%80%8e%e9%ba%bc%e4%bd%bf%e7%94%a8%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/185652\/","title":{"rendered":"\u624b\u628a\u624b\u6559\u4f60\u4f7f\u7528Redis\u9023\u63a5\u6c60\uff08redis\u9023\u63a5\u6c60\u600e\u9ebc\u4f7f\u7528\uff09"},"content":{"rendered":"<h1 id=\"%e6%89%8b%e6%8a%8a%e6%89%8b%e6%95%99%e4%bd%a0%e4%bd%bf%e7%94%a8redis%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%88redis%e9%80%a3%e6%8e%a5%e6%b1%a0%e6%80%8e%e9%ba%bc%e4%bd%bf%e7%94%a8%ef%bc%89-cqazdFhFNr\">\u624b\u628a\u624b\u6559\u4f60\u4f7f\u7528Redis\u9023\u63a5\u6c60\uff08redis\u9023\u63a5\u6c60\u600e\u9ebc\u4f7f\u7528\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0cRedis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u6578\u64da\u6301\u4e45\u5316\u7b49\u5834\u666f\u3002\u70ba\u4e86\u63d0\u9ad8Redis\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\uff0c\u4f7f\u7528\u9023\u63a5\u6c60\u662f\u4e00\u500b\u975e\u5e38\u6709\u6548\u7684\u7b56\u7565\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Redis\u9023\u63a5\u6c60\uff0c\u4e26\u63d0\u4f9b\u5be6\u7528\u7684\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-cqazdFhFNr\">\u4ec0\u9ebc\u662fRedis\u9023\u63a5\u6c60\uff1f<\/h2>\n<p>Redis\u9023\u63a5\u6c60\u662f\u4e00\u7a2e\u7ba1\u7406Redis\u9023\u63a5\u7684\u6280\u8853\uff0c\u901a\u904e\u9810\u5148\u5efa\u7acb\u4e00\u5b9a\u6578\u91cf\u7684\u9023\u63a5\uff0c\u4e26\u5728\u9700\u8981\u6642\u91cd\u8907\u4f7f\u7528\u9019\u4e9b\u9023\u63a5\uff0c\u5f9e\u800c\u6e1b\u5c11\u6bcf\u6b21\u8acb\u6c42\u6642\u5efa\u7acb\u548c\u95dc\u9589\u9023\u63a5\u7684\u958b\u92b7\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6027\u80fd\uff0c\u9084\u80fd\u6709\u6548\u5730\u7ba1\u7406\u8cc7\u6e90\uff0c\u907f\u514d\u56e0\u70ba\u904e\u591a\u7684\u9023\u63a5\u800c\u5c0e\u81f4\u7684\u6027\u80fd\u74f6\u9838\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%9c%80%e8%a6%81%e4%bd%bf%e7%94%a8%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-cqazdFhFNr\">\u70ba\u4ec0\u9ebc\u9700\u8981\u4f7f\u7528\u9023\u63a5\u6c60\uff1f<\/h2>\n<ul>\n<li><strong>\u6027\u80fd\u63d0\u5347\uff1a<\/strong>\u91cd\u8907\u4f7f\u7528\u9023\u63a5\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u5ef6\u9072\uff0c\u7279\u5225\u662f\u5728\u9ad8\u4e26\u767c\u7684\u60c5\u6cc1\u4e0b\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u7ba1\u7406\uff1a<\/strong>\u901a\u904e\u9650\u5236\u540c\u6642\u6253\u958b\u7684\u9023\u63a5\u6578\u91cf\uff0c\u53ef\u4ee5\u907f\u514d\u8cc7\u6e90\u7684\u6d6a\u8cbb\u3002<\/li>\n<li><strong>\u7c21\u5316\u4ee3\u78bc\uff1a<\/strong>\u9023\u63a5\u6c60\u53ef\u4ee5\u7c21\u5316\u61c9\u7528\u7a0b\u5f0f\u7684\u9023\u63a5\u7ba1\u7406\u908f\u8f2f\uff0c\u8b93\u958b\u767c\u8005\u5c08\u6ce8\u65bc\u696d\u52d9\u908f\u8f2f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8redis%e9%80%a3%e6%8e%a5%e6%b1%a0-cqazdFhFNr\">\u5982\u4f55\u4f7f\u7528Redis\u9023\u63a5\u6c60<\/h2>\n<p>\u5728\u9019\u88e1\uff0c\u6211\u5011\u5c07\u4f7f\u7528Python\u7684`redis-py`\u5eab\u4f86\u6f14\u793a\u5982\u4f55\u8a2d\u7f6e\u548c\u4f7f\u7528Redis\u9023\u63a5\u6c60\u3002\u9996\u5148\uff0c\u78ba\u4fdd\u4f60\u5df2\u7d93\u5b89\u88dd\u4e86`redis`\u5eab\u3002\u5982\u679c\u5c1a\u672a\u5b89\u88dd\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>pip install redis<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f1%ef%bc%9a%e5%b0%8e%e5%85%a5%e6%89%80%e9%9c%80%e7%9a%84%e5%ba%ab-cqazdFhFNr\">\u6b65\u9a5f1\uff1a\u5c0e\u5165\u6240\u9700\u7684\u5eab<\/h3>\n<pre><code>import redis<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f2%ef%bc%9a%e5%89%b5%e5%bb%ba%e9%80%a3%e6%8e%a5%e6%b1%a0-cqazdFhFNr\">\u6b65\u9a5f2\uff1a\u5275\u5efa\u9023\u63a5\u6c60<\/h3>\n<p>\u4f7f\u7528`redis.ConnectionPool`\u4f86\u5275\u5efa\u4e00\u500b\u9023\u63a5\u6c60\uff0c\u4e26\u6307\u5b9aRedis\u670d\u52d9\u5668\u7684\u5730\u5740\u548c\u7aef\u53e3\uff1a<\/p>\n<pre><code>pool = redis.ConnectionPool(host='localhost', port=6379, db=0)<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f3%ef%bc%9a%e4%bd%bf%e7%94%a8%e9%80%a3%e6%8e%a5%e6%b1%a0%e5%89%b5%e5%bb%baredis%e5%ae%a2%e6%88%b6%e7%ab%af-cqazdFhFNr\">\u6b65\u9a5f3\uff1a\u4f7f\u7528\u9023\u63a5\u6c60\u5275\u5efaRedis\u5ba2\u6236\u7aef<\/h3>\n<p>\u901a\u904e\u9023\u63a5\u6c60\u5275\u5efaRedis\u5ba2\u6236\u7aef\uff0c\u9019\u6a23\u6bcf\u6b21\u64cd\u4f5c\u90fd\u6703\u81ea\u52d5\u5f9e\u9023\u63a5\u6c60\u4e2d\u7372\u53d6\u9023\u63a5\uff1a<\/p>\n<pre><code>r = redis.Redis(connection_pool=pool)<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f4%ef%bc%9a%e5%9f%b7%e8%a1%8credis%e6%93%8d%e4%bd%9c-cqazdFhFNr\">\u6b65\u9a5f4\uff1a\u57f7\u884cRedis\u64cd\u4f5c<\/h3>\n<p>\u73fe\u5728\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Redis\u5ba2\u6236\u7aef\u4f86\u57f7\u884c\u5404\u7a2e\u64cd\u4f5c\uff0c\u4f8b\u5982\u8a2d\u7f6e\u548c\u7372\u53d6\u9375\u503c\uff1a<\/p>\n<pre><code># \u8a2d\u7f6e\u9375\u503c\nr.set('foo', 'bar')\n\n# \u7372\u53d6\u9375\u503c\nvalue = r.get('foo')\nprint(value)  # \u8f38\u51fa\uff1ab'bar'<\/code><\/pre>\n<h2 id=\"%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%9a%84%e9%85%8d%e7%bd%ae%e9%81%b8%e9%a0%85-cqazdFhFNr\">\u9023\u63a5\u6c60\u7684\u914d\u7f6e\u9078\u9805<\/h2>\n<p>\u5728\u5275\u5efa\u9023\u63a5\u6c60\u6642\uff0c\u53ef\u4ee5\u6839\u64da\u9700\u8981\u914d\u7f6e\u4e00\u4e9b\u9078\u9805\uff1a<\/p>\n<ul>\n<li><strong>max_connections\uff1a<\/strong>\u8a2d\u7f6e\u9023\u63a5\u6c60\u4e2d\u6700\u5927\u7684\u9023\u63a5\u6578\u91cf\u3002<\/li>\n<li><strong>min_connections\uff1a<\/strong>\u8a2d\u7f6e\u9023\u63a5\u6c60\u4e2d\u6700\u5c0f\u7684\u9023\u63a5\u6578\u91cf\u3002<\/li>\n<li><strong>timeout\uff1a<\/strong>\u8a2d\u7f6e\u9023\u63a5\u7684\u8d85\u6642\u6642\u9593\u3002<\/li>\n<\/ul>\n<pre><code>pool = redis.ConnectionPool(host='localhost', port=6379, db=0, max_connections=10, min_connections=2, timeout=5)<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-cqazdFhFNr\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Redis\u9023\u63a5\u6c60\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u548c\u8cc7\u6e90\u7ba1\u7406\u6548\u7387\u3002\u901a\u904e\u7c21\u55ae\u7684\u5e7e\u500b\u6b65\u9a5f\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u4f60\u7684\u61c9\u7528\u4e2d\u5be6\u73fe\u9023\u63a5\u6c60\u7684\u529f\u80fd\u3002\u7121\u8ad6\u662f\u958b\u767c\u5c0f\u578b\u61c9\u7528\u9084\u662f\u5927\u578b\u7cfb\u7d71\uff0c\u5408\u7406\u4f7f\u7528\u9023\u63a5\u6c60\u90fd\u662f\u4e00\u500b\u503c\u5f97\u8003\u616e\u7684\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u4f60\u7684Redis\u670d\u52d9\uff0cServer.HK\u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u4f60\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Redis\u9023\u63a5\u6c60\uff0c\u624b\u628a\u624b\u6559\u4f60\u914d\u7f6e\u8207\u7ba1\u7406\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u8cc7\u6e90\u5229\u7528\u6548\u7387\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-185652","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185652","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=185652"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185652\/revisions"}],"predecessor-version":[{"id":185653,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/185652\/revisions\/185653"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=185652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=185652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=185652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}