{"id":5091,"date":"2024-11-14T08:55:19","date_gmt":"2024-11-14T00:55:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5091\/"},"modified":"2024-11-14T08:55:19","modified_gmt":"2024-11-14T00:55:19","slug":"redis%e6%80%8e%e4%b9%88%e7%94%a8python","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5091\/","title":{"rendered":"redis\u600e\u4e48\u7528python"},"content":{"rendered":"<blockquote><p>\n  \u901a\u8fc7 python client \u5e93\uff0c\u60a8\u53ef\u4ee5\u8fde\u63a5 redis \u6570\u636e\u5e93\u5e76\u6267\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u5305\u62ec\uff1a\u901a\u8fc7 strictredis \u7c7b\u8fde\u63a5\u5230\u670d\u52a1\u5668\u4f7f\u7528 set() \u8bbe\u7f6e\u503c\uff0c\u4f7f\u7528 get() \u83b7\u53d6\u503c\uff0c\u4f7f\u7528 delete() \u5220\u9664\u503c\u5229\u7528\u9ad8\u7ea7\u64cd\u4f5c\u5904\u7406\u5217\u8868\u3001\u96c6\u5408\u548c\u6563\u5217\u4f7f\u7528\u7ba1\u9053\u3001\u4e8b\u52a1\u548c\u53d1\u5e03\/\u8ba2\u9605\u7b49\u9ad8\u7ea7\u529f\u80fd\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/21\/2024052115453337939.jpg\" class=\"aligncenter\" title=\"redis\u600e\u4e48\u7528python\u63d2\u56fe\" alt=\"redis\u600e\u4e48\u7528python\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528 Python \u8fde\u63a5\u548c\u64cd\u4f5c Redis<\/strong><\/p>\n<p>Redis \u662f\u4e00\u79cd\u6d41\u884c\u7684\u5185\u5b58\u4e2d\u952e\u503c\u5b58\u50a8\u6570\u636e\u5e93\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 Python Client \u5e93\u8f7b\u677e\u5730\u5c06\u5176\u4e0e Python \u5e94\u7528\u7a0b\u5e8f\u8fde\u63a5\u548c\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u8fde\u63a5\u5230 Redis<\/strong><\/p>\n<p>\u8981\u8fde\u63a5\u5230 Redis \u670d\u52a1\u5668\uff0c\u8bf7\u4f7f\u7528  \u5e93\u4e2d\u7684 StrictRedis \u7c7b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>import redis\n\nr = redis.StrictRedis(host='localhost', port=6379, db=0)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>host\uff1aRedis \u670d\u52a1\u5668\u7684\u4e3b\u673a\u540d\u6216 IP \u5730\u5740\u3002<\/li>\n<li>port\uff1aRedis \u670d\u52a1\u5668\u7684\u7aef\u53e3\u53f7\uff08\u9ed8\u8ba4\u503c\u4e3a 6379\uff09\u3002<\/li>\n<li>db\uff1a\u8981\u4f7f\u7528\u7684 Redis \u6570\u636e\u5e93\uff08\u9ed8\u8ba4\u503c\u4e3a 0\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u8bbe\u7f6e\u548c\u83b7\u53d6\u503c<\/strong><\/p>\n<p>\u8981\u8bbe\u7f6e Redis \u4e2d\u7684\u503c\uff0c\u8bf7\u4f7f\u7528 set() \u65b9\u6cd5\uff1a<\/p>\n<pre>r.set('key', 'value')<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8981\u83b7\u53d6 Redis \u4e2d\u7684\u503c\uff0c\u8bf7\u4f7f\u7528 get() \u65b9\u6cd5\uff1a<\/p>\n<pre>value = r.get('key')<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5220\u9664\u503c<\/strong><\/p>\n<p>\u8981\u5220\u9664 Redis \u4e2d\u7684\u503c\uff0c\u8bf7\u4f7f\u7528 delete() \u65b9\u6cd5\uff1a<\/p>\n<pre>r.delete('key')<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9ad8\u7ea7\u64cd\u4f5c<\/strong><\/p>\n<p>Redis \u8fd8\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u5217\u8868\u3001\u96c6\u5408\u548c\u6563\u5217\uff1a<\/p>\n<ul>\n<li> <strong>\u5217\u8868\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528 lpush() \u548c rpush() \u65b9\u6cd5\u5411\u5217\u8868\u4e2d\u8ffd\u52a0\u5143\u7d20\uff0c\u5e76\u4f7f\u7528 lrange() \u65b9\u6cd5\u83b7\u53d6\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002<\/li>\n<li> <strong>\u96c6\u5408\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528 sadd() \u65b9\u6cd5\u5411\u96c6\u5408\u4e2d\u6dfb\u52a0\u5143\u7d20\uff0c\u5e76\u4f7f\u7528 smembers() \u65b9\u6cd5\u83b7\u53d6\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002<\/li>\n<li> <strong>\u6563\u5217\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528 hset() \u65b9\u6cd5\u5411\u6563\u5217\u4e2d\u8bbe\u7f6e\uff0c\u5e76\u4f7f\u7528 hgetall() \u65b9\u6cd5\u83b7\u53d6\u6563\u5217\u4e2d\u7684\u6240\u6709\u952e\u503c\u5bf9\u3002<\/li>\n<\/ul>\n<p><strong>\u5176\u4ed6\u7528\u6cd5<\/strong><\/p>\n<p>redis \u5e93\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u6709\u7528\u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li> <strong>\u7ba1\u9053\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528\u7ba1\u9053\u5bf9\u591a\u4e2a\u547d\u4ee4\u8fdb\u884c\u6279\u5904\u7406\u64cd\u4f5c\u3002<\/li>\n<li> <strong>\u4e8b\u52a1\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528\u4e8b\u52a1\u6267\u884c\u539f\u5b50\u64cd\u4f5c\u3002<\/li>\n<li> <strong>\u53d1\u5e03\/\u8ba2\u9605\uff1a<\/strong>\u53ef\u4ee5\u4f7f\u7528\u53d1\u5e03\/\u8ba2\u9605\u529f\u80fd\u5b9e\u73b0\u5b9e\u65f6\u6d88\u606f\u4f20\u9012\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4f7f\u7528 Python \u5ba2\u6237\u7aef\u5e93\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06 Redis \u7684\u5f3a\u5927\u529f\u80fd\u96c6\u6210\u5230\u60a8\u7684 Python \u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7 python client \u5e93\uff0c\u60a8\u53ef\u4ee5\u8fde\u63a5 redis \u6570\u636e\u5e93\u5e76\u6267\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u5305\u62ec\uff1a\u901a\u8fc7 strictredis \u7c7b\u8fde\u63a5\u5230\u670d\u52a1\u5668\u4f7f\u7528 set() \u8bbe\u7f6e\u503c\uff0c\u4f7f\u7528 get() \u83b7\u53d6\u503c\uff0c\u4f7f\u7528 delete() \u5220\u9664\u503c\u5229\u7528\u9ad8\u7ea7\u64cd\u4f5c\u5904\u7406\u5217\u8868\u3001\u96c6\u5408\u548c\u6563\u5217\u4f7f\u7528\u7ba1\u9053\u3001\u4e8b\u52a1\u548c\u53d1\u5e03\/\u8ba2\u9605\u7b49\u9ad8\u7ea7\u529f\u80fd \u4f7f\u7528 Python \u8fde\u63a5\u548c\u64cd\u4f5c Redis Redis \u662f\u4e00\u79cd\u6d41\u884c\u7684\u5185\u5b58\u4e2d\u952e\u503c\u5b58\u50a8\u6570\u636e\u5e93\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 Python Client \u5e93\u8f7b\u677e\u5730\u5c06\u5176\u4e0e Python \u5e94\u7528\u7a0b\u5e8f\u8fde\u63a5\u548c\u64cd\u4f5c\u3002 \u8fde\u63a5\u5230 Redis \u8981\u8fde\u63a5\u5230 Redis \u670d\u52a1\u5668\uff0c\u8bf7\u4f7f\u7528 \u5e93\u4e2d\u7684 StrictRedis \u7c7b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b import redis r = redis.StrictRedis(host=&#8217;localhost&#8217;, port=6379, db=0) \u767b\u5f55\u540e\u590d\u5236 host\uff1aRedis \u670d\u52a1\u5668\u7684\u4e3b\u673a\u540d\u6216 IP \u5730\u5740\u3002 port\uff1aRedis \u670d\u52a1\u5668\u7684\u7aef\u53e3\u53f7\uff08\u9ed8\u8ba4\u503c\u4e3a 6379\uff09\u3002 db\uff1a\u8981\u4f7f\u7528\u7684 Redis \u6570\u636e\u5e93\uff08\u9ed8\u8ba4\u503c\u4e3a 0\uff09\u3002 \u8bbe\u7f6e\u548c\u83b7\u53d6\u503c \u8981\u8bbe\u7f6e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5091","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5091"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5091\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}