redis客户端

http://redis.io/clients

我是下载的hiredis~~~~·哎,先用别人的吧

等做进去了,自己在封装一个。

下面是我写的一个测试例子。

#include "hiredis.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
void print(redisReply * reply)
{
	//cout<<reply->type<<endl;
	cout<<reply->integer<<endl;
	//cout<<reply->len<<endl;
	//cout<<reply->str<<endl;
	//cout<<reply->elements<<endl;
}
int main()
{
	unsigned int j;    
	redisContext *c;    
	redisReply *reply;
	struct timeval timeout = { 1, 500000 }; // 1.5 seconds    
	c = redisConnectWithTimeout((char*)"127.0.0.2", 6379, timeout);
	//test set
	reply = (redisReply *)redisCommand(c,"SET %s %s", "foo", "hello world");
	cout<<"test set------------------------------"<<endl;
	print(reply);
	freeReplyObject(reply);
	
	//test get
	reply = (redisReply *)redisCommand(c,"GET %s","foo");
	cout<<"test get------------------------------"<<endl;
	print(reply);
	freeReplyObject(reply);

	//test list
	reply = (redisReply*)redisCommand(c,"RPUSH %b %b","book",strlen("book"),"the language c++",strlen("the language c++"));
	cout<<"test list add1---------------------------------"<<endl;
	print(reply);
	cout<<"22222";
	freeReplyObject(reply);
	cout<<"33333";

	reply = (redisReply*)redisCommand(c,"RPUSH %s %s","book","thinking in c++");
        cout<<"test list add2---------------------------------"<<endl;
	print(reply);
        freeReplyObject(reply);

	cout<<"test get list result"<<endl;
	reply = (redisReply*)redisCommand(c,"lrange %s %d %d","book",0,-1);
	for(int i=0;i<reply->elements;i++)
	{
		cout<<"get list:"<<reply->element[i]->str<<endl;
	}
	freeReplyObject(reply);

	
	cout<<"test erase list item"<<endl;
	reply  = (redisReply*)redisCommand(c,"lrem %s 0 %s","book","thinking in c++");
	print(reply);
	cout<<"endl"<<endl;
	return 0;
}

/* This is the reply object returned by redisCommand() */
typedef struct redisReply {
    int type; /* REDIS_REPLY_* */
    long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
    int len; /* Length of string */
    char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
    size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
    struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
} redisReply;

原文地址:https://www.cnblogs.com/xloogson/p/2176258.html