{"id":163005,"date":"2024-11-04T19:51:21","date_gmt":"2024-11-04T11:51:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/163005\/"},"modified":"2024-11-04T19:51:21","modified_gmt":"2024-11-04T11:51:21","slug":"%e6%8e%a5%e5%8f%a3%e8%88%87redis%e5%af%a6%e7%8f%be%e5%bf%ab%e9%80%9f%e5%90%8c%e6%ad%a5%ef%bc%88%e6%8e%a5%e5%8f%a3%e8%88%87redis%e5%90%8c%e6%ad%a5%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/163005\/","title":{"rendered":"\u63a5\u53e3\u8207Redis\u5be6\u73fe\u5feb\u901f\u540c\u6b65\uff08\u63a5\u53e3\u8207Redis\u540c\u6b65\uff09"},"content":{"rendered":"<h1 id=\"%e6%8e%a5%e5%8f%a3%e8%88%87redis%e5%af%a6%e7%8f%be%e5%bf%ab%e9%80%9f%e5%90%8c%e6%ad%a5%ef%bc%88%e6%8e%a5%e5%8f%a3%e8%88%87redis%e5%90%8c%e6%ad%a5%ef%bc%89-BKpXRCCAvV\">\u63a5\u53e3\u8207Redis\u5be6\u73fe\u5feb\u901f\u540c\u6b65\uff08\u63a5\u53e3\u8207Redis\u540c\u6b65\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6578\u64da\u7684\u5373\u6642\u6027\u548c\u4e00\u81f4\u6027\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u96a8\u8457\u5fae\u670d\u52d9\u67b6\u69cb\u7684\u666e\u53ca\uff0c\u5982\u4f55\u9ad8\u6548\u5730\u5728\u4e0d\u540c\u670d\u52d9\u4e4b\u9593\u540c\u6b65\u6578\u64da\u6210\u70ba\u4e86\u4e00\u500b\u91cd\u8981\u7684\u8ab2\u984c\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u56e0\u5176\u5feb\u901f\u7684\u8b80\u5beb\u6027\u80fd\u548c\u8c50\u5bcc\u7684\u6578\u64da\u7d50\u69cb\uff0c\u6210\u70ba\u4e86\u5be6\u73fe\u6578\u64da\u540c\u6b65\u7684\u7406\u60f3\u9078\u64c7\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u901a\u904e\u63a5\u53e3\u8207Redis\u5be6\u73fe\u5feb\u901f\u540c\u6b65\u3002<\/p>\n<h2 id=\"redis%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-BKpXRCCAvV\">Redis\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>Redis\uff08Remote Dictionary Server\uff09\u662f\u4e00\u500b\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\u5b83\u7684\u4e3b\u8981\u7279\u9ede\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd\uff1a<\/strong>Redis\u80fd\u5920\u6bcf\u79d2\u8655\u7406\u6578\u5341\u842c\u6b21\u7684\u8acb\u6c42\uff0c\u9019\u4f7f\u5f97\u5b83\u975e\u5e38\u9069\u5408\u7528\u65bc\u9700\u8981\u9ad8\u541e\u5410\u91cf\u7684\u61c9\u7528\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\uff1a<\/strong>Redis\u652f\u6301\u5c07\u6578\u64da\u6301\u4e45\u5316\u5230\u78c1\u789f\uff0c\u9019\u6a23\u5373\u4f7f\u5728\u670d\u52d9\u5668\u91cd\u555f\u5f8c\uff0c\u6578\u64da\u4e5f\u4e0d\u6703\u4e1f\u5931\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff1a<\/strong>Redis\u4e0d\u50c5\u50c5\u662f\u9375\u503c\u5b58\u5132\uff0c\u9084\u652f\u6301\u5217\u8868\u3001\u96c6\u5408\u3001\u6392\u5e8f\u96c6\u5408\u7b49\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u9019\u4f7f\u5f97\u5b83\u5728\u8655\u7406\u8907\u96dc\u6578\u64da\u6642\u66f4\u52a0\u9748\u6d3b\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%8e%a5%e5%8f%a3%e7%9a%84%e8%a8%ad%e8%a8%88-BKpXRCCAvV\">\u63a5\u53e3\u7684\u8a2d\u8a08<\/h2>\n<p>\u5728\u5be6\u73fe\u63a5\u53e3\u8207Redis\u7684\u540c\u6b65\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u8a2d\u8a08\u4e00\u500b\u5408\u9069\u7684API\u63a5\u53e3\u3002\u9019\u500b\u63a5\u53e3\u61c9\u8a72\u80fd\u5920\u63a5\u6536\u6578\u64da\u8acb\u6c42\uff0c\u4e26\u5c07\u6578\u64da\u5b58\u5132\u5230Redis\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684RESTful API\u8a2d\u8a08\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nPOST \/api\/data\n{\n    \"key\": \"exampleKey\",\n    \"value\": \"exampleValue\"\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u5ba2\u6236\u7aef\u901a\u904ePOST\u8acb\u6c42\u5c07\u6578\u64da\u767c\u9001\u5230\u670d\u52d9\u5668\uff0c\u670d\u52d9\u5668\u63a5\u6536\u5230\u8acb\u6c42\u5f8c\uff0c\u5c07\u6578\u64da\u5b58\u5132\u5230Redis\u4e2d\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Node.js\u548cExpress\u6846\u67b6\u5be6\u73fe\u7684\u7c21\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nconst express = require('express');\nconst redis = require('redis');\nconst bodyParser = require('body-parser');\n\nconst app = express();\nconst client = redis.createClient();\n\napp.use(bodyParser.json());\n\napp.post('\/api\/data', (req, res) =&gt; {\n    const { key, value } = req.body;\n    client.set(key, value, (err, reply) =&gt; {\n        if (err) {\n            return res.status(500).send(err);\n        }\n        res.status(200).send(reply);\n    });\n});\n\napp.listen(3000, () =&gt; {\n    console.log('Server is running on port 3000');\n});\n<\/code><\/pre>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%90%8c%e6%ad%a5%e7%9a%84%e5%af%a6%e7%8f%be-BKpXRCCAvV\">\u6578\u64da\u540c\u6b65\u7684\u5be6\u73fe<\/h2>\n<p>\u5728\u63a5\u53e3\u8a2d\u8a08\u5b8c\u6210\u5f8c\uff0c\u63a5\u4e0b\u4f86\u9700\u8981\u8003\u616e\u5982\u4f55\u5be6\u73fe\u6578\u64da\u7684\u5feb\u901f\u540c\u6b65\u3002\u9019\u88e1\u6709\u5e7e\u7a2e\u5e38\u898b\u7684\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><strong>\u76f4\u63a5\u5beb\u5165\uff1a<\/strong>\u7576\u6578\u64da\u901a\u904eAPI\u63a5\u53e3\u5beb\u5165\u6642\uff0c\u76f4\u63a5\u5c07\u6578\u64da\u5beb\u5165Redis\u3002\u9019\u662f\u6700\u7c21\u55ae\u7684\u65b9\u5f0f\uff0c\u4f46\u5728\u9ad8\u4e26\u767c\u7684\u60c5\u6cc1\u4e0b\u53ef\u80fd\u6703\u5c0e\u81f4\u6027\u80fd\u74f6\u9838\u3002<\/li>\n<li><strong>\u6279\u91cf\u5beb\u5165\uff1a<\/strong>\u5c07\u591a\u500b\u6578\u64da\u8acb\u6c42\u9032\u884c\u6279\u91cf\u8655\u7406\uff0c\u7136\u5f8c\u4e00\u6b21\u6027\u5beb\u5165Redis\u3002\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u7db2\u7d61\u8acb\u6c42\u7684\u6b21\u6578\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<li><strong>\u4f7f\u7528\u6d88\u606f\u968a\u5217\uff1a<\/strong>\u5c07\u6578\u64da\u8acb\u6c42\u653e\u5165\u6d88\u606f\u968a\u5217\u4e2d\uff0c\u7136\u5f8c\u7531\u6d88\u8cbb\u8005\u5f9e\u968a\u5217\u4e2d\u8b80\u53d6\u6578\u64da\u4e26\u5beb\u5165Redis\u3002\u9019\u6a23\u53ef\u4ee5\u5be6\u73fe\u89e3\u8026\uff0c\u4e26\u63d0\u9ad8\u7cfb\u7d71\u7684\u53ef\u64f4\u5c55\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e4%bd%bf%e7%94%a8%e6%b6%88%e6%81%af%e9%9a%8a%e5%88%97%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%90%8c%e6%ad%a5-BKpXRCCAvV\">\u793a\u4f8b\uff1a\u4f7f\u7528\u6d88\u606f\u968a\u5217\u9032\u884c\u6578\u64da\u540c\u6b65<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528RabbitMQ\u4f5c\u70ba\u6d88\u606f\u968a\u5217\u7684\u7c21\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nconst amqp = require('amqplib\/callback_api');\n\namqp.connect('amqp:\/\/localhost', (error0, connection) =&gt; {\n    if (error0) throw error0;\n    connection.createChannel((error1, channel) =&gt; {\n        if (error1) throw error1;\n        const queue = 'dataQueue';\n\n        channel.assertQueue(queue, { durable: false });\n\n        app.post('\/api\/data', (req, res) =&gt; {\n            const { key, value } = req.body;\n            channel.sendToQueue(queue, Buffer.from(JSON.stringify({ key, value })));\n            res.status(200).send('Data sent to queue');\n        });\n\n        channel.consume(queue, (msg) =&gt; {\n            const { key, value } = JSON.parse(msg.content.toString());\n            client.set(key, value);\n            channel.ack(msg);\n        });\n    });\n});\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-BKpXRCCAvV\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u63a5\u53e3\u8207Redis\u7684\u7d50\u5408\uff0c\u53ef\u4ee5\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u540c\u6b65\uff0c\u6eff\u8db3\u73fe\u4ee3\u61c9\u7528\u5c0d\u5373\u6642\u6027\u548c\u4e00\u81f4\u6027\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u76f4\u63a5\u5beb\u5165\u3001\u6279\u91cf\u8655\u7406\u9084\u662f\u4f7f\u7528\u6d88\u606f\u968a\u5217\uff0c\u9078\u64c7\u5408\u9069\u7684\u65b9\u6848\u90fd\u80fd\u986f\u8457\u63d0\u9ad8\u7cfb\u7d71\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6027\u80fd\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4e5f\u662f\u81f3\u95dc\u91cd\u8981\u7684\uff0c\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u7cfb\u7d71\u5728\u9ad8\u8ca0\u8f09\u4e0b\u4f9d\u7136\u7a69\u5b9a\u904b\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u901a\u904e\u63a5\u53e3\u8207Redis\u5be6\u73fe\u5feb\u901f\u6578\u64da\u540c\u6b65\uff0c\u63d0\u9ad8\u61c9\u7528\u6027\u80fd\u548c\u97ff\u61c9\u901f\u5ea6\uff0c\u512a\u5316\u6578\u64da\u8655\u7406\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-163005","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163005","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=163005"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163005\/revisions"}],"predecessor-version":[{"id":163006,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/163005\/revisions\/163006"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=163005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=163005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=163005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}