{"id":167493,"date":"2024-11-06T01:07:19","date_gmt":"2024-11-05T17:07:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/167493\/"},"modified":"2024-11-06T01:07:20","modified_gmt":"2024-11-05T17:07:20","slug":"%e5%88%a9%e7%94%a8redis%e7%b0%a1%e5%8c%96api%e8%aa%bf%e7%94%a8%e6%b5%81%e7%a8%8b%ef%bc%88redis%e8%aa%bf%e7%94%a8api%ef%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/167493\/","title":{"rendered":"\u5229\u7528Redis\u7c21\u5316API\u8abf\u7528\u6d41\u7a0b\uff08redis\u8abf\u7528api\uff09"},"content":{"rendered":"<h1 id=\"%e5%88%a9%e7%94%a8redis%e7%b0%a1%e5%8c%96api%e8%aa%bf%e7%94%a8%e6%b5%81%e7%a8%8b%ef%bc%88redis%e8%aa%bf%e7%94%a8api%ef%bc%89-oAGCsdUyCc\">\u5229\u7528Redis\u7c21\u5316API\u8abf\u7528\u6d41\u7a0b\uff08redis\u8abf\u7528api\uff09<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0cAPI\uff08\u61c9\u7528\u7a0b\u5f0f\u4ecb\u9762\uff09\u626e\u6f14\u8457\u81f3\u95dc\u91cd\u8981\u7684\u89d2\u8272\u3002\u96a8\u8457\u7528\u6236\u9700\u6c42\u7684\u589e\u52a0\uff0cAPI\u7684\u8abf\u7528\u983b\u7387\u4e5f\u96a8\u4e4b\u4e0a\u5347\uff0c\u9019\u4f7f\u5f97\u7cfb\u7d71\u7684\u6027\u80fd\u548c\u97ff\u61c9\u901f\u5ea6\u6210\u70ba\u958b\u767c\u8005\u9700\u8981\u91cd\u8996\u7684\u554f\u984c\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5167\u5b58\u6578\u64da\u5eab\uff0c\u80fd\u5920\u6709\u6548\u5730\u7c21\u5316API\u8abf\u7528\u6d41\u7a0b\uff0c\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528Redis\u4f86\u512a\u5316API\u8abf\u7528\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\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-oAGCsdUyCc\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\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\u7531\u65bc\u5176\u9ad8\u6548\u7684\u6578\u64da\u5b58\u53d6\u901f\u5ea6\u548c\u8c50\u5bcc\u7684\u529f\u80fd\uff0cRedis\u88ab\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u5be6\u6642\u6578\u64da\u8655\u7406\u7b49\u5834\u666f\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%bd%bf%e7%94%a8redis%e4%be%86%e7%b0%a1%e5%8c%96api%e8%aa%bf%e7%94%a8%ef%bc%9f-oAGCsdUyCc\">\u70ba\u4ec0\u9ebc\u4f7f\u7528Redis\u4f86\u7c21\u5316API\u8abf\u7528\uff1f<\/h2>\n<p>\u4f7f\u7528Redis\u4f86\u7c21\u5316API\u8abf\u7528\u6d41\u7a0b\u7684\u4e3b\u8981\u539f\u56e0\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u63d0\u9ad8\u97ff\u61c9\u901f\u5ea6\uff1a<\/strong>Redis\u7684\u6578\u64da\u5b58\u53d6\u901f\u5ea6\u975e\u5e38\u5feb\uff0c\u80fd\u5920\u986f\u8457\u6e1b\u5c11API\u7684\u97ff\u61c9\u6642\u9593\u3002<\/li>\n<li><strong>\u6e1b\u5c11\u5f8c\u7aef\u8ca0\u64d4\uff1a<\/strong>\u901a\u904e\u7de9\u5b58API\u7684\u7d50\u679c\uff0c\u53ef\u4ee5\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u670d\u52d9\u5668\u7684\u8acb\u6c42\uff0c\u5f9e\u800c\u964d\u4f4e\u5176\u8ca0\u64d4\u3002<\/li>\n<li><strong>\u652f\u6301\u9ad8\u4f75\u767c\uff1a<\/strong>Redis\u80fd\u5920\u8655\u7406\u5927\u91cf\u7684\u4e26\u767c\u8acb\u6c42\uff0c\u9069\u5408\u9ad8\u6d41\u91cf\u7684\u61c9\u7528\u5834\u666f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8redis%e7%b0%a1%e5%8c%96api%e8%aa%bf%e7%94%a8%e6%b5%81%e7%a8%8b-oAGCsdUyCc\">\u5982\u4f55\u4f7f\u7528Redis\u7c21\u5316API\u8abf\u7528\u6d41\u7a0b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5229\u7528Redis\u7c21\u5316API\u8abf\u7528\u6d41\u7a0b\u7684\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e5%ae%89%e8%a3%9dredis-oAGCsdUyCc\">1. \u5b89\u88ddRedis<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5728\u60a8\u7684\u4f3a\u670d\u5668\u4e0a\u5b89\u88ddRedis\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5728Ubuntu\u4e0a\u5b89\u88dd\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install redis-server<\/code><\/pre>\n<h3 id=\"2-%e9%85%8d%e7%bd%aeredis-oAGCsdUyCc\">2. \u914d\u7f6eRedis<\/h3>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u60a8\u53ef\u4ee5\u901a\u904e\u7de8\u8f2f\u914d\u7f6e\u6587\u4ef6\u4f86\u8abf\u6574Redis\u7684\u8a2d\u7f6e\u3002\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u4f4d\u65bc \/etc\/redis\/redis.conf\u3002\u60a8\u53ef\u4ee5\u6839\u64da\u9700\u8981\u4fee\u6539\u4e00\u4e9b\u53c3\u6578\uff0c\u4f8b\u5982\u6700\u5927\u5167\u5b58\u4f7f\u7528\u91cf\u548c\u6301\u4e45\u5316\u8a2d\u7f6e\u3002<\/p>\n<h3 id=\"3-%e5%9c%a8api%e4%b8%ad%e9%9b%86%e6%88%90redis-oAGCsdUyCc\">3. \u5728API\u4e2d\u96c6\u6210Redis<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u5728\u60a8\u7684API\u4e2d\u96c6\u6210Redis\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528Node.js\u548cExpress\u6846\u67b6\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>const express = require('express');\nconst redis = require('redis');\nconst app = express();\nconst client = redis.createClient();\n\nclient.on('error', (err) =&gt; {\n    console.log('Redis error: ' + err);\n});\n\napp.get('\/api\/data', (req, res) =&gt; {\n    const key = 'api_data';\n\n    client.get(key, (err, data) =&gt; {\n        if (err) throw err;\n\n        if (data) {\n            \/\/ \u5982\u679c\u7de9\u5b58\u4e2d\u6709\u6578\u64da\uff0c\u76f4\u63a5\u8fd4\u56de\n            res.send(JSON.parse(data));\n        } else {\n            \/\/ \u5426\u5247\uff0c\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u6578\u64da\n            const apiData = { message: 'Hello, World!' }; \/\/ \u5047\u8a2d\u9019\u662f\u5f9e\u6578\u64da\u5eab\u7372\u53d6\u7684\u6578\u64da\n\n            \/\/ \u5c07\u6578\u64da\u5b58\u5165Redis\u7de9\u5b58\n            client.setex(key, 3600, JSON.stringify(apiData)); \/\/ \u8a2d\u7f6e\u7de9\u5b58\u6709\u6548\u671f\u70ba1\u5c0f\u6642\n            res.send(apiData);\n        }\n    });\n});\n\napp.listen(3000, () =&gt; {\n    console.log('Server is running on port 3000');\n});<\/code><\/pre>\n<h3 id=\"4-%e6%b8%ac%e8%a9%a6api-oAGCsdUyCc\">4. \u6e2c\u8a66API<\/h3>\n<p>\u555f\u52d5\u60a8\u7684\u4f3a\u670d\u5668\u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Postman\u6216\u5176\u4ed6API\u6e2c\u8a66\u5de5\u5177\u4f86\u6e2c\u8a66\u60a8\u7684API\u3002\u7b2c\u4e00\u6b21\u8acb\u6c42\u5c07\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u6578\u64da\u4e26\u5b58\u5165Redis\u7de9\u5b58\uff0c\u96a8\u5f8c\u7684\u8acb\u6c42\u5c07\u76f4\u63a5\u5f9eRedis\u4e2d\u7372\u53d6\u6578\u64da\uff0c\u986f\u8457\u63d0\u9ad8\u97ff\u61c9\u901f\u5ea6\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-oAGCsdUyCc\">\u7d50\u8ad6<\/h2>\n<p>\u5229\u7528Redis\u4f86\u7c21\u5316API\u8abf\u7528\u6d41\u7a0b\u4e0d\u50c5\u80fd\u63d0\u9ad8\u7cfb\u7d71\u7684\u6027\u80fd\uff0c\u9084\u80fd\u6e1b\u5c11\u5f8c\u7aef\u670d\u52d9\u5668\u7684\u8ca0\u64d4\u3002\u901a\u904e\u7de9\u5b58API\u7684\u7d50\u679c\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u61c9\u5c0d\u9ad8\u6d41\u91cf\u7684\u8acb\u6c42\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002\u96a8\u8457\u6280\u8853\u7684\u767c\u5c55\uff0cRedis\u7684\u61c9\u7528\u5834\u666f\u5c07\u6703\u8d8a\u4f86\u8d8a\u5ee3\u6cdb\uff0c\u6210\u70ba\u958b\u767c\u8005\u512a\u5316\u7cfb\u7d71\u6027\u80fd\u7684\u91cd\u8981\u5de5\u5177\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u652f\u6301\u60a8\u7684\u696d\u52d9\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5229\u7528Redis\u7c21\u5316API\u8abf\u7528\u6d41\u7a0b\uff0c\u63d0\u9ad8\u6548\u7387\u8207\u6027\u80fd\uff0c\u8b93\u958b\u767c\u66f4\u8f15\u9b06\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-167493","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167493","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=167493"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167493\/revisions"}],"predecessor-version":[{"id":167494,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/167493\/revisions\/167494"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=167493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=167493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=167493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}