lgy514

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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
//
// Created by liuguoyao on 2019/6/13.
//

#ifndef RTSSERVER_QSS_REDIS_API_H
#define RTSSERVER_QSS_REDIS_API_H
int Send2Redis(const char *ip,int port,
const char *key,const char *value,int len_value);

//warning!!! after call the function ,free(rvalue)
int ReceivFromRedis(const char *ip,int port,
const char *key, char **rvalue,int *len_rvalue);
#endif //RTSSERVER_QSS_REDIS_API_H

 

 

qss_redis_API.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "hiredis.h"

int Send2Redis(const char *ip,int port,
        const char *key,const char *value,int len_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);
        goto ERR;
    }
    printf("after set: reply:[%s]\n",reply->str);
    freeReplyObject(reply);

    printf("return \n");
ERR:
    redisFree(context);
}

int ReceivFromRedis(const char *ip,int port,
                    const char *key, char **rvalue,int *len_rvalue)
{
    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;
        }
    }

    redisReply *reply;

    redisAppendCommand(context, "GET %s ",key);
    if(REDIS_OK != redisGetReply(context,(void**)&reply))// reply for SET
    {
        printf("ERR:after set: reply:[%s]\n",reply->str);
        freeReplyObject(reply);
        goto ERR;
    }
    //printf("after GET: reply[%d]:[%s]\n",reply->len,reply->str);
    printf("after GET: replylen[%d]\n",reply->len);
    *rvalue= (char *)malloc(sizeof(char)*reply->len);
    memcpy(*rvalue,reply->str,sizeof(char)*reply->len);
    *len_rvalue = reply->len;
    freeReplyObject(reply);

    printf("return \n");
    ERR:
    redisFree(context);
}

 

 

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "qss_redis_API.h"

int main(){

    char key[]="key1512";
    char value[]="this is the message for test!!!";
    Send2Redis("127.0.0.1", 6379,
               key,value,strlen(value));
    char *rv;
    int rvlen;
    ReceivFromRedis("127.0.0.1", 6379,key,&rv,&rvlen);
    printf("receiv[%d]:[%s]\n",rvlen,rv);
    free(rv);

}

 

makefile

#export LD_LIBRARY_PATH+=:/home/rcp/RtsServer/frame/lib/
INCLUDE=-I./
LIB=-L./ -lhiredis_vip

target:hiredistest
hiredistest:qss_redis_API.o Testcase_redis.o
    gcc -o $@ $^ $(INCLUDE) $(LIB)
*.o:*.c
    gcc -c $@ $^

install:
    cp qss_redis_API.o /home/rcp/RtsServer/frame/src/irs/bondvalue

clean:
    rm *.o hiredistest

 

posted on 2019-06-14 09:51  lgy514  阅读(1239)  评论(0编辑  收藏  举报