上篇博客已经写过,为了清楚的演示,再写一遍。php
打开Redis官网,进入下载页面,选择一个适合本身电脑的版本下载便可,下载飞机票http://redis.io/download,下载完成后解压、编译、安装,依次在终端下执行以下命令。java
tar -zxvf redis-2.8.7.tar.gz
cd redis-2.8.7
sudo apt-get install tcl(redis测试程序须要tcl版本至少为8.5)
make 32bit(64位系统直接使用make便可)
sudo make PREFIX=/usr/local/redis install(默认sudo make install将编译生成的可执行文件拷贝到/usr/local/redis/bin目录下;PREFIX更改一下目录)
把redis-2.8.7目录中的redis.conf复制到/usr/local/redis/bin目录,有的道友喜欢放在/bin目录下,只要本身额能找到就能够了。
make test(用于确认安装正确与否)
编译生成的可执行文件有:
1. redis-server redis服务器
2. redis-cli redis客户端
3. redis-benchmark redis性能测试工具
4. redis-check-aof aof文件修复工具
5. redis-check-dump rdb文件检查工具
6. redis-sentinel redis集群管理工具mysql
编译、安装完成后,在终端中输入redis-server
以最简单的方式启动redis服务端,而后在另外一个终端中输入redis-cli
来链接redis服务端,接下来能够尝试各类命令了,能够在http://try.redis.io预习下redis的各类命令,还能够在redis官网查看redis支持的命令。linux
须要使用C/C++操做Redis,就须要安装C/C++ Redis Client Library,这里我使用的是hiredis,这是官方使用的库,并且用得人比较多,在终端下依次执行下列命令进行下载、安装:git
git clone https://github.com/redis/hiredis
cd hiredis
make
sudo make install(复制生成的库到/usr/local/lib目录下)
sudo ldconfig /usr/local/lib
hiredis是redis数据库的C接口,目前只能在linux下使用,几个基本的函数就能够操做redis数据库了。github
函数原型:redisContext *redisConnect(const char *ip, int port);redis
说明:该函数用来链接Redis数据库,参数为数据库的ip地址和端口,通常redis数据库的端口为6379;sql
函数返回值:该函数返回一个结构体redisContext;数据库
相似的提供了一个函数redisContext* redisConnectWithTimeout(const char *ip, int port, timeval tv),以带有超时的方式链接redis服务器,同时获取与redis链接的上下文对象。编程
函数原型:void *redisCommand(redisContext *c, const char *format, ...);
说明:该函数执行命令,就如sql数据库中的SQL语句同样,只是执行的是redis数据库中的操做命令,第一个参数为链接数据库时返回的redisContext,剩下的参数为变参,就如C标准函数printf函数同样的变参。
函数返回值:返回值为void*,通常强制转换成为redisReply类型,以便作进一步处理。
函数原型void freeReplyObject(void *reply);
说明:释放redisCommand执行后返回的redisReply所占用的内存;
函数返回值:无。
函数原型:void redisFree(redisContext *c);
说明:释放redisConnect()所产生的链接。
函数返回值:无。
下面用一个简单的例子说明:
#include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <stdarg.h> #include <string.h> #include <assert.h> #include <hiredis/hiredis.h> void doTest() { //redis默认监听端口为6387 能够再配置文件中修改 redisContext* c = redisConnect("127.0.0.1", 6379); if ( c->err) { redisFree(c); printf("Connect to redisServer faile\n"); return ; } printf("Connect to redisServer Success\n"); const char* command1 = "set stest1 value1"; redisReply* r = (redisReply*)redisCommand(c, command1); if( NULL == r) { printf("Execut command1 failure\n"); redisFree(c); return; } if( !(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK")==0)) { printf("Failed to execute command[%s]\n",command1); freeReplyObject(r); redisFree(c); return; } freeReplyObject(r); printf("Succeed to execute command[%s]\n", command1); const char* command2 = "strlen stest1"; r = (redisReply*)redisCommand(c, command2); if ( r->type != REDIS_REPLY_INTEGER) { printf("Failed to execute command[%s]\n",command2); freeReplyObject(r); redisFree(c); return; } int length = r->integer; freeReplyObject(r); printf("The length of 'stest1' is %d.\n", length); printf("Succeed to execute command[%s]\n", command2); const char* command3 = "get stest1"; r = (redisReply*)redisCommand(c, command3); if ( r->type != REDIS_REPLY_STRING) { printf("Failed to execute command[%s]\n",command3); freeReplyObject(r); redisFree(c); return; } printf("The value of 'stest1' is %s\n", r->str); freeReplyObject(r); printf("Succeed to execute command[%s]\n", command3); const char* command4 = "get stest2"; r = (redisReply*)redisCommand(c, command4); if ( r->type != REDIS_REPLY_NIL) { printf("Failed to execute command[%s]\n",command4); freeReplyObject(r); redisFree(c); return; } freeReplyObject(r); printf("Succeed to execute command[%s]\n", command4); redisFree(c); } int main() { doTest(); return 0; }