首页 / C语言 / C语言操作Redis总结
C语言操作Redis总结
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C语言操作Redis总结,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3596字,纯文字阅读大概需要6分钟。
内容图文

1 #include "hiredis.h" 2 3#define NO_QFORKIMPL 4#pragma comment(lib,"hiredis.lib") 5#pragma comment(lib,"Win32_Interop.lib") 6 7int get_int_command(char int_command[200]) 8{ 9 reply = (redisReply *)redisCommand(c, int_command); 10//printf("exists命令执行结果: %d\n", reply->type); 11if (reply->type == 3) //返回整型标识 12 { 13//printf("%s命令执行结果: %d\n", int_command, reply->integer); 14return reply->integer; 15 } 16elseif (reply->type == 4) //返回nil对象 17 { 18return -1; 19 } 20elseif (reply->type == 6) //返回错误 21 { 22return -2; 23 } 24 freeReplyObject(reply); 25return0; 26} 27 28char* get_string_command(char string_command[200]) 29{ 30 reply = (redisReply *)redisCommand(c, string_command); 31//printf("lindex MA_h1_K 0命令执行结果 reply type: %d\n", reply->type); 32if (reply->type == 1) //返回字符串标识 33 { 34//printf("lindex MA_h1_K 0命令执行结果 reply type: %s\n", reply->str); 35return reply->str; 36 } 37elseif (reply->type == 4) //返回nil对象 38 { 39return"不存在要访问的数据"; 40 } 41elseif (reply->type == 6) //返回错误 42 { 43return reply->str; 44 } 45 freeReplyObject(reply); 46return""; 47} 48 49void run_command(char run_command[200]) 50{ 51 reply = (redisReply *)redisCommand(c, run_command); 52//printf("reply type: %d\n", reply->type); 53if (reply->type == 5) 54 { 55//printf("run_command执行结果: %s\n", reply->str); 56 } 57 freeReplyObject(reply); 58} 59 60int main() 61{ 62 SYSTEMTIME sys; 63char local_time[25] = ""; 64 65 c = redisConnect((char*)redis_host, redis_port); 66if (c->err) { /* Error flags, 0 when there is no error */ 67 printf("连接Redis失败: %s\n", c->errstr); 68 exit(1); 69 } 70else 71 { 72 printf("连接Redis成功!\n"); 73 } 74 75 reply = (redisReply *)redisCommand(c, "AUTH %s", redis_password); 76if (reply->type == REDIS_REPLY_ERROR) { 77 printf("Redis认证失败!\n"); 78 } 79else 80 { 81 printf("Redis认证成功!\n"); 82 } 83 freeReplyObject(reply); 84 85 reply = (redisReply *)redisCommand(c, "SELECT 1"); //选择数据库 86 printf("SELECT: 1 %s\n", reply->str); 87 freeReplyObject(reply); 88 89//delete命令 90 run_command("DEL foo"); 91 92//set命令 93 run_command("SET foo hello world"); 94 95 96//get命令 97 printf("GET foo命令执行结果 : %s\n", get_string_command("GET foo")); 98 99100//exists命令101 printf("exists test1命令执行结果: %d\n", get_int_command("exists test1")); 102 printf("exists MA_h1_K命令执行结果: %d\n", get_int_command("exists MA_h1_K")); 103104105//llen命令106 printf("llen MA_h1_K命令执行结果: %d\n", get_int_command("llen MA_h1_K")); 107108109//lrange命令110 reply = (redisReply *)redisCommand(c, "lrange MA_h1_K 0 7"); 111//printf("lrange MA_h1_K 0 7命令执行结果 reply type: %d\n", reply->type);112if (reply->type == 2) 113 { 114 printf("队列数量为: %d\n", reply->elements); 115if (reply->element[0]->type == 1) 116 { 117for (int i = 0; i < reply->elements; i++) 118 { 119 printf("lrange MA_h1_K 0 7命令执行结果: %s\n", reply->element[i]->str); 120 } 121 } 122123 } 124 freeReplyObject(reply); 125126//lindex命令127 printf("lindex MA_h1_K 0命令执行结果 : %s\n", get_string_command("lindex MA_h1_K 0")); 128129130//lpush命令131 run_command("lpush list test1 test2 test3"); 132133//lpop命令134 printf("lpop list命令执行结果 : %s\n", get_string_command("lpop list")); 135136//rpop命令137 printf("rpop list命令执行结果 : %s\n", get_string_command("rpop list")); 138139//rpoplpush命令140 printf("rpoplpush list list1命令执行结果 : %s\n", get_string_command("rpoplpush list list1")); 141142 printf("lpop list1命令执行结果 : %s\n", get_string_command("lpop list1")); 143144//lpush rpush lpop rpop RPOPLPUSH145146147char test; 148 test = getchar(); 149 }
原文:http://www.cnblogs.com/hushaojun/p/7291788.html
内容总结
以上是互联网集市为您收集整理的C语言操作Redis总结全部内容,希望文章能够帮你解决C语言操作Redis总结所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。