Redis接口的调用
2017-02-11 19:38
281 查看
1、hiredis是redis数据库的C接口,目录为/redis-3.2.6/deps/hiredis 2、示例代码如下: #include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <stdarg.h> #include <string.h> #include <assert.h> #include "hiredis.h" int main() { //连接redis redisContext* c = redisConnect("127.0.0.1", 6379); if ( c->err) { redisFree(c); printf("Connect to redisServer faile\n"); return -1; } printf("Connect to redisServer Success\n"); const char* setCommand = "set name andy"; redisReply* r = (redisReply*)redisCommand(c, setCommand); if( NULL == r) { printf("Execut setCommand failure\n"); redisFree(c); return -1; } if( !(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK")==0)) { printf("Failed to execute command[%s]\n",setCommand); freeReplyObject(r); redisFree(c); return -1; } freeReplyObject(r); printf("Succeed to execute command[%s]\n", setCommand); const char* getCommand = "get name"; r = (redisReply*)redisCommand(c, getCommand); if ( r->type != REDIS_REPLY_STRING) { printf("Failed to execute command[%s]\n",getCommand); freeReplyObject(r); redisFree(c); return -1; } printf("Succeed to execute command[%s]\n", getCommand); printf("The value of 'name' is %s\n", r->str); freeReplyObject(r); redisFree(c); return 0; } 3、编译,运行如下: [root@localhost hiredis]# g++ -o main main.cpp libhiredis.a [root@localhost hiredis]# ./main Connect to redisServer Success Succeed to execute command[set name andy] Succeed to execute command[get name] The value of 'name' is andy
相关文章推荐
- python 实现redis 操作API 接口,提供其他程序调用
- spring-data-redis接口调用出现乱码的问题
- Inside VCL:接口指针调用函数的时候,如何获得对象指针以完成函数调用?
- 利用动态创建自动化接口实现VB的函数指针调用
- ICTCLAS分词系统Java调用接口在Eclipse中的安装
- 使用巧妙的接口指针调用COM对象
- C#中如何进行远程调用别人的接口
- 汉王扫描仪调用接口,delphi代码
- 利用Castle IOC实现远程调用的接口统一(上)
- 如何在 SOAP 接口调用的 Http header 中增加字段
- 内核中调用用户程序的接口
- 调用Shell接口(系统运行窗口)的代码
- 如何避免不同的用户调用接口时出现多个服务程序实例
- ASP.Net程式中调用FCKeditor的接口
- 如何调用IDispatch接口的方法和属性
- 用户在SP网站上操作的WEB接口调用和流程图[修改图片]
- 推理接口的调用过程(欢迎讨论,希望这是一个大坑~~~)
- 工厂方法-接口-动态调用DLL
- Inside VCL:接口指针调用函数的时候,如何获得对象指针以完成函数调用?
- C# 接口的调用