摘要:本文主要向大家介绍了C/C++知识点之C语言操作Redis总结,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
本文主要向大家介绍了C/C++知识点之C语言操作Redis总结,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
1 #include "hiredis.h"
2
3 #define NO_QFORKIMPL
4 #pragma comment(lib,"hiredis.lib")
5 #pragma comment(lib,"Win32_Interop.lib")
6
7 int get_int_command(char int_command[200])
8 {
9 reply = (redisReply *)redisCommand(c, int_command);
10 //printf("exists命令执行结果: %d\n", reply->type);
11 if (reply->type == 3) //返回整型标识
12 {
13 //printf("%s命令执行结果: %d\n", int_command, reply->integer);
14 return reply->integer;
15 }
16 else if (reply->type == 4) //返回nil对象
17 {
18 return -1;
19 }
20 else if (reply->type == 6) //返回错误
21 {
22 return -2;
23 }
24 freeReplyObject(reply);
25 return 0;
26 }
27
28 char* 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);
32 if (reply->type == 1) //返回字符串标识
33 {
34 //printf("lindex MA_h1_K 0命令执行结果 reply type: %s\n", reply->str);
35 return reply->str;
36 }
37 else if (reply->type == 4) //返回nil对象
38 {
39 return "不存在要访问的数据";
40 }
41 else if (reply->type == 6) //返回错误
42 {
43 return reply->str;
44 }
45 freeReplyObject(reply);
46 return "";
47 }
48
49 void run_command(char run_command[200])
50 {
51 reply = (redisReply *)redisCommand(c, run_command);
52 //printf("reply type: %d\n", reply->type);
53 if (reply->type == 5)
54 {
55 //printf("run_command执行结果: %s\n", reply->str);
56 }
57 freeReplyObject(reply);
58 }
59
60 int main()
61 {
62 SYSTEMTIME sys;
63 char local_time[25] = "";
64
65 c = redisConnect((char*)redis_host, redis_port);
66 if (c->err) { /* Error flags, 0 when there is no error */
67 printf("连接Redis失败: %s\n", c->errstr);
68 exit(1);
69 }
70 else
71 {
72 printf("连接Redis成功!\n");
73 }
74
75 reply = (redisReply *)redisCommand(c, "AUTH %s", redis_password);
76 if (reply->type == REDIS_REPLY_ERROR) {
77 printf("Redis认证失败!\n");
78 }
79 else
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
99
100 //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"));
103
104
105 //llen命令
106 printf("llen MA_h1_K命令执行结果: %d\n", get_int_command("llen MA_h1_K"));
107
108
109 //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);
112 if (reply->type == 2)
113 {
114 printf("队列数量为: %d\n", reply->elements);
115 if (reply->element[0]->type == 1)
116 {
117 for (int i = 0; i < reply->elements; i++)
118 {
119 printf("lrange MA_h1_K 0 7命令执行结果: %s\n", reply->element[i]->str);
120 }
121 }
122
123 }
124 freeReplyObject(reply);
125
126 //lindex命令
127 printf("lindex MA_h1_K 0命令执行结果 : %s\n", get_string_command("lindex MA_h1_K 0"));
128
129
130 //lpush命令
131 run_command("lpush list test1 test2 test3");
132
133 //lpop命令
134 printf("lpop list命令执行结果 : %s\n", get_string_command("lpop list"));
135
136 //rpop命令
137 printf("rpop list命令执行结果 : %s\n", get_string_command("rpop list"));
138
139 //rpoplpush命令
140 printf("rpoplpush list list1命令执行结果 : %s\n", get_string_command("rpoplpush list list1"));
141
142 printf("lpop list1命令执行结果 : %s\n", get_string_command("lpop list1"));
143
144 //lpush rpush lpop rpop RPOPLPUSH
145
146
147 char test;
148 test = getchar();
149 }
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C/C+频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号