{"id":98324,"date":"2024-10-21T01:39:21","date_gmt":"2024-10-20T17:39:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/98324\/"},"modified":"2024-10-21T01:39:21","modified_gmt":"2024-10-20T17:39:21","slug":"redis-%e5%84%b2%e5%ad%98%e8%88%87%e8%ae%80%e5%8f%96%e5%9c%96%e7%89%87%e7%9a%84%e7%b6%9c%e5%90%88%e5%af%a6%e8%b8%90","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/98324\/","title":{"rendered":"Redis \u5132\u5b58\u8207\u8b80\u53d6\u5716\u7247\u7684\u7d9c\u5408\u5be6\u8e10"},"content":{"rendered":"<h1 id=\"redis-%e5%84%b2%e5%ad%98%e8%88%87%e8%ae%80%e5%8f%96%e5%9c%96%e7%89%87%e7%9a%84%e7%b6%9c%e5%90%88%e5%af%a6%e8%b8%90-wDvgoablJc\">Redis \u5132\u5b58\u8207\u8b80\u53d6\u5716\u7247\u7684\u7d9c\u5408\u5be6\u8e10<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u5716\u7247\u7684\u5132\u5b58\u8207\u8b80\u53d6\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u74b0\u3002\u96a8\u8457\u7528\u6236\u5c0d\u65bc\u5716\u7247\u8cea\u91cf\u548c\u52a0\u8f09\u901f\u5ea6\u7684\u8981\u6c42\u4e0d\u65b7\u63d0\u9ad8\uff0c\u9078\u64c7\u5408\u9069\u7684\u5132\u5b58\u89e3\u6c7a\u65b9\u6848\u8b8a\u5f97\u5c24\u70ba\u95dc\u9375\u3002Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u5feb\u901f\u7684\u6578\u64da\u5b58\u53d6\u80fd\u529b\uff0c\u9069\u5408\u7528\u65bc\u5132\u5b58\u548c\u8b80\u53d6\u5716\u7247\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528 Redis \u5132\u5b58\u8207\u8b80\u53d6\u5716\u7247\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u8e10\u7bc4\u4f8b\u3002<\/p>\n<h2 id=\"redis-%e7%b0%a1%e4%bb%8b-wDvgoablJc\">Redis \u7c21\u4ecb<\/h2>\n<p>Redis \u662f\u4e00\u7a2e\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u6578\u64da\u5eab\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\u7531\u65bc\u5176\u9ad8\u6548\u7684\u5167\u5b58\u64cd\u4f5c\uff0cRedis \u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u5373\u6642\u6578\u64da\u8655\u7406\u7b49\u5834\u666f\u3002\u5c0d\u65bc\u5716\u7247\u7684\u5132\u5b58\uff0cRedis \u63d0\u4f9b\u4e86\u5feb\u901f\u7684\u8b80\u53d6\u548c\u5beb\u5165\u80fd\u529b\uff0c\u80fd\u5920\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87-redis-%e5%84%b2%e5%ad%98%e5%9c%96%e7%89%87-wDvgoablJc\">\u70ba\u4ec0\u9ebc\u9078\u64c7 Redis \u5132\u5b58\u5716\u7247<\/h2>\n<ul>\n<li><strong>\u9ad8\u6548\u6027\uff1a<\/strong>Redis \u7684\u5167\u5b58\u5b58\u5132\u7279\u6027\u4f7f\u5f97\u5716\u7247\u7684\u8b80\u53d6\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u9069\u5408\u9700\u8981\u5feb\u901f\u97ff\u61c9\u7684\u61c9\u7528\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6027\uff1a<\/strong>\u4f7f\u7528 Redis \u5132\u5b58\u5716\u7247\u76f8\u5c0d\u7c21\u55ae\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u901a\u904e\u7c21\u55ae\u7684 API \u9032\u884c\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u64f4\u5c55\u6027\uff1a<\/strong>Redis \u652f\u6301\u5206\u4f48\u5f0f\u67b6\u69cb\uff0c\u80fd\u5920\u8f15\u9b06\u64f4\u5c55\u4ee5\u61c9\u5c0d\u5927\u91cf\u5716\u7247\u7684\u5132\u5b58\u9700\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%9c%a8-redis-%e4%b8%ad%e5%84%b2%e5%ad%98%e5%9c%96%e7%89%87-wDvgoablJc\">\u5982\u4f55\u5728 Redis \u4e2d\u5132\u5b58\u5716\u7247<\/h2>\n<p>\u5728 Redis \u4e2d\u5132\u5b58\u5716\u7247\u7684\u57fa\u672c\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5c07\u5716\u7247\u8f49\u63db\u70ba\u4e8c\u9032\u5236\u6578\u64da\u3002<\/li>\n<li>\u4f7f\u7528 Redis \u7684 <code>SET<\/code> \u547d\u4ee4\u5c07\u4e8c\u9032\u5236\u6578\u64da\u5132\u5b58\u5230\u6307\u5b9a\u7684\u9375\u4e2d\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 Python \u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5c07\u5716\u7247\u5132\u5b58\u5230 Redis \u4e2d\uff1a<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230 Redis\nr = redis.StrictRedis(host='localhost', port=6379, db=0)\n\n# \u8b80\u53d6\u5716\u7247\u4e26\u8f49\u63db\u70ba\u4e8c\u9032\u5236\u6578\u64da\nwith open('image.jpg', 'rb') as image_file:\n    image_data = image_file.read()\n\n# \u5132\u5b58\u5716\u7247\u5230 Redis\nr.set('my_image', image_data)\n<\/code><\/pre>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%be%9e-redis-%e4%b8%ad%e8%ae%80%e5%8f%96%e5%9c%96%e7%89%87-wDvgoablJc\">\u5982\u4f55\u5f9e Redis \u4e2d\u8b80\u53d6\u5716\u7247<\/h2>\n<p>\u5f9e Redis \u4e2d\u8b80\u53d6\u5716\u7247\u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528 Redis \u7684 <code>GET<\/code> \u547d\u4ee4\u8b80\u53d6\u6307\u5b9a\u9375\u7684\u6578\u64da\u3002<\/li>\n<li>\u5c07\u8b80\u53d6\u5230\u7684\u4e8c\u9032\u5236\u6578\u64da\u5beb\u5165\u6587\u4ef6\u6216\u76f4\u63a5\u986f\u793a\u3002<\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u662f\u5f9e Redis \u4e2d\u8b80\u53d6\u5716\u7247\u7684 Python \u7bc4\u4f8b\uff1a<\/p>\n<pre><code># \u5f9e Redis \u8b80\u53d6\u5716\u7247\nimage_data = r.get('my_image')\n\n# \u5c07\u4e8c\u9032\u5236\u6578\u64da\u5beb\u5165\u6587\u4ef6\nwith open('retrieved_image.jpg', 'wb') as image_file:\n    image_file.write(image_data)\n<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-wDvgoablJc\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528 Redis \u5132\u5b58\u5716\u7247\u6642\uff0c\u6709\u5e7e\u9ede\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li><strong>\u5167\u5b58\u9650\u5236\uff1a<\/strong>\u7531\u65bc Redis \u662f\u57fa\u65bc\u5167\u5b58\u7684\u6578\u64da\u5eab\uff0c\u5132\u5b58\u5927\u91cf\u5716\u7247\u53ef\u80fd\u6703\u5c0e\u81f4\u5167\u5b58\u4e0d\u8db3\uff0c\u56e0\u6b64\u9700\u8981\u5408\u7406\u898f\u5283\u5167\u5b58\u4f7f\u7528\u3002<\/li>\n<li><strong>\u6578\u64da\u6301\u4e45\u5316\uff1a<\/strong>Redis \u63d0\u4f9b\u4e86 RDB \u548c AOF \u5169\u7a2e\u6301\u4e45\u5316\u65b9\u5f0f\uff0c\u958b\u767c\u8005\u9700\u8981\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6301\u4e45\u5316\u7b56\u7565\u3002<\/li>\n<li><strong>\u5b89\u5168\u6027\uff1a<\/strong>\u78ba\u4fdd Redis \u7684\u5b89\u5168\u914d\u7f6e\uff0c\u9632\u6b62\u672a\u6388\u6b0a\u8a2a\u554f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-wDvgoablJc\">\u7e3d\u7d50<\/h2>\n<p>Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u6578\u64da\u5132\u5b58\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u6709\u6548\u5730\u5132\u5b58\u8207\u8b80\u53d6\u5716\u7247\u3002\u901a\u904e\u7c21\u55ae\u7684 API\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u5716\u7247\u8f49\u63db\u70ba\u4e8c\u9032\u5236\u6578\u64da\u4e26\u5132\u5b58\u5230 Redis \u4e2d\uff0c\u4e26\u5728\u9700\u8981\u6642\u5feb\u901f\u8b80\u53d6\u3002\u5118\u7ba1 Redis \u5728\u6027\u80fd\u4e0a\u5177\u6709\u512a\u52e2\uff0c\u4f46\u5728\u5be6\u969b\u61c9\u7528\u4e2d\u4ecd\u9700\u8003\u616e\u5167\u5b58\u9650\u5236\u548c\u6578\u64da\u6301\u4e45\u5316\u7b49\u554f\u984c\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u5716\u7247\u8655\u7406\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u6574\u9ad4\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Redis\u5728\u5716\u7247\u5132\u5b58\u8207\u8b80\u53d6\u4e2d\u7684\u61c9\u7528\uff0c\u63d0\u5347\u6027\u80fd\u8207\u6548\u7387\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u5a92\u9ad4\u7ba1\u7406\u89e3\u6c7a\u65b9\u6848\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-98324","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/98324","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=98324"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/98324\/revisions"}],"predecessor-version":[{"id":98325,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/98324\/revisions\/98325"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=98324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=98324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=98324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}