{"id":197514,"date":"2024-11-14T08:35:20","date_gmt":"2024-11-14T00:35:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/197514\/"},"modified":"2024-11-14T08:35:21","modified_gmt":"2024-11-14T00:35:21","slug":"%e5%88%a9%e7%94%a8redis%e8%bc%95%e9%ac%86%e5%ad%98%e5%8f%96%e4%b8%80%e5%80%8b%e5%b0%8d%e8%b1%a1%ef%bc%88%e5%9c%a8redis%e5%ad%98%e6%94%be%e4%b8%80%e5%80%8b%e5%b0%8d%e8%b1%a1%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/197514\/","title":{"rendered":"\u5229\u7528Redis\u8f15\u9b06\u5b58\u53d6\u4e00\u500b\u5c0d\u8c61\uff08\u5728redis\u5b58\u653e\u4e00\u500b\u5c0d\u8c61\uff09"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8redis%e8%bc%95%e9%ac%86%e5%ad%98%e5%8f%96%e4%b8%80%e5%80%8b%e5%b0%8d%e8%b1%a1%ef%bc%88%e5%9c%a8redis%e5%ad%98%e6%94%be%e4%b8%80%e5%80%8b%e5%b0%8d%e8%b1%a1%ef%bc%89-cPdeHHVwIY\">\u5229\u7528Redis\u8f15\u9b06\u5b58\u53d6\u4e00\u500b\u5c0d\u8c61\uff08\u5728Redis\u5b58\u653e\u4e00\u500b\u5c0d\u8c61\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6578\u64da\u5b58\u53d6\u7684\u901f\u5ea6\u548c\u6548\u7387\u81f3\u95dc\u91cd\u8981\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u56e0\u5176\u5feb\u901f\u7684\u6578\u64da\u5b58\u53d6\u80fd\u529b\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\u800c\u53d7\u5230\u5ee3\u6cdb\u4f7f\u7528\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u8f15\u9b06\u5b58\u53d6\u4e00\u500b\u5c0d\u8c61\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\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-cPdeHHVwIY\">\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\u7b49\u3002\u5b83\u901a\u5e38\u7528\u4f5c\u6578\u64da\u5eab\u3001\u7de9\u5b58\u548c\u6d88\u606f\u4ee3\u7406\u3002\u7531\u65bc\u5176\u9ad8\u6027\u80fd\u548c\u9748\u6d3b\u6027\uff0cRedis\u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u5834\u666f\uff0c\u5305\u62ec\u5be6\u6642\u6578\u64da\u5206\u6790\u3001\u6703\u8a71\u7ba1\u7406\u548c\u6392\u884c\u699c\u7b49\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%8f%96%e5%b0%8d%e8%b1%a1%ef%bc%9f-cPdeHHVwIY\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\u4f86\u5b58\u53d6\u5c0d\u8c61\uff1f<\/h2>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u7684\u6578\u64da\u5b58\u53d6\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u80fd\u5920\u8655\u7406\u6bcf\u79d2\u6578\u5341\u842c\u6b21\u7684\u8acb\u6c42\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u80fd\u5920\u6eff\u8db3\u4e0d\u540c\u7684\u61c9\u7528\u9700\u6c42\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>\u53ef\u4ee5\u5c07\u6578\u64da\u6301\u4e45\u5316\u5230\u78c1\u789f\uff0c\u9632\u6b62\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>\u63d0\u4f9b\u7c21\u55ae\u7684API\uff0c\u65b9\u4fbf\u958b\u767c\u8005\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%9c%a8redis%e4%b8%ad%e5%ad%98%e6%94%be%e5%b0%8d%e8%b1%a1-cPdeHHVwIY\">\u5728Redis\u4e2d\u5b58\u653e\u5c0d\u8c61<\/h2>\n<p>\u5728Redis\u4e2d\u5b58\u653e\u5c0d\u8c61\u7684\u5e38\u898b\u65b9\u6cd5\u662f\u5c07\u5c0d\u8c61\u5e8f\u5217\u5316\u70ba\u5b57\u7b26\u4e32\uff0c\u7136\u5f8c\u5c07\u5176\u5b58\u5132\u5728Redis\u4e2d\u3002\u9019\u88e1\u6211\u5011\u5c07\u4f7f\u7528Python\u8a9e\u8a00\u548cRedis\u7684Python\u5ba2\u6236\u7aef\u5eab\u4f86\u6f14\u793a\u5982\u4f55\u5be6\u73fe\u9019\u4e00\u904e\u7a0b\u3002<\/p>\n<h3 id=\"%e5%ae%89%e8%a3%9dredis%e5%92%8cpython%e5%ae%a2%e6%88%b6%e7%ab%af-cPdeHHVwIY\">\u5b89\u88ddRedis\u548cPython\u5ba2\u6236\u7aef<\/h3>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u4f60\u5df2\u7d93\u5b89\u88dd\u4e86Redis\u670d\u52d9\u5668\u3002\u63a5\u4e0b\u4f86\uff0c\u5b89\u88ddRedis\u7684Python\u5ba2\u6236\u7aef\u5eab\uff1a<\/p>\n<pre><code>pip install redis<\/code><\/pre>\n<h3 id=\"%e5%ba%8f%e5%88%97%e5%8c%96%e5%b0%8d%e8%b1%a1-cPdeHHVwIY\">\u5e8f\u5217\u5316\u5c0d\u8c61<\/h3>\n<p>\u5728Python\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528`json`\u6a21\u584a\u4f86\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u5c0d\u8c61\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>import redis\nimport json\n\n# \u9023\u63a5\u5230Redis\nr = redis.Redis(host='localhost', port=6379, db=0)\n\n# \u5b9a\u7fa9\u4e00\u500b\u5c0d\u8c61\nobj = {\n    'name': 'Alice',\n    'age': 30,\n    'city': 'Hong Kong'\n}\n\n# \u5c07\u5c0d\u8c61\u5e8f\u5217\u5316\u70baJSON\u5b57\u7b26\u4e32\nobj_json = json.dumps(obj)\n\n# \u5c07\u5e8f\u5217\u5316\u7684\u5c0d\u8c61\u5b58\u5132\u5230Redis\nr.set('user:1000', obj_json)\n<\/code><\/pre>\n<h3 id=\"%e5%be%9eredis%e4%b8%ad%e8%ae%80%e5%8f%96%e5%b0%8d%e8%b1%a1-cPdeHHVwIY\">\u5f9eRedis\u4e2d\u8b80\u53d6\u5c0d\u8c61<\/h3>\n<p>\u8981\u5f9eRedis\u4e2d\u8b80\u53d6\u5c0d\u8c61\uff0c\u6211\u5011\u9700\u8981\u7372\u53d6\u5b58\u5132\u7684\u5b57\u7b26\u4e32\u4e26\u5c07\u5176\u53cd\u5e8f\u5217\u5316\u70baPython\u5c0d\u8c61\uff1a<\/p>\n<pre><code># \u5f9eRedis\u4e2d\u7372\u53d6\u5c0d\u8c61\nobj_json_from_redis = r.get('user:1000')\n\n# \u5c07JSON\u5b57\u7b26\u4e32\u53cd\u5e8f\u5217\u5316\u70baPython\u5c0d\u8c61\nobj_from_redis = json.loads(obj_json_from_redis)\n\nprint(obj_from_redis)\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-cPdeHHVwIY\">\u7e3d\u7d50<\/h2>\n<p>\u5229\u7528Redis\u5b58\u53d6\u5c0d\u8c61\u662f\u4e00\u500b\u7c21\u55ae\u800c\u9ad8\u6548\u7684\u904e\u7a0b\u3002\u901a\u904e\u5c07\u5c0d\u8c61\u5e8f\u5217\u5316\u70ba\u5b57\u7b26\u4e32\u4e26\u5b58\u5132\u5728Redis\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u5be6\u73fe\u6578\u64da\u7684\u5feb\u901f\u5b58\u53d6\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u63d0\u9ad8\u4e86\u61c9\u7528\u7684\u6027\u80fd\uff0c\u9084\u7c21\u5316\u4e86\u6578\u64da\u7ba1\u7406\u7684\u904e\u7a0b\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u5b58\u53d6\u7684\u61c9\u7528\uff0c\u9078\u64c7Redis\u4f5c\u70ba\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\u662f\u975e\u5e38\u660e\u667a\u7684\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\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\u4e0d\u540c\u9700\u6c42\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\uff0c\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u60a8\u7684\u61c9\u7528\u548c\u6578\u64da\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5229\u7528Redis\u8f15\u9b06\u5b58\u53d6\u548c\u5b58\u653e\u5c0d\u8c61\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u7c21\u5316\u61c9\u7528\u958b\u767c\u6d41\u7a0b\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-197514","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/197514","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=197514"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/197514\/revisions"}],"predecessor-version":[{"id":197515,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/197514\/revisions\/197515"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=197514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=197514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=197514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}