{"id":125125,"date":"2024-10-25T20:01:20","date_gmt":"2024-10-25T12:01:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/125125\/"},"modified":"2024-10-25T20:01:21","modified_gmt":"2024-10-25T12:01:21","slug":"redis-%e7%b5%90%e5%90%88-c-%e9%96%8b%e7%99%bc%e7%b5%a6%e4%bd%a0%e6%a5%b5%e8%87%b4%e6%95%b8%e6%93%9a%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/125125\/","title":{"rendered":"Redis \u7d50\u5408 C \u958b\u767c\u7d66\u4f60\u6975\u81f4\u6578\u64da\u89e3\u6c7a\u65b9\u6848"},"content":{"rendered":"<h1 id=\"redis-%e7%b5%90%e5%90%88-c-%e9%96%8b%e7%99%bc%e7%b5%a6%e4%bd%a0%e6%a5%b5%e8%87%b4%e6%95%b8%e6%93%9a%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%a1%88-tXsEkTtImE\">Redis \u7d50\u5408 C \u958b\u767c\u7d66\u4f60\u6975\u81f4\u6578\u64da\u89e3\u6c7a\u65b9\u6848<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u5feb\u901f\u4e14\u9ad8\u6548\u7684\u6578\u64da\u5b58\u53d6\u6210\u70ba\u4e86\u5404\u7a2e\u61c9\u7528\u7a0b\u5e8f\u6210\u529f\u7684\u95dc\u9375\u3002Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6027\u80fd\u7684\u9375\u503c\u6578\u64da\u5eab\uff0c\u56e0\u5176\u5353\u8d8a\u7684\u6027\u80fd\u548c\u9748\u6d3b\u6027\u800c\u53d7\u5230\u5ee3\u6cdb\u95dc\u6ce8\u3002\u7576 Redis \u8207 C \u8a9e\u8a00\u7d50\u5408\u4f7f\u7528\u6642\uff0c\u958b\u767c\u8005\u80fd\u5920\u5275\u5efa\u51fa\u6975\u5177\u6548\u7387\u7684\u6578\u64da\u89e3\u6c7a\u65b9\u6848\uff0c\u6eff\u8db3\u5404\u7a2e\u9700\u6c42\u3002<\/p>\n<h2 id=\"redis-%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-tXsEkTtImE\">Redis \u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>Redis\uff08Remote Dictionary Server\uff09\u662f\u4e00\u500b\u958b\u6e90\u7684\u5167\u5b58\u6578\u64da\u7d50\u69cb\u5b58\u5132\u7cfb\u7d71\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u548c\u6709\u5e8f\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\u4ee5\u6bcf\u79d2\u6578\u5341\u842c\u6b21\u7684\u901f\u5ea6\u8655\u7406\u8b80\u5beb\u8acb\u6c42\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\uff1a<\/strong>\u652f\u6301\u5c07\u6578\u64da\u6301\u4e45\u5316\u5230\u78c1\u789f\uff0c\u78ba\u4fdd\u6578\u64da\u4e0d\u6703\u56e0\u70ba\u7cfb\u7d71\u5d29\u6f70\u800c\u4e1f\u5931\u3002<\/li>\n<li><strong>\u5206\u4f48\u5f0f\uff1a<\/strong>\u652f\u6301\u4e3b\u5f9e\u8907\u88fd\u548c\u5206\u7247\uff0c\u80fd\u5920\u8f15\u9b06\u64f4\u5c55\u3002<\/li>\n<\/ul>\n<h2 id=\"c-%e8%aa%9e%e8%a8%80%e7%9a%84%e5%84%aa%e5%8b%a2-tXsEkTtImE\">C \u8a9e\u8a00\u7684\u512a\u52e2<\/h2>\n<p>C \u8a9e\u8a00\u662f\u4e00\u7a2e\u9ad8\u6548\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u7cfb\u7d71\u7de8\u7a0b\u548c\u5d4c\u5165\u5f0f\u7cfb\u7d71\u958b\u767c\u3002\u5176\u512a\u52e2\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u9ad8\u6548\u6027\uff1a<\/strong>C \u8a9e\u8a00\u7de8\u5beb\u7684\u7a0b\u5e8f\u901a\u5e38\u57f7\u884c\u901f\u5ea6\u5feb\uff0c\u4f54\u7528\u8cc7\u6e90\u5c11\u3002<\/li>\n<li><strong>\u9748\u6d3b\u6027\uff1a<\/strong>\u958b\u767c\u8005\u53ef\u4ee5\u76f4\u63a5\u64cd\u4f5c\u5167\u5b58\uff0c\u9032\u884c\u5e95\u5c64\u7de8\u7a0b\u3002<\/li>\n<li><strong>\u5ee3\u6cdb\u7684\u5eab\u652f\u6301\uff1a<\/strong>\u6709\u5927\u91cf\u7684\u5eab\u53ef\u4f9b\u4f7f\u7528\uff0c\u80fd\u5920\u8f15\u9b06\u96c6\u6210\u5404\u7a2e\u529f\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"redis-%e8%88%87-c-%e8%aa%9e%e8%a8%80%e7%9a%84%e7%b5%90%e5%90%88-tXsEkTtImE\">Redis \u8207 C \u8a9e\u8a00\u7684\u7d50\u5408<\/h2>\n<p>\u5c07 Redis \u8207 C \u8a9e\u8a00\u7d50\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5145\u5206\u767c\u63ee\u5169\u8005\u7684\u512a\u52e2\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u61c9\u7528\u5834\u666f\uff1a<\/p>\n<h3 id=\"1-%e9%ab%98%e6%80%a7%e8%83%bd%e7%b7%a9%e5%ad%98%e7%b3%bb%e7%b5%b1-tXsEkTtImE\">1. \u9ad8\u6027\u80fd\u7de9\u5b58\u7cfb\u7d71<\/h3>\n<p>\u4f7f\u7528 Redis \u4f5c\u70ba\u7de9\u5b58\u7cfb\u7d71\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u97ff\u61c9\u901f\u5ea6\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 C \u8a9e\u8a00\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u9023\u63a5 Redis \u4e26\u57f7\u884c\u57fa\u672c\u64cd\u4f5c\uff1a<\/p>\n<pre><code>\n#include &lt;stdio.h&gt;\n#include &lt;hiredis\/hiredis.h&gt;\n\nint main() {\n    redisContext *c = redisConnect(\"127.0.0.1\", 6379);\n    if (c == NULL || c-&gt;err) {\n        if (c) {\n            printf(\"Error: %sn\", c-&gt;errstr);\n            redisFree(c);\n        } else {\n            printf(\"Can't allocate redis contextn\");\n        }\n        return 1;\n    }\n\n    redisCommand(c, \"SET foo bar\");\n    redisReply *reply = redisCommand(c, \"GET foo\");\n    printf(\"GET foo: %sn\", reply-&gt;str);\n    freeReplyObject(reply);\n    redisFree(c);\n    return 0;\n}\n<\/code><\/pre>\n<h3 id=\"2-%e5%af%a6%e6%99%82%e6%95%b8%e6%93%9a%e8%99%95%e7%90%86-tXsEkTtImE\">2. \u5be6\u6642\u6578\u64da\u8655\u7406<\/h3>\n<p>\u5728\u9700\u8981\u5be6\u6642\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\u4e2d\uff0cRedis \u7684\u9ad8\u6027\u80fd\u7279\u6027\u4f7f\u5176\u6210\u70ba\u7406\u60f3\u7684\u9078\u64c7\u3002\u958b\u767c\u8005\u53ef\u4ee5\u5229\u7528 Redis \u7684\u767c\u5e03\/\u8a02\u95b1\u529f\u80fd\uff0c\u5be6\u73fe\u5373\u6642\u6d88\u606f\u63a8\u9001\u3002<\/p>\n<h3 id=\"3-%e5%88%86%e4%bd%88%e5%bc%8f%e7%b3%bb%e7%b5%b1-tXsEkTtImE\">3. \u5206\u4f48\u5f0f\u7cfb\u7d71<\/h3>\n<p>\u5728\u5206\u4f48\u5f0f\u7cfb\u7d71\u4e2d\uff0cRedis \u53ef\u4ee5\u4f5c\u70ba\u5171\u4eab\u6578\u64da\u5b58\u5132\uff0c\u652f\u6301\u591a\u500b\u670d\u52d9\u5668\u4e4b\u9593\u7684\u6578\u64da\u540c\u6b65\u3002\u9019\u5c0d\u65bc\u9700\u8981\u9ad8\u53ef\u7528\u6027\u548c\u53ef\u64f4\u5c55\u6027\u7684\u61c9\u7528\u5c24\u70ba\u91cd\u8981\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-tXsEkTtImE\">\u7d50\u8ad6<\/h2>\n<p>Redis \u8207 C \u8a9e\u8a00\u7684\u7d50\u5408\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684\u6578\u64da\u89e3\u6c7a\u65b9\u6848\uff0c\u7121\u8ad6\u662f\u5728\u6027\u80fd\u3001\u9748\u6d3b\u6027\u9084\u662f\u64f4\u5c55\u6027\u65b9\u9762\u90fd\u8868\u73fe\u51fa\u8272\u3002\u96a8\u8457\u6578\u64da\u9700\u6c42\u7684\u589e\u9577\uff0c\u9019\u7a2e\u7d44\u5408\u5c07\u5728\u672a\u4f86\u7684\u958b\u767c\u4e2d\u626e\u6f14\u8d8a\u4f86\u8d8a\u91cd\u8981\u7684\u89d2\u8272\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u4f60\u7684 Redis \u548c C \u958b\u767c\uff0cServer.HK \u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u5e6b\u52a9\u4f60\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u7ba1\u7406\u7684\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis \u7d50\u5408 C \u958b\u767c\uff0c\u63d0\u4f9b\u9ad8\u6548\u80fd\u6578\u64da\u89e3\u6c7a\u65b9\u6848\uff0c\u52a9\u4f60\u8f15\u9b06\u8655\u7406\u5927\u898f\u6a21\u6578\u64da\uff0c\u63d0\u5347\u61c9\u7528\u6027\u80fd\u8207\u97ff\u61c9\u901f\u5ea6\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-125125","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/125125","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=125125"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/125125\/revisions"}],"predecessor-version":[{"id":125126,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/125125\/revisions\/125126"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=125125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=125125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=125125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}