{"id":126001,"date":"2024-10-26T01:04:21","date_gmt":"2024-10-25T17:04:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/126001\/"},"modified":"2024-10-26T01:04:22","modified_gmt":"2024-10-25T17:04:22","slug":"redis-%e9%80%a3%e6%8e%a5%e6%b1%a0%e8%87%aa%e5%8b%95%e5%9b%9e%e6%94%b6%e6%8f%90%e9%ab%98%e6%95%88%e7%8e%87%e8%88%87%e6%80%a7%e8%83%bd","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/126001\/","title":{"rendered":"Redis \u9023\u63a5\u6c60\u81ea\u52d5\u56de\u6536\u63d0\u9ad8\u6548\u7387\u8207\u6027\u80fd"},"content":{"rendered":"<h1 id=\"redis-%e9%80%a3%e6%8e%a5%e6%b1%a0%e8%87%aa%e5%8b%95%e5%9b%9e%e6%94%b6%e6%8f%90%e9%ab%98%e6%95%88%e7%8e%87%e8%88%87%e6%80%a7%e8%83%bd-WqeohIyUKf\">Redis \u9023\u63a5\u6c60\u81ea\u52d5\u56de\u6536\u63d0\u9ad8\u6548\u7387\u8207\u6027\u80fd<\/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\u96a8\u8457\u4f7f\u7528\u8005\u9700\u6c42\u7684\u589e\u52a0\uff0c\u5982\u4f55\u6709\u6548\u7ba1\u7406 Redis \u9023\u63a5\u6210\u70ba\u4e86\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\u7684\u95dc\u9375\u4e4b\u4e00\u3002\u672c\u6587\u5c07\u63a2\u8a0e Redis \u9023\u63a5\u6c60\u7684\u81ea\u52d5\u56de\u6536\u6a5f\u5236\uff0c\u4e26\u5206\u6790\u5176\u5c0d\u6548\u7387\u8207\u6027\u80fd\u7684\u5f71\u97ff\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-redis-%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-WqeohIyUKf\">\u4ec0\u9ebc\u662f Redis \u9023\u63a5\u6c60\uff1f<\/h2>\n<p>Redis \u9023\u63a5\u6c60\u662f\u4e00\u7a2e\u7ba1\u7406 Redis \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\u6a23\u7684\u505a\u6cd5\u4e0d\u50c5\u80fd\u63d0\u9ad8\u61c9\u7528\u7a0b\u5f0f\u7684\u97ff\u61c9\u901f\u5ea6\uff0c\u9084\u80fd\u964d\u4f4e\u8cc7\u6e90\u7684\u6d88\u8017\u3002<\/p>\n<h2 id=\"%e8%87%aa%e5%8b%95%e5%9b%9e%e6%94%b6%e7%9a%84%e5%bf%85%e8%a6%81%e6%80%a7-WqeohIyUKf\">\u81ea\u52d5\u56de\u6536\u7684\u5fc5\u8981\u6027<\/h2>\n<p>\u5728\u9ad8\u4e26\u767c\u7684\u74b0\u5883\u4e2d\uff0c\u9023\u63a5\u6c60\u4e2d\u7684\u9023\u63a5\u53ef\u80fd\u6703\u56e0\u70ba\u5404\u7a2e\u539f\u56e0\u800c\u8b8a\u5f97\u4e0d\u53ef\u7528\uff0c\u4f8b\u5982\u7db2\u7d61\u554f\u984c\u6216 Redis \u670d\u52d9\u5668\u7684\u91cd\u555f\u3002\u5982\u679c\u4e0d\u53ca\u6642\u56de\u6536\u9019\u4e9b\u5931\u6548\u7684\u9023\u63a5\uff0c\u5c07\u6703\u5c0e\u81f4\u9023\u63a5\u6c60\u4e2d\u7684\u53ef\u7528\u9023\u63a5\u6578\u91cf\u6e1b\u5c11\uff0c\u6700\u7d42\u5f71\u97ff\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u3002\u56e0\u6b64\uff0c\u81ea\u52d5\u56de\u6536\u6a5f\u5236\u986f\u5f97\u5c24\u70ba\u91cd\u8981\u3002<\/p>\n<h2 id=\"%e8%87%aa%e5%8b%95%e5%9b%9e%e6%94%b6%e7%9a%84%e5%af%a6%e7%8f%be%e6%96%b9%e5%bc%8f-WqeohIyUKf\">\u81ea\u52d5\u56de\u6536\u7684\u5be6\u73fe\u65b9\u5f0f<\/h2>\n<p>\u81ea\u52d5\u56de\u6536\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u5e7e\u7a2e\u65b9\u5f0f\u5be6\u73fe\uff1a<\/p>\n<ul>\n<li><strong>\u5b9a\u6642\u6aa2\u67e5\uff1a<\/strong>\u5b9a\u671f\u6aa2\u67e5\u9023\u63a5\u6c60\u4e2d\u7684\u9023\u63a5\u72c0\u614b\uff0c\u5c0d\u65bc\u5931\u6548\u7684\u9023\u63a5\u9032\u884c\u56de\u6536\u3002<\/li>\n<li><strong>\u8acb\u6c42\u6642\u6aa2\u67e5\uff1a<\/strong>\u5728\u6bcf\u6b21\u8acb\u6c42\u6642\u6aa2\u67e5\u9023\u63a5\u7684\u6709\u6548\u6027\uff0c\u82e5\u767c\u73fe\u5931\u6548\u5247\u7acb\u5373\u56de\u6536\u3002<\/li>\n<li><strong>\u9592\u7f6e\u8d85\u6642\uff1a<\/strong>\u5c0d\u65bc\u9577\u6642\u9593\u672a\u4f7f\u7528\u7684\u9023\u63a5\uff0c\u8a2d\u7f6e\u9592\u7f6e\u8d85\u6642\uff0c\u8d85\u904e\u6642\u9593\u5f8c\u81ea\u52d5\u56de\u6536\u3002<\/li>\n<\/ul>\n<h3 id=\"%e4%bb%a3%e7%a2%bc%e7%a4%ba%e4%be%8b-WqeohIyUKf\">\u4ee3\u78bc\u793a\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 Redis \u9023\u63a5\u6c60\u81ea\u52d5\u56de\u6536\u7684\u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nimport redis\nfrom redis.exceptions import ConnectionError\n\nclass RedisConnectionPool:\n    def __init__(self, max_connections=10):\n        self.pool = redis.ConnectionPool(max_connections=max_connections)\n\n    def get_connection(self):\n        try:\n            conn = redis.Redis(connection_pool=self.pool)\n            # \u6aa2\u67e5\u9023\u63a5\u662f\u5426\u6709\u6548\n            conn.ping()\n            return conn\n        except ConnectionError:\n            # \u5982\u679c\u9023\u63a5\u5931\u6548\uff0c\u5247\u56de\u6536\n            self.pool.disconnect()\n            return self.get_connection()\n\n# \u4f7f\u7528\u793a\u4f8b\npool = RedisConnectionPool()\nconnection = pool.get_connection()\n<\/code><\/pre>\n<h2 id=\"%e8%87%aa%e5%8b%95%e5%9b%9e%e6%94%b6%e7%9a%84%e5%84%aa%e5%8b%a2-WqeohIyUKf\">\u81ea\u52d5\u56de\u6536\u7684\u512a\u52e2<\/h2>\n<p>\u81ea\u52d5\u56de\u6536\u6a5f\u5236\u7684\u5be6\u65bd\u80fd\u5920\u5e36\u4f86\u4ee5\u4e0b\u5e7e\u500b\u512a\u52e2\uff1a<\/p>\n<ul>\n<li><strong>\u63d0\u9ad8\u53ef\u7528\u6027\uff1a<\/strong>\u901a\u904e\u53ca\u6642\u56de\u6536\u5931\u6548\u9023\u63a5\uff0c\u78ba\u4fdd\u9023\u63a5\u6c60\u4e2d\u7684\u9023\u63a5\u59cb\u7d42\u53ef\u7528\u3002<\/li>\n<li><strong>\u964d\u4f4e\u5ef6\u9072\uff1a<\/strong>\u6e1b\u5c11\u56e0\u70ba\u9023\u63a5\u5931\u6548\u800c\u5c0e\u81f4\u7684\u91cd\u8a66\u6b21\u6578\uff0c\u5f9e\u800c\u964d\u4f4e\u8acb\u6c42\u7684\u5ef6\u9072\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u7ba1\u7406\uff1a<\/strong>\u6709\u6548\u7ba1\u7406\u9023\u63a5\u8cc7\u6e90\uff0c\u907f\u514d\u56e0\u70ba\u904e\u591a\u7684\u5931\u6548\u9023\u63a5\u800c\u5c0e\u81f4\u7684\u8cc7\u6e90\u6d6a\u8cbb\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-WqeohIyUKf\">\u7d50\u8ad6<\/h2>\n<p>Redis \u9023\u63a5\u6c60\u7684\u81ea\u52d5\u56de\u6536\u6a5f\u5236\u5c0d\u65bc\u63d0\u5347\u7cfb\u7d71\u7684\u6548\u7387\u8207\u6027\u80fd\u81f3\u95dc\u91cd\u8981\u3002\u901a\u904e\u5408\u7406\u7684\u5be6\u73fe\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u9023\u63a5\u8cc7\u6e90\uff0c\u78ba\u4fdd\u61c9\u7528\u7a0b\u5f0f\u5728\u9ad8\u4e26\u767c\u74b0\u5883\u4e0b\u7684\u7a69\u5b9a\u6027\u8207\u97ff\u61c9\u901f\u5ea6\u3002\u5c0d\u65bc\u5e0c\u671b\u5728\u9999\u6e2f\u5c0b\u627e\u9ad8\u6548\u80fd\u7684\u89e3\u6c7a\u65b9\u6848\u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u670d\u52d9\u5c07\u662f\u63d0\u5347\u61c9\u7528\u6027\u80fd\u7684\u660e\u667a\u4e4b\u9078\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis \u9023\u63a5\u6c60\u81ea\u52d5\u56de\u6536\u6280\u8853\u63d0\u5347\u7cfb\u7d71\u6548\u7387\u8207\u6027\u80fd\uff0c\u78ba\u4fdd\u8cc7\u6e90\u6700\u4f73\u5229\u7528\uff0c\u6e1b\u5c11\u5ef6\u9072\uff0c\u589e\u5f37\u61c9\u7528\u7a69\u5b9a\u6027\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-126001","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/126001","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=126001"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/126001\/revisions"}],"predecessor-version":[{"id":126002,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/126001\/revisions\/126002"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=126001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=126001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=126001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}