一、redis简介

  Redis是一个key-value存储系统。和 Memcached类似,但是解决了断电后数据完全丢失的情况,而且她支持更多无化的value类型,除了和string外,还支持lists(链表)、 sets(集合)和zsets(有序集合)几种数据类型。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作, 而且这些操作都是原子性的。

二、redis的安装

首先从在官网下载redis源码安装包,下载地址:https://github.com/antirez/redis.git

目前最新分支为3.2

 git clone -b 3.2  https://github.com/antirez/redis.git

下载完成之后进入redis目录,编译安装直接是

make

make install

编译安装完成直接运行

redis-server就可以启用redis服务,可以用netstat命令查看redis监听的端口(默认是6379)

redis服务起来之后我就可以进行相应的测试看数据库是否安装成功:

在命令行输入redis-cli启用redis客户端:

输入一下命令测试redis是否安装成功

127.0.0.1:> ping
PONG
127.0.0.1:> set test hello
OK
127.0.0.1:> get test
"hello"
127.0.0.1:>

更多redis命令请参考redis命令手册,这上面详细介绍了redis-cli各种命令的用法

http://doc.redisfans.com/

至此,redis数据库安装完成

三、redis c 接口库的安装

  Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类key/value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客户端,使用很方便

  在这里简单介绍一下redis的C接口库:

hiredis是redis的C接口库,使用之前我们要先下载安装hiredis,下载地址:https://github.com/redis/hiredis.git

git clone https://github.com/redis/hiredis.git

下载之后进入hiredis目录

make

make install

下面进行hredis库中几个常用函数介绍

(1)redisConnect函数

该函数用于连接redis数据库

函数原型:

  redisContext *redisConnect(const char *ip, int port);
//说明:该函数用来连接redis数据库,参数为数据库的ip地址和端口,一般redis数据库的端口为6379该函数返回一个结构体redisContext。

redisContext结构体定义如下:

 /* Context for a connection to Redis */
typedef struct redisContext {
int err; /* Error flags, 0 when there is no error */
char errstr[]; /* String representation of error when applicable */
int fd;
int flags;
char *obuf; /* Write buffer */
redisReader *reader; /* Protocol reader */ enum redisConnectionType connection_type;
struct timeval *timeout; struct {
char *host;
char *source_addr;
int port;
} tcp; struct {
char *path;
} unix_sock; } redisContext;

(2)redisCommand函数

该函数用于执行redis的命令;

函数原型:

 void *redisCommand(redisContext *c, const char *format, ...);
/*说明:该函数执行命令,就如sql数据库中的SQL语句一样,只是执行的是redis数据库中的操作命令,
第一个参数为连接数据库时返回的redisContext,
后面为可变参数列表,跟C语言printf函数类似,
返回值为void*,一般强制转换成为redisReply类型的进行进一步的处理。*/

用法:

redisReply *reply = (redisReply*)redisCommand(c,   cmd);

redisReply结构体定义如下:

 /* This is the reply object returned by redisCommand() */
typedef struct redisReply {
int type; /* REDIS_REPLY_* */
long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
size_t len; /* Length of string */
char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
} redisReply;

下面是几种redis的常见错误及返回值类型

 #define REDIS_ERR -1
#define REDIS_OK 0
#define REDIS_ERR_IO 1 /* Error in read or write */
#define REDIS_ERR_EOF 3 /* End of file */
#define REDIS_ERR_PROTOCOL 4 /* Protocol error */
#define REDIS_ERR_OOM 5 /* Out of memory */
#define REDIS_ERR_OTHER 2 /* Everything else... */ #define REDIS_REPLY_STRING 1 //返回字符串,查看str,len字段
#define REDIS_REPLY_ARRAY 2 //返回一个数组,查看elements的值(数组个数),通过element[index]的方式访问数组元素,每个数组元素是一个redisReply对象的指针
#define REDIS_REPLY_INTEGER 3 //返回整数,从integer字段获取值
#define REDIS_REPLY_NIL 4 //没有数据返回
#define REDIS_REPLY_STATUS 5 //表示状态,内容通过str字段查看,字符串长度是len字段
#define REDIS_REPLY_ERROR 6 //表示出错,查看出错信息,如上的str,len字段

(3)redisRelpyObject函数

还函数用于释放redisCommand返回值redisReply所占用的内存

 /* Free a reply object */
void freeReplyObject(void *reply)
//该函数用于回收释放redisCommand返回值redisReply所占用的内存

(4)redisFree函数

该函数用于释放一个redis数据库的连接

 void redisFree(redisContext *c)
//用于释放redisConnect产生的连接

下面写一个简单的hredis c接口的redis测试程序:

 #include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <hiredis/hiredis.h> //redis C接口库 #define REDIS_HOST "127.0.0.1"
#define REDIS_PORT 6379 redisContext *g_ctx = NULL; int redis_init()
{
redisContext *c = NULL;
c = redisConnect(REDIS_HOST, REDIS_PORT);
if (NULL == c || c->err) {
if(c) {
printf("Redis [%s:%d], Error:[%s]\n", REDIS_HOST, REDIS_PORT, c->errstr);
redisFree(c);
} else {
printf("Redis [%s:%d] failure\n", REDIS_HOST, REDIS_PORT);
}
return -;
}
g_ctx = c; return ;
} void redis_free()
{
if (g_ctx) {
redisFree(g_ctx);
}
g_ctx = NULL;
} int redis_test(const char *cmd)
{
int i = ;
redisReply *r = NULL;
if (NULL == cmd) {
return -;
} printf("%s\n", cmd); r = (redisReply *)redisCommand(g_ctx, cmd);
if (NULL == r) {
printf("%s, Error[%d:%s]", g_ctx->err, g_ctx->errstr);
return -;
} printf("type: %d\n", r->type);
switch(r->type) {
case REDIS_REPLY_STATUS:
printf("type:%s, reply->len:%d reply->str:%s\n", "REDIS_REPLY_STATUS", r->len, r->str);
break;
case REDIS_REPLY_ERROR:
printf("type:%s, reply->len:%d reply->str:%s\n", "REDIS_REPLY_ERROR", r->len, r->str);
break;
case REDIS_REPLY_INTEGER:
printf("type:%s, reply->integer:%lld\n", "REDIS_REPLY_INTEGER", r->integer);
break;
case REDIS_REPLY_NIL:
printf("type:%s, no data\n", "REDIS_REPLY_NIL");
break;
case REDIS_REPLY_STRING:
printf("type:%s, reply->len:%d reply->str:%s\n", "REDIS_REPLY_STRING", r->len, r->str);
break;
case REDIS_REPLY_ARRAY:
printf("type:%s, reply->elements:%d\n", "REDIS_REPLY_ARRAY", r->elements);
for (i = ; i < r->elements; i++) {
printf("%d: %s\n", i, r->element[i]->str);
}
break;
default:
printf("unkonwn type:%s\n", r->type);
break;
} /*release reply and context */
freeReplyObject(r);
return ;
} int main()
{
const char *cmd = NULL; /* init redis */
if (redis_init()) {
return -;
} /* 1: SET<--->GET */
printf("SET<--->GET\n");
cmd = "set foo bar";
redis_test(cmd);
cmd = "get foo";
redis_test(cmd); /* 2: SADD<--->SMEMBERS */
printf("\nSADD<--->SMEMBERS\n");
cmd = "SADD namelist jack lily lucy tom";
redis_test(cmd);
cmd = "SMEMBERS namelist";
redis_test(cmd); /* 3: .....*/ /* free redis context */
redis_free(); return ;
}

运行结果:

SET<--->GET
set foo bar
type:
type:REDIS_REPLY_STATUS, reply->len: reply->str:OK
get foo
type:
type:REDIS_REPLY_STRING, reply->len: reply->str:bar SADD<--->SMEMBERS
SADD namelist jack lily lucy tom
type:
type:REDIS_REPLY_INTEGER, reply->integer:
SMEMBERS namelist
type:
type:REDIS_REPLY_ARRAY, reply->elements:
: lucy
: jack
: lily
: tom

linux下redis数据库的简单使用的更多相关文章

  1. Redis、Redis+sentinel安装(Ubuntu 14.04下Redis安装及简单测试)

    Ubuntu下Redis安装两种安装方式: 1.apt-get方式 步骤: 以root权限登录,切换到/usr目录下. 接下来输入命令,apt-get install redis-server,如图: ...

  2. 莫小安 Linux下Redis的安装与配置

    转载自--Linux下Redis的安装与配置 redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcached类似,但很大程度补偿了 memcached的不足,它 ...

  3. Linux下Redis服务器安装配置

    说明:操作系统:CentOS1.安装编译工具yum install wget  make gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel ...

  4. windows下和linux下 Redis 安装

    Redis 是一个高性能的key-value数据库, 使用内存作为主存储,数据访问速度非常快,当然它也提供了两种机制支持数据持久化存储.比较遗憾的是,Redis项目不直接支持Windows,Windo ...

  5. linux下mysql数据库的操作

    本文主要针对linux下mysql数据库的安装,以及数据库的创建和简单的数据库操作进行说明. ①.Mysql数据库的安装: 数据库的安装分为源码安装和rpm安装. 当然对于老手来说需要进行一些自定义的 ...

  6. linux下redis的安装及配置启动

    linux下redis的安装及配置启动 标签: redisnosql 2014-10-24 14:04 19732人阅读 评论(0) 收藏 举报  分类: 数据与性能(41)  wget http:/ ...

  7. 记录--linux下mysql数据库问题

    本次主要记录一下linux下mysql数据库的一些问题,也是之前经常用到的知识,这里简单总结一些问题,方便自己以后的回顾.原来一直使用的是阿里云的RDS数据库mysql版,主要是因为上次阿里云做活动可 ...

  8. linux下Redis与phpredis扩展安装

    ++++++++++++++++++++++++++++++++++++++++++++++linux下Redis与phpredis扩展安装++++++++++++++++++++++++++++++ ...

  9. Linux下链接数据库图形化工具

    (一).Linux环境下mysql的安装.SQL操作 Linux下安装MySQL (rmp --help) 基本步骤:上传软件->检查当前Linux环境是否已经安装,如发现系统自带的,先卸载-& ...

随机推荐

  1. 供应商信息全SQL

    SELECT hou.name, pv.vendor_name 供应商, pv.party_id, pvs.vendor_site_id, pvs.terms_id, pv.vendor_name_a ...

  2. getJSONObject与optJSONObject的区别,结合源码分析

    *json解析常见问题: getJSONObject与optJSONObject的区别,下面结合源码和案例来分析当我们使用这两周方法来解析数据时,哪种比较好. 源码分析: //使用getJSONObj ...

  3. 使用Multiplayer Networking做一个简单的多人游戏例子-3/3(Unity3D开发之二十七)

    使用Multiplayer Networking做一个简单的多人游戏例子-1/3 使用Multiplayer Networking做一个简单的多人游戏例子-2/3 使用Multiplayer Netw ...

  4. #pragma comment(转)

    此文转自微软MSDN.注意这是在Windows上才有的,Linux上可没有. #pragma comment( comment-type [,"commentstring"] ) ...

  5. Libevent库学习笔记

    Libevent是一个事件触发的网络库,适用于windows.linux.bsd等多种平台,Libevent在底层select.pool.kqueue和epoll等机制基础上,封装出一致的事件接口.可 ...

  6. linux下由带-开头文件想到的

    如果要删除文件-aaa,使用rm -aaa是不行的,rm会认为-后面的是参数.2种办法: 1 带明确路径指示 rm ./-aaa 2 使用 -- :rm -- -aaa 因为命令如果发现参数中有--, ...

  7. Docker 基础技术之 Linux cgroups 详解

    PS:欢迎大家关注我的公众号:aCloudDeveloper,专注技术分享,努力打造干货分享平台,二维码在文末可以扫,谢谢大家. 推荐大家到公众号阅读,那里阅读体验更好,也沉淀了很多篇干货. 前面两篇 ...

  8. WebService学习--(二)webservice相关介绍

    一.WebService是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨平台的规范(抽象) 3. 多个跨平台.跨语言的应用间通信整合的方案(实际 ...

  9. 技术大牛是如何拿到国内IT巨头offer的?

    关键字:技术大牛是如何拿到国内IT巨头offer的?  不是技术牛人,如何拿到国内IT巨头的Offer  不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些牛人, ...

  10. Python字符串全解

    1.字符串大小写转换 def strChange(): str = "niuXinLong@163.com" print("原字符串:" + str) prin ...