{"id":124893,"date":"2024-10-25T18:04:20","date_gmt":"2024-10-25T10:04:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/124893\/"},"modified":"2024-10-25T18:04:20","modified_gmt":"2024-10-25T10:04:20","slug":"redis-%e6%9e%b6%e8%b5%b7%e5%85%b1%e4%ba%ab%e5%bf%ab%e5%8f%96%e7%9a%84%e6%a9%8b%e6%a8%91","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/124893\/","title":{"rendered":"Redis \u67b6\u8d77\u5171\u4eab\u5feb\u53d6\u7684\u6a4b\u6a11"},"content":{"rendered":"<h1 id=\"redis-%e6%9e%b6%e8%b5%b7%e5%85%b1%e4%ba%ab%e5%bf%ab%e5%8f%96%e7%9a%84%e6%a9%8b%e6%a8%91-wsTEjVbxZf\">Redis \u67b6\u8d77\u5171\u4eab\u5feb\u53d6\u7684\u6a4b\u6a11<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u8def\u61c9\u7528\u7a0b\u5f0f\u4e2d\uff0c\u6027\u80fd\u548c\u6548\u7387\u662f\u6210\u529f\u7684\u95dc\u9375\u56e0\u7d20\u4e4b\u4e00\u3002\u96a8\u8457\u7528\u6236\u9700\u6c42\u7684\u589e\u52a0\uff0c\u5982\u4f55\u5feb\u901f\u5b58\u53d6\u548c\u8655\u7406\u6578\u64da\u6210\u70ba\u4e86\u958b\u767c\u8005\u9762\u81e8\u7684\u4e00\u5927\u6311\u6230\u3002Redis\uff0c\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5df2\u7d93\u6210\u70ba\u67b6\u8d77\u5171\u4eab\u5feb\u53d6\u7684\u6a4b\u6a11\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-redis%ef%bc%9f-wsTEjVbxZf\">\u4ec0\u9ebc\u662f Redis\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\u4e0d\u50c5\u53ef\u4ee5\u7528\u4f5c\u6578\u64da\u5eab\uff0c\u9084\u53ef\u4ee5\u7528\u4f5c\u5feb\u53d6\u548c\u6d88\u606f\u4ee3\u7406\u3002\u7531\u65bc\u5176\u9ad8\u6548\u7684\u6027\u80fd\u548c\u9748\u6d3b\u7684\u6578\u64da\u7d50\u69cb\uff0cRedis \u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u5834\u666f\uff0c\u5305\u62ec\u7db2\u7ad9\u5feb\u53d6\u3001\u6703\u8a71\u5b58\u5132\u548c\u5373\u6642\u6578\u64da\u5206\u6790\u7b49\u3002<\/p>\n<h2 id=\"redis-%e7%9a%84%e5%84%aa%e5%8b%a2-wsTEjVbxZf\">Redis \u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis \u7684\u8b80\u53d6\u548c\u5beb\u5165\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u901a\u5e38\u5728\u5fae\u79d2\u7d1a\u5225\uff0c\u9019\u4f7f\u5f97\u5b83\u6210\u70ba\u9ad8\u6027\u80fd\u61c9\u7528\u7684\u7406\u60f3\u9078\u64c7\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>Redis \u63d0\u4f9b\u591a\u7a2e\u6301\u4e45\u5316\u6a5f\u5236\uff0c\u5982 RDB \u548c AOF\uff0c\u5141\u8a31\u7528\u6236\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u6301\u4e45\u5316\u7b56\u7565\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis \u4e0d\u50c5\u652f\u6301\u57fa\u672c\u7684\u9375\u503c\u5c0d\uff0c\u9084\u652f\u6301\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\uff0c\u9019\u4f7f\u5f97\u5b83\u5728\u8655\u7406\u591a\u6a23\u5316\u6578\u64da\u6642\u66f4\u52a0\u9748\u6d3b\u3002<\/li>\n<li><strong>\u5206\u4f48\u5f0f\u67b6\u69cb\uff1a<\/strong>Redis \u652f\u6301\u4e3b\u5f9e\u8907\u88fd\u548c\u5206\u7247\uff0c\u80fd\u5920\u8f15\u9b06\u64f4\u5c55\u4ee5\u61c9\u5c0d\u66f4\u9ad8\u7684\u6d41\u91cf\u9700\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-redis-%e4%bd%9c%e7%82%ba%e5%85%b1%e4%ba%ab%e5%bf%ab%e5%8f%96-wsTEjVbxZf\">\u5982\u4f55\u4f7f\u7528 Redis \u4f5c\u70ba\u5171\u4eab\u5feb\u53d6<\/h2>\n<p>\u5728\u8a31\u591a\u61c9\u7528\u4e2d\uff0c\u4f7f\u7528 Redis \u4f5c\u70ba\u5171\u4eab\u5feb\u53d6\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u4f7f\u7528\u5834\u666f\uff1a<\/p>\n<h3 id=\"1-%e7%b6%b2%e9%a0%81%e5%bf%ab%e5%8f%96-wsTEjVbxZf\">1. \u7db2\u9801\u5feb\u53d6<\/h3>\n<p>\u5c0d\u65bc\u52d5\u614b\u7db2\u7ad9\uff0c\u4f7f\u7528 Redis \u5feb\u53d6\u5e38\u898b\u7684\u67e5\u8a62\u7d50\u679c\u53ef\u4ee5\u6e1b\u5c11\u5c0d\u8cc7\u6599\u5eab\u7684\u8a2a\u554f\u6b21\u6578\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>const redis = require('redis');\nconst client = redis.createClient();\n\napp.get('\/data', (req, res) =&gt; {\n    const key = 'some_data_key';\n    \n    client.get(key, (err, data) =&gt; {\n        if (data) {\n            return res.send(JSON.parse(data));\n        } else {\n            \/\/ \u5047\u8a2d\u5f9e\u8cc7\u6599\u5eab\u7372\u53d6\u6578\u64da\n            const result = fetchDataFromDatabase();\n            client.setex(key, 3600, JSON.stringify(result)); \/\/ \u8a2d\u7f6e\u5feb\u53d6\uff0c1\u5c0f\u6642\u904e\u671f\n            return res.send(result);\n        }\n    });\n});<\/code><\/pre>\n<h3 id=\"2-%e6%9c%83%e8%a9%b1%e7%ae%a1%e7%90%86-wsTEjVbxZf\">2. \u6703\u8a71\u7ba1\u7406<\/h3>\n<p>Redis \u4e5f\u53ef\u4ee5\u7528\u4f86\u5b58\u5132\u7528\u6236\u6703\u8a71\u4fe1\u606f\uff0c\u9019\u6a23\u53ef\u4ee5\u5728\u591a\u500b\u4f3a\u670d\u5668\u4e4b\u9593\u5171\u4eab\u6703\u8a71\u6578\u64da\u3002\u9019\u5c0d\u65bc\u8ca0\u8f09\u5747\u8861\u7684\u61c9\u7528\u7279\u5225\u91cd\u8981\u3002<\/p>\n<pre><code>const session = require('express-session');\nconst RedisStore = require('connect-redis')(session);\n\napp.use(session({\n    store: new RedisStore({ client: redisClient }),\n    secret: 'your_secret',\n    resave: false,\n    saveUninitialized: false\n}));<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-wsTEjVbxZf\">\u7d50\u8ad6<\/h2>\n<p>Redis \u4f5c\u70ba\u4e00\u500b\u9ad8\u6548\u7684\u5171\u4eab\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u986f\u8457\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u3002\u7121\u8ad6\u662f\u7528\u65bc\u7db2\u9801\u5feb\u53d6\u9084\u662f\u6703\u8a71\u7ba1\u7406\uff0cRedis \u90fd\u63d0\u4f9b\u4e86\u9748\u6d3b\u4e14\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u96a8\u8457\u6280\u8853\u7684\u9032\u6b65\uff0c\u8d8a\u4f86\u8d8a\u591a\u7684\u958b\u767c\u8005\u9078\u64c7 Redis \u4f5c\u70ba\u4ed6\u5011\u7684\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff0c\u4ee5\u61c9\u5c0d\u65e5\u76ca\u589e\u9577\u7684\u7528\u6236\u9700\u6c42\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\u4f86\u90e8\u7f72\u60a8\u7684 Redis \u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u60a8\u8f15\u9b06\u642d\u5efa\u9ad8\u6548\u7684\u61c9\u7528\u74b0\u5883\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a> \u9084\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u670d\u52a1\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis \u67b6\u8d77\u5171\u4eab\u5feb\u53d6\u7684\u6a4b\u6a11\uff0c\u63d0\u5347\u61c9\u7528\u6548\u80fd\uff0c\u5be6\u73fe\u5feb\u901f\u6578\u64da\u5b58\u53d6\uff0c\u512a\u5316\u7528\u6236\u9ad4\u9a57\uff0c\u52a9\u529b\u696d\u52d9\u589e\u9577\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-124893","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/124893","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=124893"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/124893\/revisions"}],"predecessor-version":[{"id":124894,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/124893\/revisions\/124894"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=124893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=124893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=124893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}