{"id":112030,"date":"2024-10-24T00:54:16","date_gmt":"2024-10-23T16:54:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/112030\/"},"modified":"2024-10-24T00:54:17","modified_gmt":"2024-10-23T16:54:17","slug":"redis-%e5%bf%ab%e5%8f%96%e6%94%b9%e5%96%84%e6%80%a7%e8%83%bd%e7%9a%84%e5%bf%85%e7%84%b6%e4%b9%8b%e9%81%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/112030\/","title":{"rendered":"Redis \u5feb\u53d6\u6539\u5584\u6027\u80fd\u7684\u5fc5\u7136\u4e4b\u9078"},"content":{"rendered":"<h1 id=\"redis-%e5%bf%ab%e5%8f%96%e6%94%b9%e5%96%84%e6%80%a7%e8%83%bd%e7%9a%84%e5%bf%85%e7%84%b6%e4%b9%8b%e9%81%b8-PgBwTpgJUe\">Redis \u5feb\u53d6\u6539\u5584\u6027\u80fd\u7684\u5fc5\u7136\u4e4b\u9078<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0c\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u81f3\u95dc\u91cd\u8981\u3002\u7528\u6236\u5c0d\u65bc\u901f\u5ea6\u7684\u8981\u6c42\u8d8a\u4f86\u8d8a\u9ad8\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u8005\u548c\u7cfb\u7d71\u7ba1\u7406\u54e1\u5fc5\u9808\u5c0b\u627e\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u63d0\u5347\u6027\u80fd\u3002Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5feb\u53d6\u7cfb\u7d71\uff0c\u5df2\u6210\u70ba\u6539\u5584\u6027\u80fd\u7684\u5fc5\u7136\u9078\u64c7\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-redis%ef%bc%9f-PgBwTpgJUe\">\u4ec0\u9ebc\u662f Redis\uff1f<\/h2>\n<p>Redis\uff08Remote Dictionary Server\uff09\u662f\u4e00\u500b\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u3002\u5b83\u901a\u5e38\u7528\u4f5c\u6578\u64da\u5eab\u3001\u5feb\u53d6\u548c\u6d88\u606f\u4ee3\u7406\u3002\u7531\u65bc\u5176\u5728\u5167\u5b58\u4e2d\u64cd\u4f5c\u7684\u7279\u6027\uff0cRedis \u80fd\u5920\u63d0\u4f9b\u6975\u5feb\u7684\u6578\u64da\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u3002<\/p>\n<h2 id=\"redis-%e7%9a%84%e5%84%aa%e5%8b%a2-PgBwTpgJUe\">Redis \u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis \u80fd\u5920\u6bcf\u79d2\u8655\u7406\u6578\u5341\u842c\u6b21\u7684\u8acb\u6c42\uff0c\u9019\u4f7f\u5f97\u5b83\u5728\u9700\u8981\u5feb\u901f\u97ff\u61c9\u7684\u61c9\u7528\u4e2d\u975e\u5e38\u53d7\u6b61\u8fce\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>\u96d6\u7136 Redis \u662f\u4e00\u500b\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u591a\u7a2e\u6301\u4e45\u5316\u9078\u9805\uff0c\u5141\u8a31\u7528\u6236\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6301\u4e45\u5316\u7b56\u7565\u3002<\/li>\n<li><strong>\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis \u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u9019\u4f7f\u5f97\u5b83\u80fd\u5920\u9069\u61c9\u4e0d\u540c\u7684\u61c9\u7528\u5834\u666f\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>Redis \u7684 API \u7c21\u55ae\u660e\u77ad\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u4e0a\u624b\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-redis-%e9%80%b2%e8%a1%8c%e5%bf%ab%e5%8f%96-PgBwTpgJUe\">\u5982\u4f55\u4f7f\u7528 Redis \u9032\u884c\u5feb\u53d6<\/h2>\n<p>\u4f7f\u7528 Redis \u9032\u884c\u5feb\u53d6\u7684\u57fa\u672c\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li><strong>\u5b89\u88dd Redis\uff1a<\/strong>\u53ef\u4ee5\u901a\u904e\u5305\u7ba1\u7406\u5de5\u5177\u6216\u5f9e\u6e90\u78bc\u7de8\u8b6f\u5b89\u88dd Redis\u3002<\/li>\n<li><strong>\u9023\u63a5 Redis\uff1a<\/strong>\u4f7f\u7528 Redis \u5ba2\u6236\u7aef\u9023\u63a5\u5230 Redis \u670d\u52d9\u5668\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 Python \u7684\u793a\u4f8b\uff1a<\/li>\n<\/ol>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230 Redis\nr = redis.Redis(host='localhost', port=6379, db=0)\n<\/code><\/pre>\n<li><strong>\u8a2d\u7f6e\u5feb\u53d6\uff1a<\/strong>\u5c07\u6578\u64da\u5b58\u5132\u5230 Redis \u4e2d\uff0c\u4e26\u8a2d\u7f6e\u904e\u671f\u6642\u9593\u4ee5\u63a7\u5236\u5feb\u53d6\u7684\u6709\u6548\u6027\u3002<\/li>\n<pre><code># \u8a2d\u7f6e\u5feb\u53d6\nr.set('key', 'value', ex=60)  # 60\u79d2\u5f8c\u904e\u671f\n<\/code><\/pre>\n<\/li>\n<li><strong>\u8b80\u53d6\u5feb\u53d6\uff1a<\/strong>\u5728\u9700\u8981\u6642\u5f9e Redis \u4e2d\u8b80\u53d6\u6578\u64da\u3002<\/li>\n<pre><code># \u8b80\u53d6\u5feb\u53d6\nvalue = r.get('key')\nif value:\n    print(value)\nelse:\n    print('Cache miss')\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2 id=\"redis-%e5%bf%ab%e5%8f%96%e7%9a%84%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-PgBwTpgJUe\">Redis \u5feb\u53d6\u7684\u61c9\u7528\u5834\u666f<\/h2>\n<p>Redis \u5feb\u53d6\u53ef\u4ee5\u61c9\u7528\u65bc\u591a\u7a2e\u5834\u666f\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u7db2\u9801\u5feb\u53d6\uff1a<\/strong>\u5c07\u975c\u614b\u9801\u9762\u6216\u52d5\u614b\u751f\u6210\u7684\u9801\u9762\u5feb\u53d6\u5230 Redis \u4e2d\uff0c\u4ee5\u6e1b\u5c11\u6578\u64da\u5eab\u67e5\u8a62\u7684\u6b21\u6578\u3002<\/li>\n<li><strong>\u6703\u8a71\u7ba1\u7406\uff1a<\/strong>\u4f7f\u7528 Redis \u5b58\u5132\u7528\u6236\u6703\u8a71\u4fe1\u606f\uff0c\u63d0\u4f9b\u5feb\u901f\u7684\u8b80\u53d6\u548c\u5beb\u5165\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u6392\u884c\u699c\u548c\u8a08\u6578\u5668\uff1a<\/strong>\u5229\u7528 Redis \u7684\u6578\u64da\u7d50\u69cb\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u6392\u884c\u699c\u548c\u8a08\u6578\u5668\u529f\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-PgBwTpgJUe\">\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\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u3002\u7121\u8ad6\u662f\u7528\u65bc\u7db2\u9801\u5feb\u53d6\u3001\u6703\u8a71\u7ba1\u7406\u9084\u662f\u5176\u4ed6\u61c9\u7528\u5834\u666f\uff0cRedis \u90fd\u80fd\u63d0\u4f9b\u5feb\u901f\u7684\u6578\u64da\u5b58\u53d6\u80fd\u529b\u3002\u96a8\u8457\u5c0d\u6027\u80fd\u8981\u6c42\u7684\u63d0\u9ad8\uff0c\u9078\u64c7 Redis \u4f5c\u70ba\u5feb\u53d6\u7cfb\u7d71\u5df2\u6210\u70ba\u8a31\u591a\u4f01\u696d\u7684\u5fc5\u7136\u4e4b\u9078\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u90e8\u7f72 Redis \u548c\u5176\u4ed6\u61c9\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis \u5feb\u53d6\u662f\u63d0\u5347\u61c9\u7528\u6027\u80fd\u7684\u6700\u4f73\u89e3\u6c7a\u65b9\u6848\uff0c\u63d0\u4f9b\u5feb\u901f\u6578\u64da\u5b58\u53d6\uff0c\u6e1b\u5c11\u5ef6\u9072\uff0c\u512a\u5316\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-112030","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/112030","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=112030"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/112030\/revisions"}],"predecessor-version":[{"id":112031,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/112030\/revisions\/112031"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=112030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=112030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=112030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}