亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

c開發如何使用redis

小新
348
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
宜良县| 临沧市| 孝义市| 昂仁县| 赤城县| 泾源县| 龙陵县| 湖北省| 恩平市| 喀喇沁旗| 太白县| 衡阳县| 沈阳市| 民和| 宾川县| 仪征市| 泽库县| 永泰县| 正定县| 元谋县| 和静县| 佛教| 抚顺市| 阜宁县| 全南县| 高州市| 淳化县| 思茅市| 荥经县| 江门市| 淮南市| 海淀区| 武安市| 日土县| 宿州市| 龙江县| 陈巴尔虎旗| 衡东县| 宽甸| 阆中市| 抚宁县|