Redis学习之路(007)- Redis学习手册(实例代码)
在之前的博客中已经非常详细的介绍了Redis的各种操作命令、运行机制和服务器初始化参数配置。本篇博客是该系列博客中的最后一篇,在这里将给出基于Redis客户端组件访问并操作Redis服务器的代码示例。然而需要说明的是,由于Redis官方并未提供基于C接口的Windows平台客户端,因此下面的示例仅可运行于Linux/Unix平台。但是对于使用其它编程语言的开发者而言,如C#和Java,Redis则提供了针对这些语言的客户端组件,通过该方式,同样可以达到基于Windows平台与Redis服务器进行各种交互的目的。
该篇博客中使用的客户端来自于Redis官方网站,是Redis推荐的基于C接口的客户端组件,见如下链接:
https://github.com/antirez/hiredis
在下面的代码示例中,将给出两种最为常用的Redis命令操作方式,既普通调用方式和基于管线的调用方式。
注:在阅读代码时请留意注释。
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <hiredis.h> void doTest()
{
int timeout = ;
struct timeval tv;
tv.tv_sec = timeout / ;
tv.tv_usec = timeout * ;
//以带有超时的方式链接Redis服务器,同时获取与Redis连接的上下文对象。
//该对象将用于其后所有与Redis操作的函数。
redisContext* c = redisConnectWithTimeout("192.168.149.137",,tv);
if (c->err) {
redisFree(c);
return;
}
const char* command1 = "set stest1 value1";
redisReply* r = (redisReply*)redisCommand(c,command1);
//需要注意的是,如果返回的对象是NULL,则表示客户端和服务器之间出现严重错误,必须重新链接。
//这里只是举例说明,简便起见,后面的命令就不再做这样的判断了。
if (NULL == r) {
redisFree(c);
return;
}
//不同的Redis命令返回的数据类型不同,在获取之前需要先判断它的实际类型。
//至于各种命令的返回值信息,可以参考Redis的官方文档,或者查看该系列博客的前几篇
//有关Redis各种数据类型的博客。:)
//字符串类型的set命令的返回值的类型是REDIS_REPLY_STATUS,然后只有当返回信息是"OK"
//时,才表示该命令执行成功。后面的例子以此类推,就不再过多赘述了。
if (!(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK") == )) {
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);
//这里需要先说明一下,由于stest2键并不存在,因此Redis会返回空结果,这里只是为了演示。
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); const char* command5 = "mget stest1 stest2";
r = (redisReply*)redisCommand(c,command5);
//不论stest2存在与否,Redis都会给出结果,只是第二个值为nil。
//由于有多个值返回,因为返回应答的类型是数组类型。
if (r->type != REDIS_REPLY_ARRAY) {
printf("Failed to execute command[%s].\n",command5);
freeReplyObject(r);
redisFree(c);
//r->elements表示子元素的数量,不管请求的key是否存在,该值都等于请求是键的数量。
assert( == r->elements);
return;
}
for (int i = ; i < r->elements; ++i) {
redisReply* childReply = r->element[i];
//之前已经介绍过,get命令返回的数据类型是string。
//对于不存在key的返回值,其类型为REDIS_REPLY_NIL。
if (childReply->type == REDIS_REPLY_STRING)
printf("The value is %s.\n",childReply->str);
}
//对于每一个子应答,无需使用者单独释放,只需释放最外部的redisReply即可。
freeReplyObject(r);
printf("Succeed to execute command[%s].\n",command5); printf("Begin to test pipeline.\n");
//该命令只是将待发送的命令写入到上下文对象的输出缓冲区中,直到调用后面的
//redisGetReply命令才会批量将缓冲区中的命令写出到Redis服务器。这样可以
//有效的减少客户端与服务器之间的同步等候时间,以及网络IO引起的延迟。
//至于管线的具体性能优势,可以考虑该系列博客中的管线主题。
if (REDIS_OK != redisAppendCommand(c,command1)
|| REDIS_OK != redisAppendCommand(c,command2)
|| REDIS_OK != redisAppendCommand(c,command3)
|| REDIS_OK != redisAppendCommand(c,command4)
|| REDIS_OK != redisAppendCommand(c,command5)) {
redisFree(c);
return;
} redisReply* reply = NULL;
//对pipeline返回结果的处理方式,和前面代码的处理方式完全一直,这里就不再重复给出了。
if (REDIS_OK != redisGetReply(c,(void**)&reply)) {
printf("Failed to execute command[%s] with Pipeline.\n",command1);
freeReplyObject(reply);
redisFree(c);
}
freeReplyObject(reply);
printf("Succeed to execute command[%s] with Pipeline.\n",command1); if (REDIS_OK != redisGetReply(c,(void**)&reply)) {
printf("Failed to execute command[%s] with Pipeline.\n",command2);
freeReplyObject(reply);
redisFree(c);
}
freeReplyObject(reply);
printf("Succeed to execute command[%s] with Pipeline.\n",command2); if (REDIS_OK != redisGetReply(c,(void**)&reply)) {
printf("Failed to execute command[%s] with Pipeline.\n",command3);
freeReplyObject(reply);
redisFree(c);
}
freeReplyObject(reply);
printf("Succeed to execute command[%s] with Pipeline.\n",command3); if (REDIS_OK != redisGetReply(c,(void**)&reply)) {
printf("Failed to execute command[%s] with Pipeline.\n",command4);
freeReplyObject(reply);
redisFree(c);
}
freeReplyObject(reply);
printf("Succeed to execute command[%s] with Pipeline.\n",command4); if (REDIS_OK != redisGetReply(c,(void**)&reply)) {
printf("Failed to execute command[%s] with Pipeline.\n",command5);
freeReplyObject(reply);
redisFree(c);
}
freeReplyObject(reply);
printf("Succeed to execute command[%s] with Pipeline.\n",command5);
//由于所有通过pipeline提交的命令结果均已为返回,如果此时继续调用redisGetReply,
//将会导致该函数阻塞并挂起当前线程,直到有新的通过管线提交的命令结果返回。
//最后不要忘记在退出前释放当前连接的上下文对象。
redisFree(c);
return;
} int main()
{
doTest();
return ;
} //输出结果如下:
//Succeed to execute command[set stest1 value1].
//The length of 'stest1' is 6.
//Succeed to execute command[strlen stest1].
//The value of 'stest1' is value1.
//Succeed to execute command[get stest1].
//Succeed to execute command[get stest2].
//The value is value1.
//Succeed to execute command[mget stest1 stest2].
//Begin to test pipeline.
//Succeed to execute command[set stest1 value1] with Pipeline.
//Succeed to execute command[strlen stest1] with Pipeline.
//Succeed to execute command[get stest1] with Pipeline.
//Succeed to execute command[get stest2] with Pipeline.
//Succeed to execute command[mget stest1 stest2] with Pipeline.
该示例代码已经调试通过,同时也用Valgrind进行了运行时检查,不存在任何内存泄露,特此声明。
Redis学习之路(007)- Redis学习手册(实例代码)的更多相关文章
- Redis 学习之路 (010) - redis命令手册
Redis 键(key) 命令 命令 描述 Redis DEL 命令 该命令用于在 key 存在是删除 key. Redis Dump 命令 序列化给定 key ,并返回被序列化的值. Redis E ...
- Redis学习手册(实例代码)
在之前的博客中已经非常详细的介绍了Redis的各种操作命令.运行机制和服务器初始化参数配置.本篇博客是该系列博客中的最后一篇,在这里将给出基于Redis客户端组件访问并操作Redis服务器的代码示例. ...
- Redis 学习之路 (011) - redis 多数据库
一台服务器上都快开启200个redis实例了,看着就崩溃了.这么做无非就是想让不同类型的数据属于不同的应用程序而彼此分开. 那么,redis有没有什么方法使不同的应用程序数据彼此分开同时又存储在相同的 ...
- Android开发学习之路-让注解帮你简化代码,彻底抛弃findViewById
本文主要是记录注解的使用的学习笔记,如有错误请提出. 在通常的情况下,我们在Activity中有一个View,我们要获得这个View的实例是要通过findViewById这个方法,然后这个方法返回的是 ...
- python 操作exls学习之路1-openpyxl库学习
这篇要讲到的就是如何利用Python与openpyxl结合来处理xlsx表格数据.Python处理表格的库有很多,这里的openpyxl就是其中之一,但是它是处理excel2007/2010的格式,也 ...
- Vue学习之路8-v-on指令学习简单事件绑定之属性
前言 上一篇文章以v-on指令绑定click事件为例介绍了v-on指令的使用方法,本文介绍一下v-on绑定事件的一些属性的使用方法. v-on绑定指令属性 .stop属性 阻止单击事件继续向上传播(简 ...
- Vue学习之路7-v-on指令学习之简单事件绑定
前言 在JavaScript中任何一个DOM元素都有其自身存在的事件对象,事件对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置和鼠标按钮的状态等.事件通常与函数结合使用,函数不 ...
- Android开发学习之路-回调机制学习笔记
不知道是我学Java的时候没有认真听还是怎么的,曾经一直不知道什么是“回调”,它有什么用,百度一大堆,都太复杂看不明白(好吧是我笨),所以想把自己理解的分享给其他看到的人,大家都真正认识一下这个重要的 ...
- SQLite学习手册(实例代码<一>)
一.获取表的Schema信息: 1). 动态创建表. 2). 根据sqlite3提供的API,获取表字段的信息,如字段数量以及每个字段的类型. 3). 删除该表. ...
随机推荐
- 【Scala】Scala-None-null引发的血案
Scala-None-null引发的血案 Overview - Spark 2.2.0 Documentation Spark Streaming - Spark 2.2.0 Documentatio ...
- 最全的spark基础知识解答
原文:http://www.36dsj.com/archives/61155 一. Spark基础知识 1.Spark是什么? UCBerkeley AMPlab所开源的类HadoopMapReduc ...
- 文档的js
/* 主站,子频道,定向站点共用 */ (function() { scrollToAnchor(); toggleSearchForm(); scrollTop(); initScrollBar() ...
- redis 中 set 和 hset 有什么不同,什么时候使用 hset 什么时候使用set?
转载:https://blog.csdn.net/wab719591157/article/details/73379844 redis 中存数据时,到底什么时候用 hset 相比于 set 存数据 ...
- angular5中使用jsonp请求页面
说多了,都是眼泪,折腾了很久,各种百度,查到的例子,全都报错,可能是因为我的angular的版本太高,向下都不兼容? 我的angular版本为最新的5.2.11: 下面是正确的jsonp请求的方法 1 ...
- 在Linux下如何限制命令执行的时间?
在Linux下如何限制命令执行的时间?两种解决方法,如下: 1: Linux命令——timeout 运行指定的命令,如果在指定时间后仍在运行,则杀死该进程.用来控制程序运行的时间. 2: comman ...
- postman发送post请求
在地址栏里输入请求url:http://127.0.0.1:8081/getuser 选择“POST”方式, 点击''body", ''form-data", 添加key:user ...
- 自己定义构造方法和description方法
本文文件夹 知识回想 一.自己定义构造方法 二.description方法 说明:这个Objective-C专题,是学习iOS开发的前奏,也为了让有面向对象语言开发经验的程序猿,可以高速上手Objec ...
- sublime text 3中文乱码问题解决的方法
一.首先要确保本机sublime已经有安装包管理器,假设没有.安装方法:http://blog.chinaunix.net/uid-12014716-id-4269991.html 文中的第一步:安装 ...
- mktime 和 TZ
mktime底层使用__tz_convert,可能会比较慢 http://blog.csdn.net/aquester/article/details/54669264 http://blog.csd ...