💎一站式轻松地调用各大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 ```