Redis C语言API hiredis 安装

首先从Github下载 hiredis-master.zip

地址:https://github.com/redis/hiredis

安装:

$unzip hiredis-master.zip

$cd hiredis-master

$make

如上操作,C客户端就装好,关键是在于配置,可以手动配置,也可以直接make install,这样会将需要的libhiredis.so libhiredis.a 等文件移动到/usr/local/lib目录下,头文件移动到了/user/local/include目录下。

$make install


示例程序:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "hiredis.h"

int main(void) {
	redisReply *reply;
	long int i;

//      Start measuring time
	clock_t start = clock();

//      For local connections:
//	redisContext *c = redisConnect("127.0.0.1", 6379);
//      For connections to a remote Redis server:
	redisContext *c = redisConnect
                 ("ec2-**-**-***-**.compute-1.amazonaws.com", 6379);
	if (c->err) {
        	printf("Error: %s\n", c->errstr);
    	}else{
        	printf("Connection Made! \n");
    	}
//	Get all keys for testing
	reply = redisCommand(c, "keys %s", "*");
	if ( reply->type == REDIS_REPLY_ERROR )
  		printf( "Error: %s\n", reply->str );
	else if ( reply->type != REDIS_REPLY_ARRAY )
  		printf( "Unexpected type: %d\n", reply->type );
	else {
  		for ( i=0; i< reply->elements; ++i ){
    		   printf( "Result:%lu: %s\n", i,
                      reply->element[i]->str );
  		}
	}
	printf( "Total Number of Results: %lu\n", i );

//      Output Elapsed time
        printf ( "%f Seconds\n", ( (double)clock() - start ) /
                      CLOCKS_PER_SEC );

	freeReplyObject(reply);
}

$gcc 

cc -o connect connect.c libhiredis.a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值