{"id":125949,"date":"2024-10-26T00:38:57","date_gmt":"2024-10-25T16:38:57","guid":{"rendered":"https:\/\/server.hk\/cnblog\/125949\/"},"modified":"2024-10-26T00:38:57","modified_gmt":"2024-10-25T16:38:57","slug":"redis-%e9%80%a3%e6%8e%a5%e6%b1%a0-c-%e5%af%a6%e7%8f%be%e6%89%93%e9%96%8b%e6%95%b8%e6%93%9a%e8%a8%aa%e5%95%8f%e4%b9%8b%e9%96%80","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/125949\/","title":{"rendered":"Redis \u9023\u63a5\u6c60 C \u5be6\u73fe\u6253\u958b\u6578\u64da\u8a2a\u554f\u4e4b\u9580"},"content":{"rendered":"<h1 id=\"redis-%e9%80%a3%e6%8e%a5%e6%b1%a0-c-%e5%af%a6%e7%8f%be%e6%89%93%e9%96%8b%e6%95%b8%e6%93%9a%e8%a8%aa%e5%95%8f%e4%b9%8b%e9%96%80-BwSOLEcPXQ\">Redis \u9023\u63a5\u6c60 C \u5be6\u73fe\u6253\u958b\u6578\u64da\u8a2a\u554f\u4e4b\u9580<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u61c9\u7528\u7a0b\u5e8f\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5b58\u53d6\u7684\u6548\u7387\u548c\u6027\u80fd\u81f3\u95dc\u91cd\u8981\u3002Redis \u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u9375\u503c\u5b58\u5132\u7cfb\u7d71\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u5834\u666f\u4e2d\uff0c\u5982\u7de9\u5b58\u3001\u6d88\u606f\u968a\u5217\u548c\u6578\u64da\u6301\u4e45\u5316\u7b49\u3002\u70ba\u4e86\u63d0\u9ad8 Redis \u7684\u6027\u80fd\uff0c\u9023\u63a5\u6c60\u6280\u8853\u61c9\u904b\u800c\u751f\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728 C \u8a9e\u8a00\u4e2d\u5be6\u73fe Redis \u9023\u63a5\u6c60\uff0c\u4e26\u5206\u6790\u5176\u5c0d\u6578\u64da\u8a2a\u554f\u7684\u5f71\u97ff\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-redis-%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-BwSOLEcPXQ\">\u4ec0\u9ebc\u662f Redis \u9023\u63a5\u6c60\uff1f<\/h2>\n<p>Redis \u9023\u63a5\u6c60\u662f\u4e00\u7a2e\u7ba1\u7406 Redis \u9023\u63a5\u7684\u6280\u8853\uff0c\u901a\u904e\u91cd\u7528\u5df2\u5efa\u7acb\u7684\u9023\u63a5\u4f86\u6e1b\u5c11\u9023\u63a5\u7684\u958b\u92b7\u3002\u6bcf\u6b21\u61c9\u7528\u7a0b\u5e8f\u9700\u8981\u8a2a\u554f Redis \u6642\uff0c\u7121\u9700\u91cd\u65b0\u5efa\u7acb\u9023\u63a5\uff0c\u9019\u6a23\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u9ad8\u4f75\u767c\u7684\u74b0\u5883\u4e2d\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%bd%bf%e7%94%a8%e9%80%a3%e6%8e%a5%e6%b1%a0%ef%bc%9f-BwSOLEcPXQ\">\u70ba\u4ec0\u9ebc\u4f7f\u7528\u9023\u63a5\u6c60\uff1f<\/h2>\n<ul>\n<li><strong>\u6027\u80fd\u63d0\u5347\uff1a<\/strong>\u91cd\u7528\u9023\u63a5\u53ef\u4ee5\u6e1b\u5c11\u5efa\u7acb\u548c\u95dc\u9589\u9023\u63a5\u7684\u6642\u9593\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u6027\u80fd\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u7ba1\u7406\uff1a<\/strong>\u9023\u63a5\u6c60\u53ef\u4ee5\u9650\u5236\u540c\u6642\u6253\u958b\u7684\u9023\u63a5\u6578\u91cf\uff0c\u907f\u514d\u8cc7\u6e90\u6d6a\u8cbb\u3002<\/li>\n<li><strong>\u7c21\u5316\u4ee3\u78bc\uff1a<\/strong>\u4f7f\u7528\u9023\u63a5\u6c60\u53ef\u4ee5\u7c21\u5316\u4ee3\u78bc\u7d50\u69cb\uff0c\u6e1b\u5c11\u932f\u8aa4\u7684\u53ef\u80fd\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%9c%a8-c-%e8%aa%9e%e8%a8%80%e4%b8%ad%e5%af%a6%e7%8f%be-redis-%e9%80%a3%e6%8e%a5%e6%b1%a0-BwSOLEcPXQ\">\u5728 C \u8a9e\u8a00\u4e2d\u5be6\u73fe Redis \u9023\u63a5\u6c60<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 Redis \u9023\u63a5\u6c60\u5be6\u73fe\u793a\u4f8b\u3002\u9019\u500b\u793a\u4f8b\u4f7f\u7528\u4e86 <code>hiredis<\/code> \u5eab\u4f86\u8207 Redis \u9032\u884c\u4ea4\u4e92\u3002<\/p>\n<h3 id=\"%e6%ad%a5%e9%a9%9f-1-%e5%ae%89%e8%a3%9d-hiredis-BwSOLEcPXQ\">\u6b65\u9a5f 1: \u5b89\u88dd hiredis<\/h3>\n<p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88dd <code>hiredis<\/code> \u5eab\u3002\u53ef\u4ee5\u901a\u904e\u4ee5\u4e0b\u547d\u4ee4\u9032\u884c\u5b89\u88dd\uff1a<\/p>\n<pre><code>git clone https:\/\/github.com\/redis\/hiredis.git\ncd hiredis\nmake\nsudo make install<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f-2-%e5%ae%9a%e7%be%a9%e9%80%a3%e6%8e%a5%e6%b1%a0%e7%b5%90%e6%a7%8b-BwSOLEcPXQ\">\u6b65\u9a5f 2: \u5b9a\u7fa9\u9023\u63a5\u6c60\u7d50\u69cb<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5b9a\u7fa9\u4e00\u500b\u9023\u63a5\u6c60\u7684\u7d50\u69cb\uff1a<\/p>\n<pre><code>#include &lt;hiredis\/hiredis.h&gt;\n#include &lt;pthread.h&gt;\n\n#define POOL_SIZE 10\n\ntypedef struct {\n    redisContext *connections[POOL_SIZE];\n    int current;\n    pthread_mutex_t lock;\n} RedisPool;<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f-3-%e5%88%9d%e5%a7%8b%e5%8c%96%e9%80%a3%e6%8e%a5%e6%b1%a0-BwSOLEcPXQ\">\u6b65\u9a5f 3: \u521d\u59cb\u5316\u9023\u63a5\u6c60<\/h3>\n<p>\u7136\u5f8c\uff0c\u6211\u5011\u9700\u8981\u4e00\u500b\u51fd\u6578\u4f86\u521d\u59cb\u5316\u9023\u63a5\u6c60\uff1a<\/p>\n<pre><code>RedisPool* init_pool(const char *hostname, int port) {\n    RedisPool *pool = malloc(sizeof(RedisPool));\n    pool-&gt;current = 0;\n    pthread_mutex_init(&amp;pool-&gt;lock, NULL);\n\n    for (int i = 0; i &lt; POOL_SIZE; i++) {\n        pool-&gt;connections[i] = redisConnect(hostname, port);\n        if (pool-&gt;connections[i] == NULL || pool-&gt;connections[i]-&gt;err) {\n            \/\/ \u932f\u8aa4\u8655\u7406\n        }\n    }\n    return pool;\n}<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f-4-%e7%8d%b2%e5%8f%96%e9%80%a3%e6%8e%a5-BwSOLEcPXQ\">\u6b65\u9a5f 4: \u7372\u53d6\u9023\u63a5<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u4e00\u500b\u51fd\u6578\u4f86\u7372\u53d6\u53ef\u7528\u7684\u9023\u63a5\uff1a<\/p>\n<pre><code>redisContext* get_connection(RedisPool *pool) {\n    pthread_mutex_lock(&amp;pool-&gt;lock);\n    redisContext *conn = pool-&gt;connections[pool-&gt;current];\n    pool-&gt;current = (pool-&gt;current + 1) % POOL_SIZE;\n    pthread_mutex_unlock(&amp;pool-&gt;lock);\n    return conn;\n}<\/code><\/pre>\n<h3 id=\"%e6%ad%a5%e9%a9%9f-5-%e9%87%8b%e6%94%be%e9%80%a3%e6%8e%a5%e6%b1%a0-BwSOLEcPXQ\">\u6b65\u9a5f 5: \u91cb\u653e\u9023\u63a5\u6c60<\/h3>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u4e00\u500b\u51fd\u6578\u4f86\u91cb\u653e\u9023\u63a5\u6c60\uff1a<\/p>\n<pre><code>void free_pool(RedisPool *pool) {\n    for (int i = 0; i &lt; POOL_SIZE; i++) {\n        redisFree(pool-&gt;connections[i]);\n    }\n    pthread_mutex_destroy(&amp;pool-&gt;lock);\n    free(pool);\n}<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-BwSOLEcPXQ\">\u7d50\u8ad6<\/h2>\n<p>\u901a\u904e\u5be6\u73fe Redis \u9023\u63a5\u6c60\uff0c\u6211\u5011\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6578\u64da\u8a2a\u554f\u7684\u6548\u7387\uff0c\u7279\u5225\u662f\u5728\u9ad8\u4f75\u767c\u7684\u61c9\u7528\u5834\u666f\u4e2d\u3002\u9019\u7a2e\u6280\u8853\u4e0d\u50c5\u80fd\u5920\u63d0\u5347\u6027\u80fd\uff0c\u9084\u80fd\u6709\u6548\u7ba1\u7406\u8cc7\u6e90\uff0c\u7c21\u5316\u4ee3\u78bc\u7d50\u69cb\u3002\u5c0d\u65bc\u5e0c\u671b\u5728 C \u8a9e\u8a00\u4e2d\u4f7f\u7528 Redis \u7684\u958b\u767c\u8005\u4f86\u8aaa\uff0c\u9023\u63a5\u6c60\u662f\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u4e86\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u9084\u662f\u5176\u4ed6\u5730\u5340\u7684\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528 C \u8a9e\u8a00\u5be6\u73fe Redis \u9023\u63a5\u6c60\uff0c\u63d0\u5347\u6578\u64da\u8a2a\u554f\u6548\u7387\uff0c\u6253\u958b\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u65b0\u5927\u9580\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-125949","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/125949","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=125949"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/125949\/revisions"}],"predecessor-version":[{"id":125950,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/125949\/revisions\/125950"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=125949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=125949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=125949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}