{"id":188026,"date":"2024-11-11T19:10:20","date_gmt":"2024-11-11T11:10:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/188026\/"},"modified":"2024-11-11T19:10:21","modified_gmt":"2024-11-11T11:10:21","slug":"%e6%95%b8%e5%88%a9%e7%94%a8redis%e5%af%a6%e7%8f%behttp%e8%ab%8b%e6%b1%82%e7%b5%b1%e8%a8%88%ef%bc%88redis-%e7%b5%b1%e8%a8%88%e8%ab%8b%e6%b1%82%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/188026\/","title":{"rendered":"\u6578\u5229\u7528Redis\u5be6\u73feHTTP\u8acb\u6c42\u7d71\u8a08\uff08redis \u7d71\u8a08\u8acb\u6c42\uff09"},"content":{"rendered":"<h1 id=\"%e6%95%b8%e5%88%a9%e7%94%a8redis%e5%af%a6%e7%8f%behttp%e8%ab%8b%e6%b1%82%e7%b5%b1%e8%a8%88%ef%bc%88redis-%e7%b5%b1%e8%a8%88%e8%ab%8b%e6%b1%82%ef%bc%89-jAVfLMSdMT\">\u6578\u5229\u7528Redis\u5be6\u73feHTTP\u8acb\u6c42\u7d71\u8a08\uff08redis \u7d71\u8a08\u8acb\u6c42\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u74b0\u5883\u4e2d\uff0cHTTP\u8acb\u6c42\u7684\u7d71\u8a08\u5c0d\u65bc\u7db2\u7ad9\u7684\u6027\u80fd\u512a\u5316\u548c\u7528\u6236\u884c\u70ba\u5206\u6790\u81f3\u95dc\u91cd\u8981\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u56e0\u5176\u5feb\u901f\u7684\u6578\u64da\u8b80\u53d6\u548c\u5beb\u5165\u80fd\u529b\uff0c\u6210\u70ba\u4e86\u5be6\u73feHTTP\u8acb\u6c42\u7d71\u8a08\u7684\u7406\u60f3\u9078\u64c7\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u4f86\u5be6\u73feHTTP\u8acb\u6c42\u7684\u7d71\u8a08\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87redis%ef%bc%9f-jAVfLMSdMT\">\u70ba\u4ec0\u9ebc\u9078\u64c7Redis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u958b\u6e90\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u5177\u6709\u4ee5\u4e0b\u5e7e\u500b\u7279\u9ede\uff0c\u4f7f\u5176\u975e\u5e38\u9069\u5408\u7528\u65bcHTTP\u8acb\u6c42\u7d71\u8a08\uff1a<\/p>\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\u5be6\u6642\u7d71\u8a08\u7684\u61c9\u7528\u4f86\u8aaa\u81f3\u95dc\u91cd\u8981\u3002<\/li>\n<li><strong>\u6578\u64da\u7d50\u69cb\u9748\u6d3b\uff1a<\/strong>Redis\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\uff0c\u9019\u4f7f\u5f97\u5b83\u80fd\u5920\u9748\u6d3b\u5730\u5b58\u5132\u548c\u8655\u7406\u5404\u7a2e\u7d71\u8a08\u6578\u64da\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u9078\u9805\uff1a<\/strong>Redis\u63d0\u4f9b\u4e86\u591a\u7a2e\u6301\u4e45\u5316\u6a5f\u5236\uff0c\u80fd\u5920\u5728\u7cfb\u7d71\u91cd\u555f\u5f8c\u4fdd\u7559\u6578\u64da\u3002<\/li>\n<\/ul>\n<h2 id=\"http%e8%ab%8b%e6%b1%82%e7%b5%b1%e8%a8%88%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%80%9d%e8%b7%af-jAVfLMSdMT\">HTTP\u8acb\u6c42\u7d71\u8a08\u7684\u57fa\u672c\u601d\u8def<\/h2>\n<p>HTTP\u8acb\u6c42\u7d71\u8a08\u7684\u57fa\u672c\u601d\u8def\u662f\u8a18\u9304\u6bcf\u4e00\u500b\u9032\u5165\u7684\u8acb\u6c42\uff0c\u4e26\u6839\u64da\u9700\u8981\u9032\u884c\u7d71\u8a08\u3002\u9019\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\u4f86\u5be6\u73fe\uff1a<\/p>\n<ol>\n<li>\u5728\u6bcf\u6b21\u63a5\u6536\u5230HTTP\u8acb\u6c42\u6642\uff0c\u5c07\u8acb\u6c42\u7684\u76f8\u95dc\u4fe1\u606f\uff08\u5982\u8acb\u6c42\u6642\u9593\u3001\u8acb\u6c42\u8def\u5f91\u3001\u7528\u6236IP\u7b49\uff09\u5beb\u5165Redis\u3002<\/li>\n<li>\u5b9a\u671f\u5f9eRedis\u4e2d\u8b80\u53d6\u6578\u64da\uff0c\u9032\u884c\u7d71\u8a08\u5206\u6790\uff0c\u4f8b\u5982\u8a08\u7b97\u6bcf\u500b\u8def\u5f91\u7684\u8acb\u6c42\u6b21\u6578\u3001\u8acb\u6c42\u7684\u5e73\u5747\u97ff\u61c9\u6642\u9593\u7b49\u3002<\/li>\n<li>\u5c07\u7d71\u8a08\u7d50\u679c\u5b58\u5132\u5230\u6578\u64da\u5eab\u6216\u5176\u4ed6\u6301\u4e45\u5316\u5b58\u5132\u4e2d\uff0c\u4ee5\u4fbf\u5f8c\u7e8c\u67e5\u8a62\u548c\u5206\u6790\u3002<\/li>\n<\/ol>\n<h2 id=\"%e5%af%a6%e7%8f%be%e7%a4%ba%e4%be%8b-jAVfLMSdMT\">\u5be6\u73fe\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684Node.js\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Redis\u4f86\u7d71\u8a08HTTP\u8acb\u6c42\uff1a<\/p>\n<pre><code>const express = require('express');\nconst redis = require('redis');\nconst client = redis.createClient();\n\nconst app = express();\nconst PORT = 3000;\n\napp.use((req, res, next) =&gt; {\n    const currentTime = new Date().toISOString();\n    const requestPath = req.path;\n\n    \/\/ \u5c07\u8acb\u6c42\u4fe1\u606f\u5beb\u5165Redis\n    client.hincrby('http_requests', requestPath, 1, (err, reply) =&gt; {\n        if (err) {\n            console.error('Error writing to Redis:', err);\n        }\n    });\n\n    next();\n});\n\napp.get('\/', (req, res) =&gt; {\n    res.send('Hello World!');\n});\n\napp.listen(PORT, () =&gt; {\n    console.log(`Server is running on http:\/\/localhost:${PORT}`);\n});<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528Express\u6846\u67b6\u4f86\u8655\u7406HTTP\u8acb\u6c42\uff0c\u4e26\u5728\u6bcf\u6b21\u8acb\u6c42\u6642\u5c07\u8acb\u6c42\u8def\u5f91\u7684\u8a08\u6578\u5beb\u5165Redis\u7684\u54c8\u5e0c\u8868\u4e2d\u3002\u9019\u6a23\uff0c\u6211\u5011\u5c31\u80fd\u5920\u5be6\u6642\u7d71\u8a08\u6bcf\u500b\u8def\u5f91\u7684\u8acb\u6c42\u6b21\u6578\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e7%b5%b1%e8%a8%88%e8%88%87%e5%88%86%e6%9e%90-jAVfLMSdMT\">\u6578\u64da\u7d71\u8a08\u8207\u5206\u6790<\/h2>\n<p>\u4e00\u65e6\u6211\u5011\u5c07\u8acb\u6c42\u6578\u64da\u5beb\u5165Redis\uff0c\u5c31\u53ef\u4ee5\u5b9a\u671f\u5f9e\u4e2d\u8b80\u53d6\u6578\u64da\u9032\u884c\u7d71\u8a08\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5f9eRedis\u4e2d\u7372\u53d6\u8acb\u6c42\u7d71\u8a08\uff1a<\/p>\n<pre><code>client.hgetall('http_requests', (err, data) =&gt; {\n    if (err) {\n        console.error('Error reading from Redis:', err);\n        return;\n    }\n    console.log('HTTP Request Statistics:', data);\n});<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u5f9eRedis\u4e2d\u7372\u53d6\u6240\u6709\u7684HTTP\u8acb\u6c42\u7d71\u8a08\u6578\u64da\uff0c\u4e26\u5c07\u5176\u6253\u5370\u5230\u63a7\u5236\u53f0\u3002\u6839\u64da\u9019\u4e9b\u6578\u64da\uff0c\u6211\u5011\u53ef\u4ee5\u9032\u4e00\u6b65\u5206\u6790\u7db2\u7ad9\u7684\u6d41\u91cf\u8da8\u52e2\u3001\u71b1\u9580\u8def\u5f91\u7b49\u4fe1\u606f\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-jAVfLMSdMT\">\u7e3d\u7d50<\/h2>\n<p>\u5229\u7528Redis\u5be6\u73feHTTP\u8acb\u6c42\u7d71\u8a08\u662f\u4e00\u7a2e\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u65b9\u6cd5\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5feb\u901f\u7372\u53d6\u7db2\u7ad9\u7684\u6d41\u91cf\u6578\u64da\u548c\u7528\u6236\u884c\u70ba\u5206\u6790\u3002\u901a\u904e\u7c21\u55ae\u7684\u4ee3\u78bc\u5be6\u73fe\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u8acb\u6c42\u6578\u64da\u5b58\u5132\u5230Redis\u4e2d\uff0c\u4e26\u9032\u884c\u5f8c\u7e8c\u7684\u7d71\u8a08\u548c\u5206\u6790\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6027\u80fd\u548c\u5be6\u6642\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\uff0c\u9078\u64c7\u5408\u9069\u7684\u670d\u52d9\u5668\u74b0\u5883\uff0c\u5982<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\uff0c\u5c07\u9032\u4e00\u6b65\u63d0\u5347\u7cfb\u7d71\u7684\u6574\u9ad4\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528Redis\u5be6\u73feHTTP\u8acb\u6c42\u7d71\u8a08\uff0c\u8f15\u9b06\u8ffd\u8e64\u548c\u5206\u6790\u8acb\u6c42\u6578\u64da\uff0c\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\u8207\u76e3\u63a7\u6548\u7387\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-188026","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188026","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=188026"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188026\/revisions"}],"predecessor-version":[{"id":188027,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/188026\/revisions\/188027"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=188026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=188026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=188026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}