{"id":152445,"date":"2024-11-01T23:05:21","date_gmt":"2024-11-01T15:05:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/152445\/"},"modified":"2024-11-01T23:05:22","modified_gmt":"2024-11-01T15:05:22","slug":"%e6%8f%92%e6%9b%b2%e5%b0%87%e6%95%b8%e6%93%9a%e5%ad%98%e5%85%a5redis%ef%bc%88%e6%8f%92%e6%9b%b2%e6%95%b8%e6%93%9a%e5%ad%98%e5%85%a5redis%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/152445\/","title":{"rendered":"\u63d2\u66f2\u5c07\u6578\u64da\u5b58\u5165Redis\uff08\u63d2\u66f2\u6578\u64da\u5b58\u5165redis\uff09"},"content":{"rendered":"<h1 id=\"%e6%8f%92%e6%9b%b2%e5%b0%87%e6%95%b8%e6%93%9a%e5%ad%98%e5%85%a5redis%ef%bc%88%e6%8f%92%e6%9b%b2%e6%95%b8%e6%93%9a%e5%ad%98%e5%85%a5redis%ef%bc%89-fJSAJsHqwU\">\u63d2\u66f2\u5c07\u6578\u64da\u5b58\u5165Redis\uff08\u63d2\u66f2\u6578\u64da\u5b58\u5165redis\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u5feb\u901f\u4e14\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\u8b8a\u5f97\u8d8a\u4f86\u8d8a\u91cd\u8981\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6027\u80fd\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u56e0\u5176\u5feb\u901f\u7684\u6578\u64da\u8b80\u53d6\u548c\u5beb\u5165\u80fd\u529b\u800c\u53d7\u5230\u5ee3\u6cdb\u4f7f\u7528\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5c07\u63d2\u66f2\u6578\u64da\u5b58\u5165Redis\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-fJSAJsHqwU\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\uff08Remote Dictionary Server\uff09\u662f\u4e00\u500b\u958b\u6e90\u7684\u5167\u5b58\u6578\u64da\u7d50\u69cb\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\u548c\u6709\u5e8f\u96c6\u5408\u3002\u7531\u65bc\u5176\u9ad8\u6548\u7684\u6027\u80fd\uff0cRedis\u5e38\u88ab\u7528\u4f5c\u6578\u64da\u7de9\u5b58\u3001\u6d88\u606f\u4ee3\u7406\u548c\u4efb\u52d9\u968a\u5217\u7b49\u5834\u666f\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%e4%be%86%e5%ad%98%e5%84%b2%e6%8f%92%e6%9b%b2%e6%95%b8%e6%93%9a%ef%bc%9f-fJSAJsHqwU\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\u4f86\u5b58\u5132\u63d2\u66f2\u6578\u64da\uff1f<\/h2>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u80fd\u5920\u5728\u6beb\u79d2\u7d1a\u5225\u5167\u5b8c\u6210\u6578\u64da\u7684\u8b80\u53d6\u548c\u5beb\u5165\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u5feb\u901f\u97ff\u61c9\u7684\u61c9\u7528\u7a0b\u5e8f\u81f3\u95dc\u91cd\u8981\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\u9748\u6d3b\u5730\u8655\u7406\u4e0d\u540c\u985e\u578b\u7684\u6578\u64da\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>\u96d6\u7136Redis\u4e3b\u8981\u662f\u5167\u5b58\u6578\u64da\u5eab\uff0c\u4f46\u5b83\u4e5f\u63d0\u4f9b\u4e86\u6301\u4e45\u5316\u9078\u9805\uff0c\u80fd\u5920\u5c07\u6578\u64da\u4fdd\u5b58\u5230\u78c1\u789f\u4e2d\uff0c\u9632\u6b62\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%b0%87%e6%8f%92%e6%9b%b2%e6%95%b8%e6%93%9a%e5%ad%98%e5%85%a5redis-fJSAJsHqwU\">\u5982\u4f55\u5c07\u63d2\u66f2\u6578\u64da\u5b58\u5165Redis<\/h2>\n<p>\u5728\u5c07\u63d2\u66f2\u6578\u64da\u5b58\u5165Redis\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88ddRedis\u4e26\u555f\u52d5\u670d\u52d9\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u5728Linux\u7cfb\u7d71\u4e0a\u5b89\u88ddRedis\uff1a<\/p>\n<pre><code>sudo apt-get update\nsudo apt-get install redis-server\n<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u555f\u52d5Redis\u670d\u52d9\uff1a<\/p>\n<pre><code>sudo service redis-server start\n<\/code><\/pre>\n<h3 id=\"%e6%8f%92%e6%9b%b2%e6%95%b8%e6%93%9a%e7%9a%84%e7%b5%90%e6%a7%8b-fJSAJsHqwU\">\u63d2\u66f2\u6578\u64da\u7684\u7d50\u69cb<\/h3>\n<p>\u5728\u5b58\u5132\u63d2\u66f2\u6578\u64da\u4e4b\u524d\uff0c\u6211\u5011\u9700\u8981\u5b9a\u7fa9\u6578\u64da\u7684\u7d50\u69cb\u3002\u5047\u8a2d\u6211\u5011\u7684\u63d2\u66f2\u6578\u64da\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a<\/p>\n<ul>\n<li><strong>id\uff1a<\/strong>\u63d2\u66f2\u7684\u552f\u4e00\u6a19\u8b58\u7b26<\/li>\n<li><strong>title\uff1a<\/strong>\u63d2\u66f2\u7684\u6a19\u984c<\/li>\n<li><strong>artist\uff1a<\/strong>\u63d2\u66f2\u7684\u85dd\u8853\u5bb6<\/li>\n<li><strong>duration\uff1a<\/strong>\u63d2\u66f2\u7684\u6642\u9577\uff08\u4ee5\u79d2\u70ba\u55ae\u4f4d\uff09<\/li>\n<\/ul>\n<h3 id=\"%e4%bd%bf%e7%94%a8redis%e5%ad%98%e5%84%b2%e6%8f%92%e6%9b%b2%e6%95%b8%e6%93%9a-fJSAJsHqwU\">\u4f7f\u7528Redis\u5b58\u5132\u63d2\u66f2\u6578\u64da<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528Python\u548cRedis\u5eab\u5c07\u63d2\u66f2\u6578\u64da\u5b58\u5165Redis\u7684\u793a\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>import redis\n\n# \u9023\u63a5\u5230Redis\u670d\u52d9\u5668\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u63d2\u66f2\u6578\u64da\ntrack = {\n    'id': '1',\n    'title': '\u63d2\u66f2\u4e00',\n    'artist': '\u85dd\u8853\u5bb6A',\n    'duration': 240\n}\n\n# \u5c07\u63d2\u66f2\u6578\u64da\u5b58\u5165Redis\nr.hmset(f'track:{track[\"id\"]}', track)\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u9023\u63a5\u5230\u672c\u5730\u7684Redis\u670d\u52d9\u5668\uff0c\u7136\u5f8c\u5b9a\u7fa9\u4e86\u4e00\u500b\u63d2\u66f2\u6578\u64da\u7684\u5b57\u5178\uff0c\u6700\u5f8c\u4f7f\u7528`hmset`\u65b9\u6cd5\u5c07\u6578\u64da\u5b58\u5165Redis\u3002\u9019\u88e1\u4f7f\u7528\u4e86\u54c8\u5e0c\u6578\u64da\u7d50\u69cb\u4f86\u5b58\u5132\u63d2\u66f2\u7684\u5404\u500b\u5c6c\u6027\u3002<\/p>\n<h3 id=\"%e6%aa%a2%e7%b4%a2%e6%8f%92%e6%9b%b2%e6%95%b8%e6%93%9a-fJSAJsHqwU\">\u6aa2\u7d22\u63d2\u66f2\u6578\u64da<\/h3>\n<p>\u8981\u6aa2\u7d22\u5b58\u5132\u5728Redis\u4e2d\u7684\u63d2\u66f2\u6578\u64da\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<pre><code># \u7372\u53d6\u63d2\u66f2\u6578\u64da\ntrack_data = r.hgetall('track:1')\nprint(track_data)\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u8fd4\u56de\u63d2\u66f2ID\u70ba1\u7684\u6240\u6709\u6578\u64da\uff0c\u4e26\u4ee5\u5b57\u5178\u7684\u5f62\u5f0f\u986f\u793a\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-fJSAJsHqwU\">\u7e3d\u7d50<\/h2>\n<p>Redis\u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\uff0c\u9069\u5408\u7528\u65bc\u5b58\u5132\u5404\u985e\u578b\u7684\u6578\u64da\uff0c\u5305\u62ec\u63d2\u66f2\u6578\u64da\u3002\u901a\u904e\u7c21\u55ae\u7684API\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u6578\u64da\u5b58\u5165Redis\u4e26\u9032\u884c\u6aa2\u7d22\u3002\u5c0d\u65bc\u9700\u8981\u5feb\u901f\u97ff\u61c9\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u9078\u64c7Redis\u7121\u7591\u662f\u4e00\u500b\u660e\u667a\u7684\u9078\u64c7\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6027\u80fd\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684Redis\u670d\u52d9\uff0cServer.HK\u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u5c07\u63d2\u66f2\u6578\u64da\u9ad8\u6548\u5b58\u5165Redis\uff0c\u63d0\u5347\u6578\u64da\u5b58\u53d6\u901f\u5ea6\uff0c\u512a\u5316\u61c9\u7528\u6027\u80fd\uff0c\u5be6\u73fe\u5feb\u901f\u97ff\u61c9\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-152445","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/152445","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=152445"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/152445\/revisions"}],"predecessor-version":[{"id":152446,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/152445\/revisions\/152446"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=152445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=152445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=152445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}