redis整理一--下载、安装、配置、调用示例
2018-02-27 18:18
639 查看
1.redis简介
Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理。它支持字符串、哈希表、列表、集合、有序集合,位图,hyperloglogs等数据类型。内置复制、Lua脚本、LRU收回、事务以及不同级别磁盘持久化功能,同时通过Redis Sentinel提供高可用,通过Redis Cluster提供自动分区。注:redis可跨平台,可运行于windows和linux各版本,作者平台ubuntu14.04,下载版本为linux,有兴趣读者可搜索windows上使用方法。
2.redis源码下载及安装
2.1.源码下载
源码下载地址:选取官网 http://www.redis.net.cn/官网上亦有大量相关介绍,欲进一步学习了解,可参阅官网内容。或去源码:wget http://redis.googlecode.com/files/redis-2.4.6.tar.gz注:为配合源码分析,作者下载的为redis 3.0版本,下载位置一样。
2.2.安装方法
Redis的代码遵循ANSI-C编写,可以在所有POSIX系统(如Linux, *BSD, Mac OS X, Solaris等)上安装运行。而且Redis并不依赖任何非标准库,也没有编译参数必需添加。redis的安装出奇的简单,这可能也是他风靡的一个原因,让人很容易上手,不像某些东西,编译阶段就能让人完全绝望。1)解压:tar –zxvf redis-2.4.6.tar.gz编译需要说明的事,redis的安装非常简单,已经有现成的Makefile文件,直接运行make命令即可。makemake install
注意安装权限问题。
2)安装成功与否测试终端运行:redis-server,就可以启用redis服务,可以用netstat命令查看redis监听的端口(默认是6379)或调用:ps -aux |grep redis 查看进程
另开启终端,运行:redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set test hello
OK
127.0.0.1:6379> get test
"hello"
127.0.0.1:6379>
可参考文章:http://www.cnblogs.com/shanyou/archive/2012/01/28/2330451.html
3.redis配置
redis-server /etc/path<redis>/redis.conf注意:修改redis路径。
作者直接选取其默认配置,未做修改,该部分后续根据需要进行补充,有需要的读者可参考网上相关文章,亦可参考其官网说明文档,很详细。
4.redis使用实例
4.1.redis命令操作
参考redis官网教程。4.2.linux下C语言调用--hiredis
4.2.1.C语言接口库下载
Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便。 在这里简单介绍一下redis的C接口库:C语言接口函数库hiredis,下载地址:https://github.com/redis/hiredishiredis是redis的C接口库,使用之前我们要先下载安装hiredis,下载地址:https://github.com/redis/hiredis.gitgit clone https://github.com/redis/hiredis.git[/code] 下载之后进入hiredis目录makemake install
参考文章: https://www.cnblogs.com/wenqiang/p/5506744.html4.2.2.hiredis简介
下面进行hredis库中几个常用函数介绍(1)redisConnect函数该函数用于连接redis数据库函数原型:1 redisContext *redisConnect(const char *ip, int port); 2 //说明:该函数用来连接redis数据库,参数为数据库的ip地址和端口,一般redis数据库的端口为6379该函数返回一个结构体redisContext。redisContext结构体定义如下:1 /* Context for a connection to Redis */ 2 typedef struct redisContext { 3 int err; /* Error flags, 0 when there is no error */ 4 char errstr[128]; /* String representation of error when applicable */ 5 int fd; 6 int flags; 7 char *obuf; /* Write buffer */ 8 redisReader *reader; /* Protocol reader */ 9 10 enum redisConnectionType connection_type; 11 struct timeval *timeout; 12 13 struct { 14 char *host; 15 char *source_addr; 16 int port; 17 } tcp; 18 19 struct { 20 char *path; 21 } unix_sock; 22 23 } redisContext;(2)redisCommand函数该函数用于执行redis的命令;函数原型:1 void *redisCommand(redisContext *c, const char *format, ...); 2 /*说明:该函数执行命令,就如sql数据库中的SQL语句一样,只是执行的是redis数据库中的操作命令, 3 第一个参数为连接数据库时返回的redisContext, 4 后面为可变参数列表,跟C语言printf函数类似, 5 返回值为void*,一般强制转换成为redisReply类型的进行进一步的处理。*/用法:redisReply *reply = (redisReply*)redisCommand(c, cmd);redisReply结构体定义如下:1 /* This is the reply object returned by redisCommand() */ 2 typedef struct redisReply { 3 int type; /* REDIS_REPLY_* */ 4 long long integer; /* The integer when type is REDIS_REPLY_INTEGER */ 5 size_t len; /* Length of string */ 6 char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */ 7 size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */ 8 struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */ 9 } redisReply;下面是几种redis的常见错误及返回值类型1 #define REDIS_ERR -1 2 #define REDIS_OK 0 3 #define REDIS_ERR_IO 1 /* Error in read or write */ 4 #define REDIS_ERR_EOF 3 /* End of file */ 5 #define REDIS_ERR_PROTOCOL 4 /* Protocol error */ 6 #define REDIS_ERR_OOM 5 /* Out of memory */ 7 #define REDIS_ERR_OTHER 2 /* Everything else... */ 8 9 #define REDIS_REPLY_STRING 1 //返回字符串,查看str,len字段 10 #define REDIS_REPLY_ARRAY 2 //返回一个数组,查看elements的值(数组个数),通过element[index]的方式访问数组元素,每个数组元素是一个redisReply对象的指针 11 #define REDIS_REPLY_INTEGER 3 //返回整数,从integer字段获取值 12 #define REDIS_REPLY_NIL 4 //没有数据返回 13 #define REDIS_REPLY_STATUS 5 //表示状态,内容通过str字段查看,字符串长度是len字段 14 #define REDIS_REPLY_ERROR 6 //表示出错,查看出错信息,如上的str,len字段(3)redisRelpyObject函数还函数用于释放redisCommand返回值redisReply所占用的内存1 /* Free a reply object */ 2 void freeReplyObject(void *reply) 3 //该函数用于回收释放redisCommand返回值redisReply所占用的内存(4)redisFree函数该函数用于释放一个redis数据库的连接1 void redisFree(redisContext *c) 2 //用于释放redisConnect产生的连接
4.2.3.使用示例
下面写一个简单的hredis c接口的redis测试程序:1 #include <stdio.h> 2 #include <string.h> 3 #include <stddef.h> 4 #include <stdarg.h> 5 #include <string.h> 6 #include <assert.h> 7 #include <hiredis/hiredis.h> //redis C接口库 8 9 #define REDIS_HOST "127.0.0.1" 10 #define REDIS_PORT 6379 11 12 redisContext *g_ctx = NULL; 13 14 int redis_init() 15 { 16 redisContext *c = NULL; 17 c = redisConnect(REDIS_HOST, REDIS_PORT); 18 if (NULL == c || c->err) { 19 if(c) { 20 printf("Redis [%s:%d], Error:[%s]\n", REDIS_HOST, REDIS_PORT, c->errstr); 21 redisFree(c); 22 } else { 23 printf("Redis [%s:%d] failure\n", REDIS_HOST, REDIS_PORT); 24 } 25 return -1; 26 } 27 g_ctx = c; 28 29 return 0; 30 } 31 32 void redis_free() 33 { 34 if (g_ctx) { 35 redisFree(g_ctx); 36 } 37 g_ctx = NULL; 38 } 39 40 int redis_test(const char *cmd) 41 { 42 int i = 0; 43 redisReply *r = NULL; 44 if (NULL == cmd) { 45 return -1; 46 } 47 48 printf("%s\n", cmd); 49 50 r = (redisReply *)redisCommand(g_ctx, cmd); 51 if (NULL == r) { 52 printf("%s, Error[%d:%s]", g_ctx->err, g_ctx->errstr); 53 return -1; 54 } 55 56 printf("type: %d\n", r->type); 57 switch(r->type) { 58 case REDIS_REPLY_STATUS: 59 printf("type:%s, reply->len:%d reply->str:%s\n", "REDIS_REPLY_STATUS", r->len, r->str); 60 break; 61 case REDIS_REPLY_ERROR: 62 printf("type:%s, reply->len:%d reply->str:%s\n", "REDIS_REPLY_ERROR", r->len, r->str); 63 break; 64 case REDIS_REPLY_INTEGER: 65 printf("type:%s, reply->integer:%lld\n", "REDIS_REPLY_INTEGER", r->integer); 66 break; 67 case REDIS_REPLY_NIL: 68 printf("type:%s, no data\n", "REDIS_REPLY_NIL"); 69 break; 70 case REDIS_REPLY_STRING: 71 printf("type:%s, reply->len:%d reply->str:%s\n", "REDIS_REPLY_STRING", r->len, r->str); 72 break; 73 case REDIS_REPLY_ARRAY: 74 printf("type:%s, reply->elements:%d\n", "REDIS_REPLY_ARRAY", r->elements); 75 for (i = 0; i < r->elements; i++) { 76 printf("%d: %s\n", i, r->element[i]->str); 77 } 78 break; 79 default: 80 printf("unkonwn type:%s\n", r->type); 81 break; 82 } 83 84 /*release reply and context */ 85 freeReplyObject(r); 86 return 0; 87 } 88 89 int main() 90 { 91 const char *cmd = NULL; 92 93 /* init redis */ 94 if (redis_init()) { 95 return -1; 96 } 97 98 /* 1: SET<--->GET */ 99 printf("SET<--->GET\n"); 100 cmd = "set foo bar"; 101 redis_test(cmd); 102 cmd = "get foo"; 103 redis_test(cmd); 104 105 /* 2: SADD<--->SMEMBERS */ 106 printf("\nSADD<--->SMEMBERS\n"); 107 cmd = "SADD namelist jack lily lucy tom"; 108 redis_test(cmd); 109 cmd = "SMEMBERS namelist"; 110 redis_test(cmd); 111 112 /* 3: .....*/ 113 114 /* free redis context */ 115 redis_free(); 116 117 return 0; 118 }运行结果:SET<--->GET
set foo bar
type: 5
type:REDIS_REPLY_STATUS, reply->len:2 reply->str:OKget foo
type: 1
type:REDIS_REPLY_STRING, reply->len:3 reply->str:bar
SADD<--->SMEMBERS
SADD namelist jack lily lucy tom
type: 3
type:REDIS_REPLY_INTEGER, reply->integer:0
SMEMBERS namelist
type: 2
type:REDIS_REPLY_ARRAY, reply->elements:4
0: lucy
1: jack
2: lily
3: tom5.参考文章
http://blog.csdn.net/zouqingfang/article/details/45093355http://blog.csdn.net/oxiaoerbuyu123456/article/details/61918191https://www.cnblogs.com/wenqiang/p/5506744.htmlhttp://www.redis.net.cn/http://www.cnblogs.com/shanyou/archive/2012/01/28/2330451.html
相关文章推荐
- Redis(Windows安装方法与Java调用实例 & 配置文件参数说明 & Java使用Redis所用Jar包 & Redis与Memcached区别 & redis-cli.exe命令及示例)
- 搭建coreseek(sphinx+mmseg3)详细安装配置+php之sphinx扩展安装+php调用示例
- 搭建coreseek(sphinx+mmseg3)详细安装配置+php之sphinx扩展安装+php调用示例
- 搭建coreseek(sphinx+mmseg3)详细安装配置+php之sphinx扩展安装+php调用示例(转)
- linux下redis的安装及java调用的配置
- 搭建coreseek(sphinx+mmseg3)详细安装配置+php之sphinx扩展安装+php调用示例
- Ubuntu系统里下载安装配置redis-2.2.13.tar.gz
- centos6.5 redis 安装配置及java调用
- 搭建coreseek(sphinx+mmseg3)详细安装配置+php之sphinx扩展安装+php调用示例
- Redis笔记整理(一):Redis安装配置与数据类型操作
- 搭建coreseek(sphinx+mmseg3)详细安装配置+php之sphinx扩展安装+php调用示例
- 搭建coreseek(sphinx+mmseg3)详细安装配置+php之sphinx扩展安装+php调用示例
- 搭建coreseek(sphinx+mmseg3)详细安装配置+php之sphinx扩展安装+php调用示例
- redis安装和配置_动力节点Java学院整理
- liunx (centos )redis下载安装配置最新
- 搭建coreseek(sphinx+mmseg3)详细安装配置+php之sphinx扩展安装+php调用示例
- CentOS Linux下 redis 下载、安装、配置、主从集群配置
- redis学习系列--1.redis下载安装配置
- CentOS Linux下 redis 下载、安装、配置、主从集群配置
- 搭建coreseek(sphinx+mmseg3)详细安装配置+php之sphinx扩展安装+php调用示例