<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 3.8 redis-C-API * 網址: https://github.com/redis/hiredis * 下載: ```bash git clone https://github.com/redis/hiredis.git ``` * 安裝 ```bash cd hiredis make sudo make install ``` 安裝輸出 ```bash mkdir -p /usr/local/include/hiredis /usr/local/lib cp -a hiredis.h async.h read.h sds.h adapters /usr/local/include/hiredis cp -a libhiredis.so /usr/local/lib/libhiredis.so.0.13 cd /usr/local/lib && ln -sf libhiredis.so.0.13 libhiredis.so cp -a libhiredis.a /usr/local/lib mkdir -p /usr/local/lib/pkgconfig cp -a hiredis.pc /usr/local/lib/pkgconfig ``` 默認庫安裝在"/usr/local/lib/",設置加載庫路徑 ```bash vim ~/.bashrc ``` 最后一行寫入 ``` export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/ ``` 重啟終端 --- ###API ```cpp redisContext *redisConnect(const char *ip, int port); ``` 說明:該函數用來連接redis數據庫,參數為數據庫的ip地址和端口,一般redis數據庫的端口為6379 該函數返回一個結構體 redisContext。 --- ```cpp void *redisCommand(redisContext *c, const char *format, ...); ``` 說明:該函數執行命令,就如sql數據庫中的SQL語句一樣,只是執行的是redis數據庫中的操作命令,第一個參數為連接數據庫時 返回的redisContext,剩下的參數為變參,就如C標準函數printf函數一樣的變參。返回值為void*,一般強制轉換成為redisReply類 型的進行進一步的處理。 redisCommand函數返回一個東西叫redisReply,我們需要通過判斷它的type字段 來知道返回了具體什么樣的內容: | 狀態標識| 含義 | | -- | -- | | REDIS_REPLY_STATUS |表示狀態,內容通過str字段查看,字符串長度是len字段 | | REDIS_REPLY_ERROR |表示出錯,查看出錯信息,如上的str,len字段 | | REDIS_REPLY_INTEGER | 返回整數,從integer字段獲取值| | REDIS_REPLY_NIL | 沒有數據返回 | | REDIS_REPLY_STRING | 返回字符串,查看str,len字段 | | REDIS_REPLY_ARRAY | 返回一個數組,查看elements的值(數組個數),通過 element[index]的方式訪問數組元素,每個數組元素是一個redisReply對象的指針。 | --- ```cpp void freeReplyObject(void *reply); ``` 說明:釋放redisCommand執行后返回的redisReply所占用的內存 redisReply返回結果處理: |標識|含義 | | -- | -- | | REDIS_OK | 正常 | | REDIS_ERR_IO| IO讀/寫出現異常,通過errno查看原因 | | REDIS_ERR_EOF | 服務器關閉了鏈接,讀結束 | | REDIS_ERR_PROTOCOL |分析redis協議內容出錯 | | EDIS_ERR_OTHER| 其他未知的錯誤 | 上述錯誤類型都可以通過redisReply的errstr字段查看簡短的描述 --- ```cpp void redisFree(redisContext *c); ``` 說明:釋放redisConnect()所產生的連接。 ###測試用例 ```cpp #include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <stdarg.h> #include <string.h> #include <assert.h> #include <hiredis/hiredis.h> void doTest() { //redis默認監聽端口為6387,可以在配置文件中修改 redisContext* c = redisConnect("127.0.0.1", 6379); if ( c->err) { redisFree(c); printf("Connect to redisServer faile\n"); return ; } printf("Connect to redisServer Success\n"); const char* command1 = "set key1 itcast_1"; redisReply* r = (redisReply*)redisCommand(c, command1); if( NULL == r) { printf("Execut command1 failure\n"); redisFree(c); return; } if( !(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK")==0)) { printf("Failed to execute command[%s]\n",command1); freeReplyObject(r); redisFree(c); return; } freeReplyObject(r); printf("Succeed to execute command[%s]\n", command1); const char* command2 = "strlen key1"; r = (redisReply*)redisCommand(c, command2); if ( r->type != REDIS_REPLY_INTEGER) { printf("Failed to execute command[%s]\n",command2); freeReplyObject(r); redisFree(c); return; } intlength= r->integer; freeReplyObject(r); printf("The length of 'key1' is %d.\n", length); printf("Succeed to execute command[%s]\n", command2); const char* command3 = "get key1"; r = (redisReply*)redisCommand(c, command3); if ( r->type != REDIS_REPLY_STRING) { printf("Failed to execute command[%s]\n",command3); freeReplyObject(r); redisFree(c); return; } printf("The value of 'key1' is %s\n", r->str); freeReplyObject(r); printf("Succeed to execute command[%s]\n", command3); const char* command4 = "get key2"; r = (redisReply*)redisCommand(c, command4); if ( r->type != REDIS_REPLY_NIL) { printf("Failed to execute command[%s]\n",command4); freeReplyObject(r); redisFree(c); return; } freeReplyObject(r); printf("Succeed to execute command[%s]\n", command4); redisFree(c); } int main(void) { doTest(); return 0; } ``` 編譯: ```bash gcc testredis.c -lhiredis -o testredis ``` 運行: ```bash ./testredis ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看