{"id":183656,"date":"2024-11-10T06:38:19","date_gmt":"2024-11-09T22:38:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/183656\/"},"modified":"2024-11-10T06:38:20","modified_gmt":"2024-11-09T22:38:20","slug":"%e4%bd%bf%e7%94%a8c%e8%aa%9e%e8%a8%80%e9%80%a3%e6%8e%a5redis%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e7%b0%a1%e4%be%bf%e6%98%93%e8%a1%8c%e7%9a%84%e6%95%99%e7%a8%8b-c-%e5%a6%82%e4%bd%95%e9%80%a3%e6%8e%a5r","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/183656\/","title":{"rendered":"\u4f7f\u7528C\u8a9e\u8a00\u9023\u63a5Redis\u6578\u64da\u5eab\uff0c\u7c21\u4fbf\u6613\u884c\u7684\u6559\u7a0b (c \u5982\u4f55\u9023\u63a5redis\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8c%e8%aa%9e%e8%a8%80%e9%80%a3%e6%8e%a5redis%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%8c%e7%b0%a1%e4%be%bf%e6%98%93%e8%a1%8c%e7%9a%84%e6%95%99%e7%a8%8b-epwHUcpxug\">\u4f7f\u7528C\u8a9e\u8a00\u9023\u63a5Redis\u6578\u64da\u5eab\uff0c\u7c21\u4fbf\u6613\u884c\u7684\u6559\u7a0b<\/h1>\n<p>Redis\u662f\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u6578\u64da\u6301\u4e45\u5316\u7b49\u5834\u666f\u3002\u7531\u65bc\u5176\u9ad8\u6027\u80fd\u548c\u9748\u6d3b\u6027\uff0c\u8a31\u591a\u958b\u767c\u8005\u9078\u64c7\u4f7f\u7528Redis\u4f86\u63d0\u5347\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528C\u8a9e\u8a00\u9023\u63a5Redis\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u7c21\u55ae\u6613\u884c\u7684\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-epwHUcpxug\">\u74b0\u5883\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u60a8\u7684\u958b\u767c\u74b0\u5883\u4e2d\u5df2\u7d93\u5b89\u88dd\u4e86\u4ee5\u4e0b\u5de5\u5177\uff1a<\/p>\n<ul>\n<li>Redis\u4f3a\u670d\u5668\uff1a\u53ef\u4ee5\u5728\u672c\u5730\u6216\u9060\u7aef\u4f3a\u670d\u5668\u4e0a\u5b89\u88dd\u3002<\/li>\n<li>C\u7de8\u8b6f\u5668\uff1a\u5982GCC\uff0c\u78ba\u4fdd\u53ef\u4ee5\u7de8\u8b6fC\u8a9e\u8a00\u4ee3\u78bc\u3002<\/li>\n<li>hiredis\u5eab\uff1a\u9019\u662f\u4e00\u500bC\u8a9e\u8a00\u7684Redis\u5ba2\u6236\u7aef\u5eab\uff0c\u63d0\u4f9b\u4e86\u9023\u63a5\u548c\u64cd\u4f5cRedis\u7684\u529f\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%ae%89%e8%a3%9dhiredis%e5%ba%ab-epwHUcpxug\">\u5b89\u88ddhiredis\u5eab<\/h2>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u4e0b\u8f09\u4e26\u5b89\u88ddhiredis\u5eab\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u4f86\u5b89\u88dd\uff1a<\/p>\n<pre><code>git clone https:\/\/github.com\/redis\/hiredis.git\ncd hiredis\nmake\nsudo make install<\/code><\/pre>\n<p>\u9019\u5c07\u6703\u5c07hiredis\u5eab\u5b89\u88dd\u5230\u60a8\u7684\u7cfb\u7d71\u4e2d\uff0c\u4e26\u4f7f\u5176\u53ef\u4ee5\u5728C\u8a9e\u8a00\u9805\u76ee\u4e2d\u4f7f\u7528\u3002<\/p>\n<h2 id=\"%e9%80%a3%e6%8e%a5redis%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%ad%a5%e9%a9%9f-epwHUcpxug\">\u9023\u63a5Redis\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f<\/h2>\n<p>\u4f7f\u7528C\u8a9e\u8a00\u9023\u63a5Redis\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5305\u542bhiredis\u7684\u982d\u6587\u4ef6\u3002<\/li>\n<li>\u5275\u5efaRedis\u4e0a\u4e0b\u6587\u3002<\/li>\n<li>\u57f7\u884cRedis\u547d\u4ee4\u3002<\/li>\n<li>\u91cb\u653e\u8cc7\u6e90\u3002<\/li>\n<\/ol>\n<h2 id=\"%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a2%bc-epwHUcpxug\">\u793a\u4f8b\u4ee3\u78bc<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528C\u8a9e\u8a00\u9023\u63a5Redis\u4e26\u57f7\u884c\u57fa\u672c\u7684SET\u548cGET\u64cd\u4f5c\uff1a<\/p>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;hiredis\/hiredis.h&gt;\n\nint main() {\n    \/\/ \u5275\u5efaRedis\u4e0a\u4e0b\u6587\n    redisContext *context = redisConnect(\"127.0.0.1\", 6379);\n    if (context == NULL || context-&gt;err) {\n        if (context) {\n            printf(\"Error: %sn\", context-&gt;errstr);\n            redisFree(context);\n        } else {\n            printf(\"Can't allocate redis contextn\");\n        }\n        return 1;\n    }\n\n    \/\/ \u57f7\u884cSET\u547d\u4ee4\n    redisCommand(context, \"SET key1 value1\");\n\n    \/\/ \u57f7\u884cGET\u547d\u4ee4\n    redisReply *reply = redisCommand(context, \"GET key1\");\n    if (reply-&gt;type == REDIS_REPLY_STRING) {\n        printf(\"GET key1: %sn\", reply-&gt;str);\n    } else {\n        printf(\"GET key1: (nil)n\");\n    }\n\n    \/\/ \u91cb\u653e\u8cc7\u6e90\n    freeReplyObject(reply);\n    redisFree(context);\n    return 0;\n}<\/code><\/pre>\n<h2 id=\"%e4%bb%a3%e7%a2%bc%e8%a7%a3%e6%9e%90-epwHUcpxug\">\u4ee3\u78bc\u89e3\u6790<\/h2>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5275\u5efa\u4e86\u4e00\u500bRedis\u4e0a\u4e0b\u6587\uff0c\u4e26\u6aa2\u67e5\u662f\u5426\u6210\u529f\u9023\u63a5\u5230Redis\u4f3a\u670d\u5668\u3002\u63a5\u8457\uff0c\u6211\u5011\u4f7f\u7528redisCommand\u51fd\u6578\u57f7\u884cSET\u548cGET\u547d\u4ee4\uff0c\u4e26\u6253\u5370\u51fa\u7d50\u679c\u3002\u6700\u5f8c\uff0c\u6211\u5011\u91cb\u653e\u4e86reply\u5c0d\u8c61\u548cRedis\u4e0a\u4e0b\u6587\uff0c\u4ee5\u907f\u514d\u5167\u5b58\u6cc4\u6f0f\u3002<\/p>\n<h2 id=\"%e5%b8%b8%e8%a6%8b%e5%95%8f%e9%a1%8c-epwHUcpxug\">\u5e38\u898b\u554f\u984c<\/h2>\n<h3 id=\"1-%e5%a6%82%e4%bd%95%e8%99%95%e7%90%86%e9%80%a3%e6%8e%a5%e9%8c%af%e8%aa%a4%ef%bc%9f-epwHUcpxug\">1. \u5982\u4f55\u8655\u7406\u9023\u63a5\u932f\u8aa4\uff1f<\/h3>\n<p>\u5728\u5275\u5efaRedis\u4e0a\u4e0b\u6587\u6642\uff0c\u61c9\u8a72\u6aa2\u67e5context\u662f\u5426\u70baNULL\uff0c\u4e26\u6aa2\u67e5context-&gt;err\u4ee5\u7372\u53d6\u932f\u8aa4\u4fe1\u606f\u3002<\/p>\n<h3 id=\"2-hiredis%e5%ba%ab%e6%98%af%e5%90%a6%e6%94%af%e6%8c%81%e7%95%b0%e6%ad%a5%e6%93%8d%e4%bd%9c%ef%bc%9f-epwHUcpxug\">2. hiredis\u5eab\u662f\u5426\u652f\u6301\u7570\u6b65\u64cd\u4f5c\uff1f<\/h3>\n<p>\u662f\u7684\uff0chiredis\u5eab\u652f\u6301\u7570\u6b65\u64cd\u4f5c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528hiredis\u7684\u4e8b\u4ef6\u5faa\u74b0\u4f86\u5be6\u73fe\u975e\u963b\u585e\u7684Redis\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-epwHUcpxug\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528C\u8a9e\u8a00\u9023\u63a5Redis\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u4e86\u7c21\u55ae\u7684\u793a\u4f8b\u4ee3\u78bc\u3002\u901a\u904e\u4f7f\u7528hiredis\u5eab\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5728C\u8a9e\u8a00\u61c9\u7528\u4e2d\u96c6\u6210Redis\u6578\u64da\u5eab\uff0c\u5f9e\u800c\u63d0\u5347\u61c9\u7528\u7684\u6027\u80fd\u548c\u6548\u7387\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u4f3a\u670d\u5668\u4f86\u904b\u884c\u60a8\u7684\u61c9\u7528\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u670d\u52d9\uff0c\u4ee5\u7372\u5f97\u66f4\u597d\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528C\u8a9e\u8a00\u8f15\u9b06\u9023\u63a5Redis\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u7c21\u4fbf\u6613\u884c\u7684\u6b65\u9a5f\u548c\u7bc4\u4f8b\uff0c\u52a9\u4f60\u5feb\u901f\u4e0a\u624b\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-183656","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183656","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=183656"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183656\/revisions"}],"predecessor-version":[{"id":183657,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183656\/revisions\/183657"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=183656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=183656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=183656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}