search
HomeDatabaseMysql Tutoriallinux下redis的安装,使用

redis:1.直接下载: wget http://download.redis.io/releases/redis-2.8.9.tar.gz2.make编译3.设置配置文件redis.config,在src目录去执行启动 : ./redis-server ../redis.config 后面可以加上配置文件实现让服务器后台运行,成为守护进程4.登陆客户端,同样

 redis: 1.直接下载: wget http://download.redis.io/releases/redis-2.8.9.tar.gz 2.make编译 3.设置配置文件redis.config,在src目录去执行启动 : ./redis-server ../redis.config 后面可以加上配置文件实现让服务器后台运行,成为守护进程 4.登陆客户端,同样在src目录 ./redis-cli 也可以telnet ip port不过输出键值对的时候有个$2 5.写程序的时候需要使用redis的api,方法,在hiredis目录下生产一个静态库文件libhiredis.a,把头文件放进/usr/include,静态库文件放进/lib,编译文件的时候加上-lhiredis,就可以使用了,动态库也是类似的调用方法 c语言连接插入redis实例: http://blog.csdn.net/hj19870806/article/details/8724907 http://yaocoder.blog.51cto.com/2668309/1297031
#include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
 #include <stdarg.h>
 #include <string.h>
 #include <assert.h>
 #include <hiredis/hiredis.h>
 
 void doTest()
 {
     int timeout = 10000;
     struct timeval tv;
     tv.tv_sec = timeout / 1000;
     tv.tv_usec = timeout * 1000;
     //以带有超时的方式链接Redis服务器,同时获取与Redis连接的上下文对象。
     //该对象将用于其后所有与Redis操作的函数。
     redisContext* c = redisConnect((char*)"127.0.0.1", 6379);
     if (c->err) {
         redisFree(c);
         return;
     }
     const char* command1 = "set stest1 value9";
     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") == 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 &#39;stest1&#39; 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 &#39;stest1&#39; 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(2 == r->elements);
         return;
     }
     int i;
      for (i = 0; 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;
     }
 */

    redisAppendCommand(c,command1);
    redisAppendCommand(c,command2);
    redisAppendCommand(c,command3);
    redisAppendCommand(c,command4);
    redisAppendCommand(c,command5);
     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 0;
 }


memcache: http://php.net/manual/zh/book.memcache.php http://docs.linuxtone.org/ebooks/NOSQL/memcached/memcached--%E9%BB%91%E5%A4%9C%E8%B7%AF%E4%BA%BA.pdf
gcc源码编译: http://www.cnblogs.com/codemood/archive/2013/06/01/3113200.html 下载地址:http://ftp.gnu.org/ GNU的ftp服务器下载
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
什么是linux设备节点什么是linux设备节点Apr 18, 2022 pm 08:10 PM

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

Linux中open和fopen的区别有哪些Linux中open和fopen的区别有哪些Apr 29, 2022 pm 06:57 PM

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

linux中什么叫端口映射linux中什么叫端口映射May 09, 2022 pm 01:49 PM

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

什么是linux交叉编译什么是linux交叉编译Apr 29, 2022 pm 06:47 PM

在linux中,交叉编译是指在一个平台上生成另一个平台上的可执行代码,即编译源代码的平台和执行源代码编译后程序的平台是两个不同的平台。使用交叉编译的原因:1、目标系统没有能力在其上进行本地编译;2、有能力进行源代码编译的平台与目标平台不同。

linux中eof是什么linux中eof是什么May 07, 2022 pm 04:26 PM

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

linux怎么判断pcre是否安装linux怎么判断pcre是否安装May 09, 2022 pm 04:14 PM

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux怎么查询mac地址linux怎么查询mac地址Apr 24, 2022 pm 08:01 PM

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

linux中rpc是什么意思linux中rpc是什么意思May 07, 2022 pm 04:48 PM

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),