Redis學習筆記 hiredis(C語言)

2021-07-28 10:55:53 字數 3870 閱讀 5039

redis學習中,c語言通過hiredis開源庫操作redis。

1 hiredis開源**的路徑。和說明文件**:

2 hiredis中部分api使用示例。

#include 

#include "hiredis.h"

const

char* redis_ip = "127.0.0.1";

const

int redis_port = 6380;

//connect redis

rediscontext* getrediscontext();

//free redis connect

void freerediscontext(rediscontext *rc);

//test redis key

int testrediskey(rediscontext *rc);

//string type

void testredisstring(rediscontext *rc);

//prase redisreplay

int praseredisreply(redisreply*& rediscmdreply, const

char*& rediscmd);

//main function

int main(int argc, char* argv)

//free redis connect

freerediscontext(rc);

return0;}

//get redis context

rediscontext* getrediscontext()

else

redisfree(rc);

return null;

}printf("connect redis ok\n");

return rc;

}//free redis context

void freerediscontext(rediscontext *c)

//redis key operate

int testrediskey(rediscontext *rc)

//keys * command

const

char* keyscommand = "keys *";

redisreply* rediscmdreply = (redisreply*)rediscommand(rc, keyscommand);

//parse redis reply

int res = praseredisreply(rediscmdreply, keyscommand);

if(res != 0)

//exists command

const

char* existscommand = "exists bar";

rediscmdreply = (redisreply*)rediscommand(rc, existscommand);

//parse redis reply

res = praseredisreply(rediscmdreply, existscommand);

if(res != 0)

const

char* existshaocommand = "exists hao";

rediscmdreply = (redisreply*)rediscommand(rc, existshaocommand);

//parse redis reply

res = praseredisreply(rediscmdreply, existshaocommand);

if(res != 0)

//delete key command

const

char* delkeycomman = "del bar";

rediscmdreply = (redisreply*)rediscommand(rc, delkeycomman);

//parse redis reply

res = praseredisreply(rediscmdreply, delkeycomman);

if(res != 0)

rediscmdreply = (redisreply*)rediscommand(rc, keyscommand);

//parse redis reply

res = praseredisreply(rediscmdreply, keyscommand);

if(res != 0)

//free redisreply

freereplyobject(rediscmdreply);

return0;}

//string type

void testredisstring(rediscontext *rc)

//prase redis reply

int praseredisreply(redisreply*& rediscmdreply, const

char*& rediscmd)

if(null == rediscmdreply)

printf("redisreply type:%d\n", rediscmdreply->type);

int ret = 0;

switch(rediscmdreply->type)

case redis_reply_array:

ret = 0;

break;

}case redis_reply_integer:

case redis_reply_nil:

case redis_reply_status:

case redis_reply_error:

default:

}return ret;

}

3 編譯問題

使用hiredis的makefile檔案編譯出hiredis的靜態庫(命令make static)。

檔案目錄

hiredis makefile testredis.cpp

自己的makefile檔案內容:

#g++

cxx = g++ -g -wall

#redis dir

hredis_dir = ./hiredis

#release dir

release_dir = ./release

#cflags

cflags = -i./ -i

$(hredis_dir)

#terg

targets = testredis

#object

objs = ./testredis.o

all:

$(targets)

./testredis.o: ./testredis.cpp $(

hredis_dir)/hiredis.h

$(cxx) $(

cflags) -c ./testredis.cpp -o $@

$(targets):

$(objs) $(

hredis_dir)/libhiredis.a

test -d $(

release_dir) || mkdir -p $(

release_dir)

$(cxx) -o $@

$^ cp $@

$(release_dir)

clean:

rm -f $(

targets) $(

release_dir)/* ./*.o

redis 學習筆記

本文寫作源於最近產品中部分功能用到了redis redis在功能上是乙個與memecache類似的軟體,但它比memecache提供了更強大的功能。redis本質上是乙個key value型別的記憶體資料庫。其value可以是string,list,set,sorted set,hash。很多地方都...

redis學習筆記

1.redis的無序集合 sadd命令產生乙個無序集合 smembers檢視集合元素 sismember用於檢視集合是否存在,匹配項包括集合名和元素個數。匹配成功返回1,匹配失敗返回0 2.有序集合 zadd 生成乙個有序集合。zrange檢視正序的集合,zrevrange檢視反序的集合 set和g...

redis學習筆記

redis啟動伺服器和客戶端的相關命令 string型別 乙個key對應乙個value set key value 比如 set name geyule setnx 如果key存在返回0,nx表示not exist setex 指定有效期 setrange 替換 setrange key n val...