{"id":97582,"date":"2024-10-20T19:13:18","date_gmt":"2024-10-20T11:13:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97582\/"},"modified":"2024-10-20T19:13:18","modified_gmt":"2024-10-20T11:13:18","slug":"redis-%e7%94%b1%e6%ad%a4%e8%80%8c%e8%b5%b7%e7%9a%84odm%e4%b9%8b%e8%b7%af%ef%bc%88redis%e7%b7%a3%e8%b5%b7%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97582\/","title":{"rendered":"Redis \u7531\u6b64\u800c\u8d77\u7684ODM\u4e4b\u8def\uff08redis\u7de3\u8d77\uff09"},"content":{"rendered":"<h1 id=\"redis-%e7%94%b1%e6%ad%a4%e8%80%8c%e8%b5%b7%e7%9a%84odm%e4%b9%8b%e8%b7%af%ef%bc%88redis%e7%b7%a3%e8%b5%b7%ef%bc%89-yCIKNpVdIK\">Redis \u7531\u6b64\u800c\u8d77\u7684ODM\u4e4b\u8def\uff08redis\u7de3\u8d77\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u6280\u8853\u7684\u9078\u64c7\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u81f3\u95dc\u91cd\u8981\u3002Redis\uff0c\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5df2\u7d93\u6210\u70ba\u8a31\u591a\u958b\u767c\u8005\u7684\u9996\u9078\u3002\u672c\u6587\u5c07\u63a2\u8a0eRedis\u7684\u8d77\u6e90\u53ca\u5176\u5728\u7269\u4ef6\u6578\u64da\u6620\u5c04\uff08ODM\uff09\u9818\u57df\u7684\u767c\u5c55\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e8%b5%b7%e6%ba%90-yCIKNpVdIK\">Redis\u7684\u8d77\u6e90<\/h2>\n<p>Redis\uff08Remote Dictionary Server\uff09\u6700\u521d\u7531Salvatore Sanfilippo\u65bc2009\u5e74\u958b\u767c\uff0c\u65e8\u5728\u63d0\u4f9b\u4e00\u7a2e\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u89e3\u6c7a\u65b9\u6848\u3002\u5b83\u7684\u8a2d\u8a08\u7406\u5ff5\u662f\u7c21\u55ae\u3001\u5feb\u901f\u4e14\u9748\u6d3b\uff0c\u80fd\u5920\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\u9019\u4f7f\u5f97Redis\u4e0d\u50c5\u50c5\u662f\u4e00\u500b\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u66f4\u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u6578\u64da\u7d50\u69cb\u4f3a\u670d\u5668\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e7%89%b9%e6%80%a7-yCIKNpVdIK\">Redis\u7684\u7279\u6027<\/h2>\n<p>Redis\u7684\u6210\u529f\u5728\u65bc\u5176\u591a\u6a23\u7684\u7279\u6027\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u80fd\u5920\u4ee5\u6beb\u79d2\u7d1a\u7684\u5ef6\u9072\u8655\u7406\u6578\u767e\u842c\u6b21\u8acb\u6c42\uff0c\u9019\u4f7f\u5f97\u5b83\u975e\u5e38\u9069\u5408\u9700\u8981\u5feb\u901f\u97ff\u61c9\u7684\u61c9\u7528\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\uff1a<\/strong>Redis\u652f\u6301\u591a\u7a2e\u6301\u4e45\u5316\u6a5f\u5236\uff0c\u5982RDB\u5feb\u7167\u548cAOF\u65e5\u8a8c\uff0c\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u3002<\/li>\n<li><strong>\u5206\u4f48\u5f0f\u652f\u6301\uff1a<\/strong>Redis Cluster\u5141\u8a31\u7528\u6236\u5728\u591a\u500b\u4f3a\u670d\u5668\u4e4b\u9593\u5206\u914d\u6578\u64da\uff0c\u5be6\u73fe\u9ad8\u53ef\u7528\u6027\u548c\u53ef\u64f4\u5c55\u6027\u3002<\/li>\n<li><strong>\u7c21\u55ae\u6613\u7528\uff1a<\/strong>Redis\u7684API\u8a2d\u8a08\u7c21\u55ae\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u4e0a\u624b\u3002<\/li>\n<\/ul>\n<h2 id=\"redis%e8%88%87odm%e7%9a%84%e9%97%9c%e4%bf%82-yCIKNpVdIK\">Redis\u8207ODM\u7684\u95dc\u4fc2<\/h2>\n<p>\u7269\u4ef6\u6578\u64da\u6620\u5c04\uff08ODM\uff09\u662f\u4e00\u7a2e\u5c07\u7269\u4ef6\u5c0e\u5411\u7de8\u7a0b\u8a9e\u8a00\u4e2d\u7684\u7269\u4ef6\u8207\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u9032\u884c\u6620\u5c04\u7684\u6280\u8853\u3002\u96a8\u8457Redis\u7684\u6d41\u884c\uff0c\u8a31\u591a\u958b\u767c\u8005\u958b\u59cb\u5c0b\u6c42\u5c07\u5176\u8207ODM\u7d50\u5408\u4f7f\u7528\uff0c\u4ee5\u4fbf\u66f4\u65b9\u4fbf\u5730\u64cd\u4f5c\u6578\u64da\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4f7f\u7528Node.js\u7684\u958b\u767c\u8005\u53ef\u4ee5\u5229\u7528<code>mongoose-redis<\/code>\u9019\u6a23\u7684\u5eab\uff0c\u5c07MongoDB\u7684ODM\u529f\u80fd\u64f4\u5c55\u5230Redis\u3002\u9019\u6a23\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5728Redis\u4e2d\u5b58\u5132\u548c\u6aa2\u7d22\u6578\u64da\uff0c\u540c\u6642\u4eab\u53d7ODM\u5e36\u4f86\u7684\u4fbf\u5229\u6027\u3002<\/p>\n<h3 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e4%bd%bf%e7%94%a8redis%e4%bd%9c%e7%82%baodm-yCIKNpVdIK\">\u793a\u4f8b\uff1a\u4f7f\u7528Redis\u4f5c\u70baODM<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Node.js\u548cRedis\u9032\u884c\u7269\u4ef6\u6578\u64da\u6620\u5c04\uff1a<\/p>\n<pre><code>\nconst redis = require('redis');\nconst client = redis.createClient();\n\nconst user = {\n    id: 1,\n    name: 'Alice',\n    age: 30\n};\n\n\/\/ \u5c07\u7528\u6236\u6578\u64da\u5b58\u5132\u5230Redis\nclient.hmset(`user:${user.id}`, user, (err, res) =&gt; {\n    if (err) throw err;\n    console.log('User saved:', res);\n});\n\n\/\/ \u5f9eRedis\u4e2d\u6aa2\u7d22\u7528\u6236\u6578\u64da\nclient.hgetall(`user:${user.id}`, (err, res) =&gt; {\n    if (err) throw err;\n    console.log('User retrieved:', res);\n});\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-yCIKNpVdIK\">\u7d50\u8ad6<\/h2>\n<p>Redis\u7684\u51fa\u73fe\u70ba\u6578\u64da\u5b58\u5132\u548c\u7ba1\u7406\u5e36\u4f86\u4e86\u9769\u547d\u6027\u7684\u8b8a\u5316\u3002\u5176\u9ad8\u6027\u80fd\u3001\u9748\u6d3b\u6027\u548c\u6613\u7528\u6027\u4f7f\u5176\u6210\u70ba\u8a31\u591a\u61c9\u7528\u7a0b\u5e8f\u7684\u7406\u60f3\u9078\u64c7\u3002\u96a8\u8457ODM\u6280\u8853\u7684\u767c\u5c55\uff0c\u958b\u767c\u8005\u80fd\u5920\u66f4\u65b9\u4fbf\u5730\u5c07Redis\u8207\u7269\u4ef6\u5c0e\u5411\u7de8\u7a0b\u7d50\u5408\uff0c\u9032\u4e00\u6b65\u63d0\u5347\u958b\u767c\u6548\u7387\u3002<\/p>\n<p>\u5c0d\u65bc\u5e0c\u671b\u5728\u9999\u6e2f\u5c0b\u627e\u9ad8\u6548\u80fd\u7684\u6578\u64da\u89e3\u6c7a\u65b9\u6848\u7684\u958b\u767c\u8005\uff0c<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u63d0\u4f9b\u4e86\u7a69\u5b9a\u7684\u74b0\u5883\u4f86\u904b\u884cRedis\u548c\u5176\u4ed6\u6578\u64da\u5eab\u6280\u8853\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u9805\u76ee\u9084\u662f\u5927\u578b\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684\u4f3a\u670d\u5668\u914d\u7f6e\u90fd\u662f\u6210\u529f\u7684\u95dc\u9375\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Redis\u7684\u8d77\u6e90\u53ca\u5176\u53d1\u5c55\u4e3a\u5bf9\u8c61\u6587\u6863\u6620\u5c04\uff08ODM\uff09\u7684\u5386\u7a0b\uff0c\u63ed\u793a\u5176\u5728\u6570\u636e\u5b58\u50a8\u4e2d\u7684\u91cd\u8981\u6027\u4e0e\u5e94\u7528\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-97582","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97582","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=97582"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97582\/revisions"}],"predecessor-version":[{"id":97583,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97582\/revisions\/97583"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}