c开发如何使用redis c++开发经验

主机教程 建站分享 2年前 (2022-09-28) 190次浏览

文章摘要:c开发如何使用redis c++开发经验

c开发使用redis的示例: 在linux下使用hiredis接口,hiredis是redis数据库的C接口, […]

c开发使用redis的示例:

在linux下使用hiredis接口,hiredis是redis数据库的C接口,代码:

#include "hiredis.h"

#define NO_QFORKIMPL

#pragma comment(lib,"hiredis.lib")

#pragma comment(lib,"Win32_Interop.lib")

int get_int_command(char int_command[200])

{

reply = (redisReply *)redisCommand(c, int_command);

//printf("exists命令执行结果: %d
", reply->type);

if (reply->type == 3) //返回整型标识

{

//printf("%s命令执行结果: %d
", int_command, reply->integer);

return reply->integer;

}

else if (reply->type == 4) //返回nil对象

{

return -1;

}

else if (reply->type == 6) //返回错误

{

return -2;

}

freeReplyObject(reply);

return 0;

}

char* get_string_command(char string_command[200])

{

reply = (redisReply *)redisCommand(c, string_command);

//printf("lindex MA_h1_K 0命令执行结果 reply type: %d
", reply->type);

if (reply->type == 1) //返回字符串标识

{

//printf("lindex MA_h1_K 0命令执行结果 reply type: %s
", reply->str);

return reply->str;

}

else if (reply->type == 4) //返回nil对象

{

return "不存在要访问的数据";

}

else if (reply->type == 6) //返回错误

{

return reply->str;

}

freeReplyObject(reply);

return "";

}

void run_command(char run_command[200])

{

reply = (redisReply *)redisCommand(c, run_command);

//printf("reply type: %d
", reply->type);

if (reply->type == 5)

{

//printf("run_command执行结果: %s
", reply->str);

}

freeReplyObject(reply);

}

int main()

{

SYSTEMTIME sys;

char local_time[25] = "";

c = redisConnect((char*)redis_host, redis_port);

if (c->err) { /* Error flags, 0 when there is no error */

printf("连接Redis失败: %s
", c->errstr);

exit(1);

}

else

{

printf("连接Redis成功!
");

}

reply = (redisReply *)redisCommand(c, "AUTH %s", redis_password);

if (reply->type == REDIS_REPLY_ERROR) {

printf("Redis认证失败!
");

}

else

{

printf("Redis认证成功!
");

}

freeReplyObject(reply);

reply = (redisReply *)redisCommand(c, "SELECT 1"); //选择数据库

printf("SELECT: 1 %s
", reply->str);

freeReplyObject(reply);

//delete命令

run_command("DEL foo");

//set命令

run_command("SET foo hello world");

//get命令

printf("GET foo命令执行结果 : %s
", get_string_command("GET foo"));

//exists命令

printf("exists test1命令执行结果: %d
", get_int_command("exists test1"));

printf("exists MA_h1_K命令执行结果: %d
", get_int_command("exists MA_h1_K"));

//llen命令

printf("llen MA_h1_K命令执行结果: %d
", get_int_command("llen MA_h1_K"));

//lrange命令

reply = (redisReply *)redisCommand(c, "lrange MA_h1_K 0 7");

//printf("lrange MA_h1_K 0 7命令执行结果 reply type: %d
", reply->type);

if (reply->type == 2)

{

printf("队列数量为: %d
", reply->elements);

if (reply->element[0]->type == 1)

{

for (int i = 0; i < reply->elements; i++)

{

printf("lrange MA_h1_K 0 7命令执行结果: %s
", reply->element[i]->str);

}

}

}

freeReplyObject(reply);

//lindex命令

printf("lindex MA_h1_K 0命令执行结果 : %s
", get_string_command("lindex MA_h1_K 0"));

//lpush命令

run_command("lpush list test1 test2 test3");

//lpop命令

printf("lpop list命令执行结果 : %s
", get_string_command("lpop list"));

//rpop命令

printf("rpop list命令执行结果 : %s
", get_string_command("rpop list"));

//rpoplpush命令

printf("rpoplpush list list1命令执行结果 : %s
", get_string_command("rpoplpush list list1"));

printf("lpop list1命令执行结果 : %s
", get_string_command("lpop list1"));

//lpush rpush lpop rpop RPOPLPUSH

char test;

test = getchar();

}


声明:
若非注明,本站文章源于互联网收集整理和网友分享发布,如有侵权,请联系站长处理。
文章名称:c开发如何使用redis c++开发经验
文章链接:http://www.7966.org/post/16569.html
转载请注明出处

喜欢 (0)