Redis学习中,C语言通过hiredis开源库操作redis。
1 hiredis开源代码的路径。和说明文档网址:
https://github.com/redis/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[])
{
//get redis connect
redisContext* rc = getRedisContext();
if(rc == NULL) return -1;
int res = testRedisKey(rc);
if(0 != res){
return -1;
}
//free redis connect
freeRedisContext(rc);
return 0;
}
//get redis context
redisContext* getRedisContext()
{
printf("Redis IP:%s Port:%d\n", REDIS_IP, REDIS_PORT);
redisContext* rc = redisConnect(REDIS_IP, REDIS_PORT);
if(NULL == rc || rc->err)
{
if(rc){
printf("Connect Redis Error:%s\n", rc->errstr);
}else{
printf("Can't allocate redis context\n");
}
redisFree(rc);
return NULL;
}
printf("Connect Redis OK\n");
return rc;
}
//free redis context
void freeRedisContext(redisContext *c)
{
printf("Free Redis Context.\n");
redisFree(c);
return ;
}
//redis key operate
int testRedisKey(redisContext *rc)
{
printf("redis key operate\n");
if(NULL == rc){
printf("redisContext is NULL\n");
return -1;
}
//KEYS * command
const char* keysCommand = "KEYS *";
redisReply* redisCmdReply = (redisReply*)redisCommand(rc, keysCommand);
//parse redis reply
int res = praseRedisReply(redisCmdReply, keysCommand);
if(res != 0){
freeReplyObject(redisCmdReply);
freeRedisContext(rc);
return -1;
}
//EXISTS command
const char* existsCommand = "EXISTS bar";
redisCmdReply = (redisReply*)redisCommand(rc, existsCommand);
//parse redis reply
res = praseRedisReply(redisCmdReply, existsCommand);
if(res != 0){
freeReplyObject(redisCmdReply);
freeRedisContext(rc);
return -1;
}
const char* existsHaoCommand = "EXISTS hao";
redisCmdReply = (redisReply*)redisCommand(rc, existsHaoCommand);
//parse redis reply
res = praseRedisReply(redisCmdReply, existsHaoCommand);
if(res != 0){
freeReplyObject(redisCmdReply);
freeRedisContext(rc);
return -1;
}
//DELETE key command
const char* delKeycomman = "DEL bar";
redisCmdReply = (redisReply*)redisCommand(rc, delKeycomman);
//parse redis reply
res = praseRedisReply(redisCmdReply, delKeycomman);
if(res != 0){
freeReplyObject(redisCmdReply);
freeRedisContext(rc);
return -1;
}
redisCmdReply = (redisReply*)redisCommand(rc, keysCommand);
//parse redis reply
res = praseRedisReply(redisCmdReply, keysCommand);
if(res != 0){
freeReplyObject(redisCmdReply);
freeRedisContext(rc);
return -1;
}
//free redisReply
freeReplyObject(redisCmdReply);
return 0;
}
//string type
void testRedisString(redisContext *rc)
{
printf("Test Redius String Type.\n");
return ;
}
//prase redis reply
int praseRedisReply(redisReply*& redisCmdReply, const char*& redisCmd)
{
if(NULL == redisCmd){
printf("Invalid Parameter: redisCmd is NULL\n");
return -1;
}
if(NULL == redisCmdReply){
printf("Execute Command [%s] Error: redisCmdReply is NULL.\n", redisCmd);
return -1;
}
printf("redisReply Type:%d\n", redisCmdReply->type);
int ret = 0;
switch(redisCmdReply->type){
case REDIS_REPLY_STRING:
{
printf("Execute Command [%s] return string[%s]", redisCmd, redisCmdReply->str);
ret = 0;
break;
}
case REDIS_REPLY_ARRAY:
{
printf("Execute Command [%s] return context:\n", redisCmd);
redisReply* temp = NULL;
for(int i=0; ielements; i++){
temp = redisCmdReply->element[i];
printf("(%d) %s\n", temp->type, temp->str);
}
ret = 0;
break;
}
case REDIS_REPLY_INTEGER:
{
printf("Execute Command [%s] return intger[%lld].\n", redisCmd, redisCmdReply->integer);
ret = 0;
break;
}
case REDIS_REPLY_NIL:
{
printf("Execute Command [%s] return nil.\n", redisCmd);
ret = -1;
break;
}
case REDIS_REPLY_STATUS:
{
printf("Execute Command [%s] return status[%s].\n", redisCmd, redisCmdReply->str);
ret = 0;
break;
}
case REDIS_REPLY_ERROR:
{
printf("Execute Command [%s] return error[%s].\n", redisCmd, redisCmdReply->str);
ret = -1;
break;
}
default:
{
printf("Execute Command [%s] return unknown type\n", redisCmd);
ret = -1;
break;
}
}
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