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();
}