redis

c开发如何使用redis

小新
351
2020-12-25 18:36:22
栏目: 云计算

c开发如何使用redis

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\n", reply->type);

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

{

//printf("%s命令执行结果: %d\n", 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\n", reply->type);

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

{

//printf("lindex MA_h1_K 0命令执行结果 reply type: %s\n", 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\n", reply->type);

if (reply->type == 5)

{

//printf("run_command执行结果: %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认证失败!\n");

}

else

{

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

}

freeReplyObject(reply);

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

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命令执行结果 : %s\n", get_string_command("GET foo"));

//exists命令

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

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

//llen命令

printf("llen MA_h1_K命令执行结果: %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命令执行结果 reply type: %d\n", reply->type);

if (reply->type == 2)

{

printf("队列数量为: %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命令执行结果: %s\n", reply->element[i]->str);

}

}

}

freeReplyObject(reply);

//lindex命令

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

//lpush命令

run_command("lpush list test1 test2 test3");

//lpop命令

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

//rpop命令

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

//rpoplpush命令

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

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

//lpush rpush lpop rpop RPOPLPUSH

char test;

test = getchar();

}

0
看了该问题的人还看了