c開(kāi)發(fā)如何使用redis

小新
349
2020-12-25 18:36:22
欄目: 云計(jì)算

c開(kāi)發(fā)如何使用redis

c開(kāi)發(fā)使用redis的示例:

在linux下使用hiredis接口,hiredis是redis數(shù)據(jù)庫(kù)的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命令執(zhí)行結(jié)果: %d\n", reply->type);

if (reply->type == 3) //返回整型標(biāo)識(shí)

{

//printf("%s命令執(zhí)行結(jié)果: %d\n", int_command, reply->integer);

return reply->integer;

}

else if (reply->type == 4) //返回nil對(duì)象

{

return -1;

}

else if (reply->type == 6) //返回錯(cuò)誤

{

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命令執(zhí)行結(jié)果 reply type: %d\n", reply->type);

if (reply->type == 1) //返回字符串標(biāo)識(shí)

{

//printf("lindex MA_h1_K 0命令執(zhí)行結(jié)果 reply type: %s\n", reply->str);

return reply->str;

}

else if (reply->type == 4) //返回nil對(duì)象

{

return "不存在要訪問(wèn)的數(shù)據(jù)";

}

else if (reply->type == 6) //返回錯(cuò)誤

{

return reply->str;

}

freeReplyObject(reply);

return "";

}

void run_command(char run_command[200])

{

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

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

if (reply->type == 5)

{

//printf("run_command執(zhí)行結(jié)果: %s\n", 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\n", c->errstr);

exit(1);

}

else

{

printf("連接Redis成功!\n");

}

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

if (reply->type == REDIS_REPLY_ERROR) {

printf("Redis認(rèn)證失??!\n");

}

else

{

printf("Redis認(rèn)證成功!\n");

}

freeReplyObject(reply);

reply = (redisReply *)redisCommand(c, "SELECT 1"); //選擇數(shù)據(jù)庫(kù)

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

freeReplyObject(reply);

//delete命令

run_command("DEL foo");

//set命令

run_command("SET foo hello world");

//get命令

printf("GET foo命令執(zhí)行結(jié)果 : %s\n", get_string_command("GET foo"));

//exists命令

printf("exists test1命令執(zhí)行結(jié)果: %d\n", get_int_command("exists test1"));

printf("exists MA_h1_K命令執(zhí)行結(jié)果: %d\n", get_int_command("exists MA_h1_K"));

//llen命令

printf("llen MA_h1_K命令執(zhí)行結(jié)果: %d\n", 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命令執(zhí)行結(jié)果 reply type: %d\n", reply->type);

if (reply->type == 2)

{

printf("隊(duì)列數(shù)量為: %d\n", reply->elements);

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

{

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

{

printf("lrange MA_h1_K 0 7命令執(zhí)行結(jié)果: %s\n", reply->element[i]->str);

}

}

}

freeReplyObject(reply);

//lindex命令

printf("lindex MA_h1_K 0命令執(zhí)行結(jié)果 : %s\n", get_string_command("lindex MA_h1_K 0"));

//lpush命令

run_command("lpush list test1 test2 test3");

//lpop命令

printf("lpop list命令執(zhí)行結(jié)果 : %s\n", get_string_command("lpop list"));

//rpop命令

printf("rpop list命令執(zhí)行結(jié)果 : %s\n", get_string_command("rpop list"));

//rpoplpush命令

printf("rpoplpush list list1命令執(zhí)行結(jié)果 : %s\n", get_string_command("rpoplpush list list1"));

printf("lpop list1命令執(zhí)行結(jié)果 : %s\n", get_string_command("lpop list1"));

//lpush rpush lpop rpop RPOPLPUSH

char test;

test = getchar();

}

0