Redis
1. wget http://download.redis.io/releases/redis-4.0.8.tar.gz
2. tar xzvf redis-4.0.8.tar.gz
3.
cd redis-4.0.8
make
cd src
make install #PREFIX=/usr/local/redis
export LD_LIBRARY_PATH=/usr/local/lib #or modify bash_profile
4.
cd ../
mkdir /usr/local/redis/etc
mv redis.conf /usr/local/redis/etc
5.
cd /usr/local/redis/bin
./redis-server
./redis-cli
6.
keys *
set key1 "tom"
get key1
7.
HIRedis
https://github.com/redis/hiredis
download zip
unzip and make
qss_redis_API.h
#ifndef RTSSERVER_QSS_REDIS_API_H#define RTSSERVER_QSS_REDIS_API_H
int Send2Redis(const char *ip,intport,const char *key,const char *value,intlen_value);#endif //RTSSERVER_QSS_REDIS_API_H
qss_redis_API.c
#include #include#include#include"hiredis.h"
int Send2Redis(const char *ip,intport,const char *key,const char *value,intlen_value){
printf("Enter Send2Redis\n");
redisContext*context =redisConnect(ip, port);if (context == NULL || context->err) {if(context) {
printf("Error: %s\n", context->errstr);//handle error
return -1;
}else{
printf("Can't allocate redis context\n");return -2;
}
}
printf("send date key:[%s] value[%d]:[%s]\n",key,len_value,value);
redisReply*reply;
redisAppendCommand(context,"SET %s %b",key, value, (size_t) len_value);if(REDIS_OK != redisGetReply(context,(void**)&reply))//reply for SET
{
printf("ERR:after set: reply:[%s]\n",reply->str);
freeReplyObject(reply);gotoERR;
}
printf("after set: reply:[%s]\n",reply->str);
freeReplyObject(reply);//redisAppendCommand(context, "GET %s ",key);//redisGetReply(context,(void**)&reply);//reply for SET//printf("after GET: reply:[%s]\n",reply->str);//freeReplyObject(reply);
printf("return \n");
ERR:
redisFree(context);
}
main.c
#include #include#include#include"qss_redis_API.h"
intmain(){char key[]="key1512";char value[]="this is the message for test!!!";
Send2Redis("127.0.0.1", 6379,
key,value,strlen(value));
}