使用不同的语言,redis支持不同的编程语言,但是调用了不同的redis包,例如:
java对应jedis;
php对应phpredis;
C++对应的则是hredis。
安装Redis
上篇博客已经写过,为了清楚的演示,再写一遍。
打开Redis官网,进入下载页面,选择一个适合自己电脑的版本下载即可,下载飞机票http://redis.io/download,下载完成后解压、编译、安装,依次在终端下执行如下命令。
tar -zxvf redis-2.8.7.tar.gzcd redis-2.8.7sudo 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集群管理工具
编译、安装完成后,在终端中输入redis-server以最简单的方式启动redis服务端,然后在另一个终端中输入redis-cli来连接redis服务端,接下来可以尝试各种命令了,可以在http://try.redis.io预习下redis的各种命令,还可以在redis官网查看redis支持的命令。
安装hiredis
需要使用C/C++操作Redis,就需要安装C/C++ Redis Client Library,这里我使用的是hiredis,这是官方使用的库,而且用得人比较多,在终端下依次执行下列命令进行下载、安装:
git clone https://github.com/redis/hirediscd hiredismakesudo make install(复制生成的库到/usr/local/lib目录下)sudo ldconfig /usr/local/lib
C/C++操作Redis
hiredis是redis数据库的C接口,目前只能在linux下使用,几个基本的函数就可以操作redis数据库了。
函数原型:redisContext *redisConnect(const char *ip, int port);
说明:该函数用来连接Redis数据库,参数为数据库的ip地址和端口,一般redis数据库的端口为6379;
函数返回值:该函数返回一个结构体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
");
return ;
}
printf("Connect to redisServer Success
");
const char* command1 = "set stest1 value1";
redisReply* r = (redisReply*)redisCommand(c, command1);
if( NULL == r)
{
printf("Execut command1 failure
");
redisFree(c);
return;
}
if( !(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK")==0))
{
printf("Failed to execute command[%s]
",command1);
freeReplyObject(r);
redisFree(c);
return;
}
freeReplyObject(r);
printf("Succeed to execute command[%s]
", command1);
const char* command2 = "strlen stest1";
r = (redisReply*)redisCommand(c, command2);
if ( r->type != REDIS_REPLY_INTEGER)
{
printf("Failed to execute command[%s]
",command2);
freeReplyObject(r);
redisFree(c);
return;
}
int length = r->integer;
freeReplyObject(r);
printf("The length of 'stest1' is %d.
", length);
printf("Succeed to execute command[%s]
", command2);
const char* command3 = "get stest1";
r = (redisReply*)redisCommand(c, command3);
if ( r->type != REDIS_REPLY_STRING)
{
printf("Failed to execute command[%s]
",command3);
freeReplyObject(r);
redisFree(c);
return;
}
printf("The value of 'stest1' is %s
", r->str);
freeReplyObject(r);
printf("Succeed to execute command[%s]
", command3);
const char* command4 = "get stest2";
r = (redisReply*)redisCommand(c, command4);
if ( r->type != REDIS_REPLY_NIL)
{
printf("Failed to execute command[%s]
",command4);
freeReplyObject(r);
redisFree(c);
return;
}
freeReplyObject(r);
printf("Succeed to execute command[%s]
", command4);
redisFree(c);
}
int main()
{
doTest();
return 0;
}
- http://redis.io/:Redis官网
- http://redis.cn/:Redis中文官网
- http://try.redis.io/:在线体验Redis
- https://github.com/antirez/redis:Redis开发版本源码
- http://www.redisdoc.com/en/latest/:Redis命令参考
- http://blog.nosqlfan.com/topics/redis:Redis系类文章
- http://redisbook.readthedocs.org/en/latest/:Redis设计与实现
- https://github.com/huangz1990/annotated_redis_source:注释版Redis源码