首先从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