{"id":151863,"date":"2024-11-01T18:07:21","date_gmt":"2024-11-01T10:07:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/151863\/"},"modified":"2024-11-01T18:07:22","modified_gmt":"2024-11-01T10:07:22","slug":"%e4%bf%9d%e9%9a%9c%e5%bf%ab%e5%8f%96%e6%95%b8%e6%93%9a%e4%b8%80%e8%87%b4%e6%80%a7redis%e9%9b%99%e5%af%ab%e6%a9%9f%e5%88%b6%ef%bc%88redis%e5%bf%ab%e5%8f%96%e9%9b%99%e5%af%ab%e4%b8%80%e8%87%b4%e6%80%a7","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/151863\/","title":{"rendered":"\u4fdd\u969c\u5feb\u53d6\u6578\u64da\u4e00\u81f4\u6027Redis\u96d9\u5beb\u6a5f\u5236\uff08redis\u5feb\u53d6\u96d9\u5beb\u4e00\u81f4\u6027\uff09"},"content":{"rendered":"<h1 id=\"%e4%bf%9d%e9%9a%9c%e5%bf%ab%e5%8f%96%e6%95%b8%e6%93%9a%e4%b8%80%e8%87%b4%e6%80%a7%ef%bc%9aredis%e9%9b%99%e5%af%ab%e6%a9%9f%e5%88%b6-psTLIimdpb\">\u4fdd\u969c\u5feb\u53d6\u6578\u64da\u4e00\u81f4\u6027\uff1aRedis\u96d9\u5beb\u6a5f\u5236<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u61c9\u7528\u7a0b\u5f0f\u67b6\u69cb\u4e2d\uff0c\u5feb\u53d6\u6280\u8853\u5df2\u6210\u70ba\u63d0\u5347\u6027\u80fd\u548c\u7528\u6236\u9ad4\u9a57\u7684\u91cd\u8981\u624b\u6bb5\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u5feb\u53d6\u89e3\u6c7a\u65b9\u6848\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u5834\u666f\u3002\u7136\u800c\uff0c\u96a8\u8457\u7cfb\u7d71\u7684\u8907\u96dc\u6027\u589e\u52a0\uff0c\u6578\u64da\u4e00\u81f4\u6027\u554f\u984c\u9010\u6f38\u6d6e\u73fe\uff0c\u7279\u5225\u662f\u5728\u4f7f\u7528\u5feb\u53d6\u7684\u60c5\u6cc1\u4e0b\u3002\u70ba\u4e86\u89e3\u6c7a\u9019\u4e00\u554f\u984c\uff0cRedis\u7684\u96d9\u5beb\u6a5f\u5236\u61c9\u904b\u800c\u751f\uff0c\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eRedis\u96d9\u5beb\u6a5f\u5236\u7684\u539f\u7406\u53ca\u5176\u5728\u4fdd\u969c\u5feb\u53d6\u6578\u64da\u4e00\u81f4\u6027\u65b9\u9762\u7684\u61c9\u7528\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%e9%9b%99%e5%af%ab%e6%a9%9f%e5%88%b6%ef%bc%9f-psTLIimdpb\">\u4ec0\u9ebc\u662fRedis\u96d9\u5beb\u6a5f\u5236\uff1f<\/h2>\n<p>Redis\u96d9\u5beb\u6a5f\u5236\u662f\u6307\u5728\u5c0d\u6578\u64da\u9032\u884c\u5beb\u5165\u64cd\u4f5c\u6642\uff0c\u540c\u6642\u5c07\u6578\u64da\u5beb\u5165\u5230Redis\u5feb\u53d6\u548c\u5f8c\u7aef\u6578\u64da\u5eab\u4e2d\u3002\u9019\u6a23\u7684\u8a2d\u8a08\u65e8\u5728\u78ba\u4fdd\u5feb\u53d6\u4e2d\u7684\u6578\u64da\u8207\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u4fdd\u6301\u4e00\u81f4\uff0c\u5f9e\u800c\u907f\u514d\u56e0\u5feb\u53d6\u6578\u64da\u904e\u671f\u6216\u4e0d\u4e00\u81f4\u800c\u5c0e\u81f4\u7684\u554f\u984c\u3002<\/p>\n<h2 id=\"%e9%9b%99%e5%af%ab%e6%a9%9f%e5%88%b6%e7%9a%84%e5%b7%a5%e4%bd%9c%e5%8e%9f%e7%90%86-psTLIimdpb\">\u96d9\u5beb\u6a5f\u5236\u7684\u5de5\u4f5c\u539f\u7406<\/h2>\n<p>Redis\u96d9\u5beb\u6a5f\u5236\u7684\u57fa\u672c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u7576\u61c9\u7528\u7a0b\u5f0f\u9700\u8981\u5beb\u5165\u6578\u64da\u6642\uff0c\u9996\u5148\u5c07\u6578\u64da\u5beb\u5165Redis\u5feb\u53d6\u3002<\/li>\n<li>\u96a8\u5f8c\uff0c\u61c9\u7528\u7a0b\u5f0f\u5c07\u76f8\u540c\u7684\u6578\u64da\u5beb\u5165\u5f8c\u7aef\u6578\u64da\u5eab\u3002<\/li>\n<li>\u5728\u5beb\u5165\u6210\u529f\u5f8c\uff0c\u61c9\u7528\u7a0b\u5f0f\u53ef\u4ee5\u9078\u64c7\u6027\u5730\u66f4\u65b0\u5feb\u53d6\u4e2d\u7684\u6578\u64da\uff0c\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/li>\n<\/ol>\n<p>\u9019\u4e00\u904e\u7a0b\u4e2d\uff0c\u61c9\u7528\u7a0b\u5f0f\u9700\u8981\u8655\u7406\u5beb\u5165\u5931\u6557\u7684\u60c5\u6cc1\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6578\u64da\u6210\u529f\u5beb\u5165Redis\u4f46\u5beb\u5165\u6578\u64da\u5eab\u5931\u6557\uff0c\u5247\u53ef\u80fd\u5c0e\u81f4\u5feb\u53d6\u4e2d\u7684\u6578\u64da\u8207\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u4e0d\u4e00\u81f4\u3002\u70ba\u4e86\u89e3\u6c7a\u9019\u4e00\u554f\u984c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u4e8b\u52d9\u6216\u88dc\u511f\u6a5f\u5236\u4f86\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<h2 id=\"%e9%9b%99%e5%af%ab%e6%a9%9f%e5%88%b6%e7%9a%84%e5%84%aa%e5%8b%a2-psTLIimdpb\">\u96d9\u5beb\u6a5f\u5236\u7684\u512a\u52e2<\/h2>\n<ul>\n<li><strong>\u63d0\u9ad8\u6027\u80fd\uff1a<\/strong>\u901a\u904e\u5c07\u6578\u64da\u5beb\u5165\u5feb\u53d6\uff0c\u61c9\u7528\u7a0b\u5f0f\u53ef\u4ee5\u5feb\u901f\u8b80\u53d6\u6578\u64da\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002<\/li>\n<li><strong>\u6e1b\u5c11\u6578\u64da\u5eab\u58d3\u529b\uff1a<\/strong>\u5feb\u53d6\u53ef\u4ee5\u6e1b\u5c11\u5c0d\u5f8c\u7aef\u6578\u64da\u5eab\u7684\u76f4\u63a5\u8acb\u6c42\uff0c\u964d\u4f4e\u6578\u64da\u5eab\u7684\u8ca0\u8f09\u3002<\/li>\n<li><strong>\u4fdd\u969c\u6578\u64da\u4e00\u81f4\u6027\uff1a<\/strong>\u96d9\u5beb\u6a5f\u5236\u78ba\u4fdd\u4e86\u5feb\u53d6\u548c\u6578\u64da\u5eab\u4e4b\u9593\u7684\u6578\u64da\u4e00\u81f4\u6027\uff0c\u6e1b\u5c11\u4e86\u56e0\u6578\u64da\u4e0d\u4e00\u81f4\u800c\u5c0e\u81f4\u7684\u932f\u8aa4\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%af%a6%e7%8f%beredis%e9%9b%99%e5%af%ab%e6%a9%9f%e5%88%b6%e7%9a%84%e7%a4%ba%e4%be%8b-psTLIimdpb\">\u5be6\u73feRedis\u96d9\u5beb\u6a5f\u5236\u7684\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728Node.js\u4e2d\u5be6\u73feRedis\u96d9\u5beb\u6a5f\u5236\uff1a<\/p>\n<pre><code>\nconst redis = require('redis');\nconst mysql = require('mysql');\n\nconst redisClient = redis.createClient();\nconst dbConnection = mysql.createConnection({\n    host: 'localhost',\n    user: 'root',\n    password: 'password',\n    database: 'test'\n});\n\nfunction writeData(key, value) {\n    \/\/ \u5beb\u5165Redis\u5feb\u53d6\n    redisClient.set(key, value, (err, reply) =&gt; {\n        if (err) {\n            console.error('Redis\u5beb\u5165\u5931\u6557:', err);\n            return;\n        }\n        console.log('Redis\u5beb\u5165\u6210\u529f:', reply);\n\n        \/\/ \u5beb\u5165\u6578\u64da\u5eab\n        const query = 'INSERT INTO data (key, value) VALUES (?, ?)';\n        dbConnection.query(query, [key, value], (err, result) =&gt; {\n            if (err) {\n                console.error('\u6578\u64da\u5eab\u5beb\u5165\u5931\u6557:', err);\n                \/\/ \u53ef\u4ee5\u8003\u616e\u5728\u9019\u88e1\u9032\u884c\u88dc\u511f\u64cd\u4f5c\n                return;\n            }\n            console.log('\u6578\u64da\u5eab\u5beb\u5165\u6210\u529f:', result);\n        });\n    });\n}\n\n\/\/ \u4f7f\u7528\u793a\u4f8b\nwriteData('exampleKey', 'exampleValue');\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-psTLIimdpb\">\u7d50\u8ad6<\/h2>\n<p>Redis\u96d9\u5beb\u6a5f\u5236\u662f\u4e00\u7a2e\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\uff0c\u80fd\u5920\u5728\u5feb\u53d6\u548c\u6578\u64da\u5eab\u4e4b\u9593\u4fdd\u6301\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u8a08\u548c\u5be6\u73fe\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5145\u5206\u5229\u7528Redis\u7684\u6027\u80fd\u512a\u52e2\uff0c\u540c\u6642\u4fdd\u969c\u6578\u64da\u7684\u6e96\u78ba\u6027\u548c\u53ef\u9760\u6027\u3002\u5728\u7576\u524d\u7684\u9ad8\u4e26\u767c\u74b0\u5883\u4e2d\uff0c\u9019\u4e00\u6a5f\u5236\u986f\u5f97\u5c24\u70ba\u91cd\u8981\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bcVPS\u3001<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u3001\u4f3a\u670d\u5668\u6216\u5176\u4ed6\u96f2\u670d\u52d9\u6709\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Redis\u96d9\u5beb\u6a5f\u5236\uff0c\u78ba\u4fdd\u5feb\u53d6\u6578\u64da\u4e00\u81f4\u6027\uff0c\u63d0\u5347\u7cfb\u7d71\u6027\u80fd\u8207\u53ef\u9760\u6027\uff0c\u89e3\u6c7a\u6578\u64da\u4e0d\u4e00\u81f4\u554f\u984c\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-151863","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/151863","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=151863"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/151863\/revisions"}],"predecessor-version":[{"id":151864,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/151863\/revisions\/151864"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=151863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=151863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=151863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}