zoukankan      html  css  js  c++  java
  • Redis:安装、配置、操作和简单代码实例(C语言Client端)

    Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis的开发工作由VMware主持。

      如何安装Redis?

      Redis的官方下载站是http://redis.io/download,可以去上面下载最新的安装程序下来,我写此文章时的的稳定版本是2.2.12。

    如何安装Redis?

      怎么安装 Redis数据库呢?下面将介绍Linux版本的安装方法:

      步骤一: 下载Redis

      下载安装包:wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz

      [root@localhost 4setup]# wget http://redis.googlecode.com/files/redis-2.2.12.tar.gz

      
    --19:06:56-- http://redis.googlecode.com/files/redis-2.2.12.tar.gz

      正在解析主机 redis.googlecode.com... 
    74.125.71.82

      Connecting to redis.googlecode.com
    |74.125.71.82|:80... 已连接。

      已发出 HTTP 请求,正在等待回应... 
    200 OK

      长度:
    455240 (445K) [application/x-gzip]

      Saving to: `redis
    -2.2.12.tar.gz'

      
    100%[==========================================>]455,24034.8K/sin 13s

      
    19:07:16 (34.8 KB/s) - `redis-2.2.12.tar.gz' saved [455240/455240]

      [root@localhost 4setup]#

      步骤二: 编译源程序

      [root@localhost 4setup]# ll

      总计 
    29168

      
    -rw-r--r--1 root root 4552402011-07-22 redis-2.2.12.tar.gz

      [root@localhost 4setup]# tar xzf redis
    -2.2.12.tar.gz

      [root@localhost 4setup]# cd redis
    -2.2.12

      [root@localhost redis
    -2.2.12]# make

      cd src 
    && make all

      make[
    1]: Entering directory `/root/4setup/redis-2.2.12/src'

      步骤三: 启动Redis服务

      src/redis-server

      [root@localhost redis
    -2.2.12]# src/redis-server

      [
    624605 Aug 19:17:22 # Warning: no config file specified, using the default config. In order to specify a config file use'redis-server /path/to/redis.conf'

      [
    624605 Aug 19:17:22* Server started, Redis version 2.2.12

      [
    624605 Aug 19:17:22 # WARNING overcommit_memory issetto0! Background save may fail under low memory condition.Tofix this issue add'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

      [
    624605 Aug 19:17:22* The server isnow readyto accept connectionson port 6379

      [
    624605 Aug 19:17:22-0 clients connected (0 slaves),539544 bytes in use

      Redis 服务端的默认连接端口是 6379。

      步骤四: 将Redis作为 Linux 服务随机启动

      vi /etc/rc.local, 使用vi编辑器打开随机启动配置文件,并在其中加入下面一行代码。

    /root/4setup/redis-2.2.12/src/redis-server
     

      步骤五: 客户端连接验证

      新打开一个Session输入:src/redis-cli,如果出现下面提示,那么您就可以开始Redis之旅了。

      [root@localhost redis-2.2.12]# src/redis-cli

      redis 
    127.0.0.1:6379>

      步骤六: 查看Redis日志

      查看服务器端session,即可对Redis的运行状况进行查看或分析了。

      [6246]05 Aug19:24:33-0 clients connected (0 slaves),539544 bytes in use

      [
    624605 Aug 19:24:37- Accepted 127.0.0.1:51381

      [
    624605 Aug 19:24:38-1 clients connected (0 slaves),547372 bytes in use

      以上的几个步骤就OK了!!这样一个简单的Redis数据库就可以畅通无阻地运行起来了。

      步骤七: 停止Redis实例

      最简单的方法是在启动实例的session中,直接使用Control-C来将实例停止。

      我们还可以用客户端来停止服务,如可以用shutdown来停止Redis实例, 具体如下:

      [root@localhost redis-2.2.12]# src/redis-cli shutdown

      如何配置Redis?

      如果是一个专业的DBA,那么实例启动时会加很多的参数以便使系统运行的非常稳定,这样就可能会在启动时在Redis后面加一个参数,以指定配置文件的路径,就象mysql一样的读取启动配置文件的方式来启动数据库。源码编译完成后,在redis-2.2.12目录下有一个redis.conf文件,这个文件即是Redis的配置文件,用配置文件来启动Redis的方法如下:

      [root@localhost redis-2.2.12]# src/redis-server redis.conf

      [
    635305 Aug 19:36:45* Server started, Redis version 2.2.12

      [
    635305 Aug 19:36:45 # WARNING overcommit_memory issetto0! Background save may fail under low memory condition.Tofix this issue add'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

      [
    635305 Aug 19:36:45* The server isnow readyto accept connectionson port 6379

      [
    635305 Aug 19:36:45-0 clients connected (0 slaves),539540 bytes in use

      Redis支持很多的参数,但都有默认值。

      ●daemonize:

      默认情况下,redis不是在后台运行的,如果需要在后台运行,把该项的值更改为yes。

      ●pidfile

      当Redis在后台运行的时候,Redis默认会把pid文件放在/var/run/redis.pid,你可以配置到其他地址。当运行多个redis服务时,需要指定不同的pid文件和端口。

      ●bind

      指定Redis只接收来自于该IP地址的请求,如果不进行设置,那么将处理所有请求,在生产环境中最好设置该项。

      ●port

      监听端口,默认为6379。

      ●timeout

      设置客户端连接时的超时时间,单位为秒。当客户端在这段时间内没有发出任何指令,那么关闭该连接。

      ●loglevel

      log等级分为4级,debug, verbose, notice, 和warning。生产环境下一般开启notice。

      ●logfile

      配置log文件地址,默认使用标准输出,即打印在命令行终端的窗口上。

      ●databases

      设置数据库的个数,可以使用SELECT 命令来切换数据库。默认使用的数据库是0。

      ●save

      设置Redis进行数据库镜像的频率。

      if(在60秒之内有10000个keys发生变化时){

      进行镜像备份

      }else if(在300秒之内有10个keys发生了变化){

      进行镜像备份

      }else if(在900秒之内有1个keys发生了变化){

      进行镜像备份

      }


      ●rdbcompression

      在进行镜像备份时,是否进行压缩。

      ●dbfilename

      镜像备份文件的文件名。

      ●dir

      数据库镜像备份的文件放置的路径。这里的路径跟文件名要分开配置是因为Redis在进行备份时,先会将当前数据库的状态写入到一个临时文件中,等备份完成时,再把该该临时文件替换为上面所指定的文件,而这里的临时文件和上面所配置的备份文件都会放在这个指定的路径当中。

      ●slaveof

      设置该数据库为其他数据库的从数据库。

      ●masterauth

      当主数据库连接需要密码验证时,在这里指定。

      ●requirepass

      设置客户端连接后进行任何其他指定前需要使用的密码。警告:因为redis速度相当快,所以在一台比较好的服务器下,一个外部的用户可以在一秒钟进行150K次的密码尝试,这意味着你需要指定非常非常强大的密码来防止暴力破解。

      ●maxclients

      限制同时连接的客户数量。当连接数超过这个值时,redis将不再接收其他连接请求,客户端尝试连接时将收到error信息。

      ●maxmemory

      设置redis能够使用的最大内存。当内存满了的时候,如果还接收到set命令,redis将先尝试剔除设置过expire信息的key,而不管该key的过期时间还没有到达。在删除时,将按照过期时间进行删除,最早将要被过期的key将最先被删除。如果带有expire信息的key都删光了,那么将返回错误。这样,redis将不再接收写请求,只接收get请求。maxmemory的设置比较适合于把redis当作于类似memcached的缓存来使用。

      ●appendonly

      默认情况下,redis会在后台异步的把数据库镜像备份到磁盘,但是该备份是非常耗时的,而且备份也不能很频繁,如果发生诸如拉闸限电、拔插头等状况,那么将造成比较大范围的数据丢失。所以redis提供了另外一种更加高效的数据库备份及灾难恢复方式。开启append only模式之后,redis会把所接收到的每一次写操作请求都追加到appendonly.aof文件中,当redis重新启动时,会从该文件恢复出之前的状态。但是这样会造成appendonly.aof文件过大,所以redis还支持了BGREWRITEAOF指令,对appendonly.aof进行重新整理。所以我认为推荐生产环境下的做法为关闭镜像,开启appendonly.aof,同时可以选择在访问较少的时间每天对appendonly.aof进行重写一次。

      ●appendfsync

      设置对appendonly.aof文件进行同步的频率。always表示每次有写操作都进行同步,everysec表示对写操作进行累积,每秒同步一次。这个需要根据实际业务场景进行配置。

      ●vm-enabled

      是否开启虚拟内存支持。因为redis是一个内存数据库,而且当内存满的时候,无法接收新的写请求,所以在redis 2.0中,提供了虚拟内存的支持。但是需要注意的是,redis中,所有的key都会放在内存中,在内存不够时,只会把value值放入交换区。这样保证了虽然使用虚拟内存,但性能基本不受影响,同时,你需要注意的是你要把vm-max-memory设置到足够来放下你的所有的key。

      ●vm-swap-file

      设置虚拟内存的交换文件路径。

      ●vm-max-memory

      这里设置开启虚拟内存之后,redis将使用的最大物理内存的大小。默认为0,redis将把他所有的能放到交换文件的都放到交换文件中,以尽量少的使用物理内存。在生产环境下,需要根据实际情况设置该值,最好不要使用默认的0。

      ●vm-page-size

      设置虚拟内存的页大小,如果你的value值比较大,比如说你要在value中放置博客、新闻之类的所有文章内容,就设大一点,如果要放置的都是很小的内容,那就设小一点。

      ●vm-pages

      设置交换文件的总的page数量,需要注意的是,page table信息会放在物理内存中,每8个page就会占据RAM中的1个byte。总的虚拟内存大小 = vm-page-size * vm-pages。

      ●vm-max-threads

      设置VM IO同时使用的线程数量。因为在进行内存交换时,对数据有编码和解码的过程,所以尽管IO设备在硬件上本上不能支持很多的并发读写,但是还是如果你所保存的vlaue值比较大,将该值设大一些,还是能够提升性能的。

      ●glueoutputbuf

      把小的输出缓存放在一起,以便能够在一个TCP packet中为客户端发送多个响应,具体原理和真实效果我不是很清楚。所以根据注释,你不是很确定的时候就设置成yes。

      ●hash-max-zipmap-entries

      在redis 2.0中引入了hash数据结构。当hash中包含超过指定元素个数并且最大的元素没有超过临界时,hash将以一种特殊的编码方式(大大减少内存使用)来存储,这里可以设置这两个临界值。

      ●activerehashing

      开启之后,redis将在每100毫秒时使用1毫秒的CPU时间来对redis的hash表进行重新hash,可以降低内存的使用。当你的使用场景中,有非常严格的实时性需要,不能够接受Redis时不时的对请求有2毫秒的延迟的话,把这项配置为no。如果没有这么严格的实时性要求,可以设置为yes,以便能够尽可能快的释放内存。


      操作Redis数据库

      下面我们来简单的操作一下数据库。

      1、插入数据

      redis 127.0.0.1:6379>set name wwl

      OK

      设置一个key-value对。

      2、查询数据

      redis 127.0.0.1:6379>get name

      
    "wwl"

      取出key所对应的value。

      3、删除键值

      redis 127.0.0.1:6379> del name

      删除这个key及对应的value。

      4、验证键是否存在

      redis 127.0.0.1:6379>exists name

      (
    integer)0

      其中0,代表此key不存在;1代表存在。  

    redis客户端(C语言)

    上面介绍了redis 安装和简单使用操作,下面,我们来介绍一下redis client端的编写。

    [root@localhost redis-2.2.12]# cd deps/hiredis/
    [root@localhost hiredis]# make 
    cc -c -std=c99 -pedantic -O3 -fPIC -Wall -W -Wwrite-strings    -g -ggdb  net.c
    cc -c -std=c99 -pedantic -O3 -fPIC -Wall -W -Wwrite-strings    -g -ggdb  hiredis.c
    cc -c -std=c99 -pedantic -O3 -fPIC -Wall -W -Wwrite-strings    -g -ggdb  sds.c
    cc -c -std=c99 -pedantic -O3 -fPIC -Wall -W -Wwrite-strings    -g -ggdb  async.c
    gcc -shared -Wl,-soname,libhiredis.so -o libhiredis.so net.o hiredis.o sds.o async.o
    cc -c -std=c99 -pedantic -O3 -fPIC -Wall -W -Wwrite-strings    -g -ggdb  example.c
    cc -o hiredis-example -std=c99 -pedantic -O3 -fPIC -Wall -W -Wwrite-strings   -lm -pthread -g -ggdb -L. -Wl,-rpath,. -lhiredis example.o
    cc -c -std=c99 -pedantic -O3 -fPIC -Wall -W -Wwrite-strings    -g -ggdb  test.c
    cc -o hiredis-test -std=c99 -pedantic -O3 -fPIC -Wall -W -Wwrite-strings   -lm -pthread -g -ggdb -L. -Wl,-rpath,. -lhiredis test.o
    [root@localhost hiredis]# make install 
     ar rcs libhiredis.a net.o hiredis.o sds.o async.o
    mkdir -p /usr/local/include/hiredis /usr/local/lib
    cp -a hiredis.h async.h adapters /usr/local/include/hiredis
    cp -a libhiredis.so libhiredis.a /usr/local/lib

    这种方式安装的安装库,没有到达默认目录,需要我们:
    [root@localhost hiredis] cp libhiredis.so /usr/lib64 /usr/lib  (若是32系统只需要运行 cp libhiredis.so /usr/lib
    [root@localhost hiredis]# gcc test.c -o test -lhiredis
    [root@loaclhost hiredis]# ./test 
    #01 Format command without interpolation: PASSED
    #02 Format command with %s string interpolation: PASSED
    #03 Format command with %s and an empty string: PASSED
    #04 Format command with %b string interpolation: PASSED
    #05 Format command with %b and an empty string: PASSED
    #06 Format command with literal %: PASSED
    #07 Format command with printf-delegation (long long): PASSED
    #08 Format command with printf-delegation (float): PASSED
    #09 Format command with printf-delegation and extra interpolation: PASSED
    #10 Format command with wrong printf format and extra interpolation: PASSED
    #11 Format command by passing argc/argv without lengths: PASSED
    #12 Format command by passing argc/argv with lengths: PASSED
    #13 Returns error when host cannot be resolved: FAILED
    #14 Returns error when the port is not open: PASSED
    #15 Is able to deliver commands: PASSED
    #16 Is a able to send commands verbatim: PASSED
    #17 %s String interpolation works: PASSED
    #18 %b String interpolation works: PASSED
    #19 Binary reply length is correct: PASSED
    #20 Can parse nil replies: PASSED
    #21 Can parse integer replies: PASSED
    #22 Can parse multi bulk replies: PASSED
    #23 Can handle nested multi bulk replies: PASSED
    #24 Returns I/O error when the connection is lost: PASSED
    #25 Error handling in reply parser: PASSED
    #26 Memory cleanup in reply parser: PASSED
    #27 Set error on nested multi bulks with depth > 1: FAILED
    #28 Works with NULL functions for reply: PASSED
    #29 Works when a single newline ( ) covers two calls to feed: PASSED
    #30 Throughput:
        (1000x PING: 0.04s)
        (1000x LRANGE with 500 elements: 0.16s)
    *** 2 TESTS FAILED ***

    不需要关注上面的两个FAILED,当然你也可以查资料修补。
    [root@localhost hiredis]# gcc example.c  -o example -lhiredis
    [root@localhost hiredis]# ./example 
    PING: PONG
    SET: OK
    SET (binary API): OK
    GET foo: hello world
    INCR counter: 3
    INCR counter: 4
    0) element-9
    1) element-8
    2) element-7
    3) element-6
    4) element-5
    5) element-4
    6) element-3
    7) element-2
    8) element-1
    9) element-0

    编程时,可以参照example.c 的实例即可

    自己编写的程序

      在下面的代码示例中,将给出两种最为常用的Redis命令操作方式,既普通调用方式和基于管线的调用方式。

    1. #include <stdio.h>  
    2.  #include <stdlib.h>  
    3.  #include <stddef.h>  
    4.  #include <stdarg.h>  
    5.  #include <string.h>  
    6.  #include <assert.h>  
    7.  #include <hiredis/hiredis.h>  
    8.    
    9.  void doTest()  
    10.  {  
    11.      int timeout = 10000;  
    12.      struct timeval tv;  
    13.      tv.tv_sec = timeout / 1000;  
    14.      tv.tv_usec = timeout * 1000;  
    15.      //以带有超时的方式链接Redis服务器,同时获取与Redis连接的上下文对象。  
    16.      //该对象将用于其后所有与Redis操作的函数。  
    17.      redisContext* c = redisConnect((char*)"127.0.0.1", 6379);  
    18.      if (c->err) {  
    19.          redisFree(c);  
    20.          return;  
    21.      }  
    22.      const char* command1 = "set stest1 value9";  
    23.      redisReply* r = (redisReply*)redisCommand(c,command1);  
    24.      //需要注意的是,如果返回的对象是NULL,则表示客户端和服务器之间出现严重错误,必须重新链接。  
    25.      //这里只是举例说明,简便起见,后面的命令就不再做这样的判断了。  
    26.      if (NULL == r) {  
    27.           redisFree(c);  
    28.          return;  
    29.      }  
    30.      //不同的Redis命令返回的数据类型不同,在获取之前需要先判断它的实际类型。  
    31.      //至于各种命令的返回值信息,可以参考Redis的官方文档,或者查看该系列博客的前几篇  
    32.      //有关Redis各种数据类型的博客。:)  
    33.      //字符串类型的set命令的返回值的类型是REDIS_REPLY_STATUS,然后只有当返回信息是"OK"  
    34.      //时,才表示该命令执行成功。后面的例子以此类推,就不再过多赘述了。  
    35.      if (!(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK") == 0)) {  
    36.          printf("Failed to execute command[%s]. ",command1);  
    37.          freeReplyObject(r);  
    38.          redisFree(c);  
    39.          return;  
    40.      }  
    41.      //由于后面重复使用该变量,所以需要提前释放,否则内存泄漏。  
    42.      freeReplyObject(r);  
    43.      printf("Succeed to execute command[%s]. ",command1);  
    44.    
    45.      const char* command2 = "strlen stest1";  
    46.      r = (redisReply*)redisCommand(c,command2);  
    47.      if (r->type != REDIS_REPLY_INTEGER) {  
    48.          printf("Failed to execute command[%s]. ",command2);  
    49.          freeReplyObject(r);  
    50.          redisFree(c);  
    51.          return;  
    52.      }  
    53.      int length = r->integer;  
    54.      freeReplyObject(r);  
    55.      printf("The length of 'stest1' is %d. ",length);  
    56.      printf("Succeed to execute command[%s]. ",command2);  
    57.    
    58.      const char* command3 = "get stest1";  
    59.      r = (redisReply*)redisCommand(c,command3);  
    60.      if (r->type != REDIS_REPLY_STRING) {  
    61.          printf("Failed to execute command[%s]. ",command3);  
    62.          freeReplyObject(r);  
    63.          redisFree(c);  
    64.          return;  
    65.      }  
    66.      printf("The value of 'stest1' is %s. ",r->str);  
    67.      freeReplyObject(r);  
    68.      printf("Succeed to execute command[%s]. ",command3);  
    69.    
    70.      const char* command4 = "get stest2";  
    71.      r = (redisReply*)redisCommand(c,command4);  
    72.      //这里需要先说明一下,由于stest2键并不存在,因此Redis会返回空结果,这里只是为了演示。  
    73.      if (r->type != REDIS_REPLY_NIL) {  
    74.          printf("Failed to execute command[%s]. ",command4);  
    75.          freeReplyObject(r);  
    76.          redisFree(c);  
    77.          return;  
    78.      }  
    79.      freeReplyObject(r);  
    80.      printf("Succeed to execute command[%s]. ",command4);  
    81.    
    82.      const char* command5 = "mget stest1 stest2";  
    83.      r = (redisReply*)redisCommand(c,command5);  
    84.      //不论stest2存在与否,Redis都会给出结果,只是第二个值为nil。  
    85.      //由于有多个值返回,因为返回应答的类型是数组类型。  
    86.      if (r->type != REDIS_REPLY_ARRAY) {  
    87.          printf("Failed to execute command[%s]. ",command5);  
    88.          freeReplyObject(r);  
    89.          redisFree(c);  
    90.          //r->elements表示子元素的数量,不管请求的key是否存在,该值都等于请求是键的数量。  
    91.          assert(2 == r->elements);  
    92.          return;  
    93.      }  
    94.      int i;  
    95.       for (i = 0; i < r->elements; ++i) {  
    96.          redisReply* childReply = r->element[i];  
    97.          //之前已经介绍过,get命令返回的数据类型是string。  
    98.          //对于不存在key的返回值,其类型为REDIS_REPLY_NIL。  
    99.          if (childReply->type == REDIS_REPLY_STRING)  
    100.              printf("The value is %s. ",childReply->str);  
    101.      }  
    102.      //对于每一个子应答,无需使用者单独释放,只需释放最外部的redisReply即可。  
    103.      freeReplyObject(r);  
    104.      printf("Succeed to execute command[%s]. ",command5);  
    105.    
    106.      printf("Begin to test pipeline. ");  
    107.      //该命令只是将待发送的命令写入到上下文对象的输出缓冲区中,直到调用后面的  
    108.      //redisGetReply命令才会批量将缓冲区中的命令写出到Redis服务器。这样可以  
    109.      //有效的减少客户端与服务器之间的同步等候时间,以及网络IO引起的延迟。  
    110.      //至于管线的具体性能优势,可以考虑该系列博客中的管线主题。  
    111.     /* if (REDIS_OK != redisAppendCommand(c,command1) 
    112.          || REDIS_OK != redisAppendCommand(c,command2) 
    113.          || REDIS_OK != redisAppendCommand(c,command3) 
    114.          || REDIS_OK != redisAppendCommand(c,command4) 
    115.          || REDIS_OK != redisAppendCommand(c,command5)) { 
    116.          redisFree(c); 
    117.          return; 
    118.      } 
    119.  */  
    120.   
    121.     redisAppendCommand(c,command1);  
    122.     redisAppendCommand(c,command2);  
    123.     redisAppendCommand(c,command3);  
    124.     redisAppendCommand(c,command4);  
    125.     redisAppendCommand(c,command5);  
    126.      redisReply* reply = NULL;  
    127.      //对pipeline返回结果的处理方式,和前面代码的处理方式完全一直,这里就不再重复给出了。  
    128.      if (REDIS_OK != redisGetReply(c,(void**)&reply)) {  
    129.          printf("Failed to execute command[%s] with Pipeline. ",command1);  
    130.          freeReplyObject(reply);  
    131.          redisFree(c);  
    132.      }  
    133.      freeReplyObject(reply);  
    134.      printf("Succeed to execute command[%s] with Pipeline. ",command1);  
    135.    
    136.      if (REDIS_OK != redisGetReply(c,(void**)&reply)) {  
    137.          printf("Failed to execute command[%s] with Pipeline. ",command2);  
    138.          freeReplyObject(reply);  
    139.          redisFree(c);  
    140.      }  
    141.      freeReplyObject(reply);  
    142.      printf("Succeed to execute command[%s] with Pipeline. ",command2);  
    143.    
    144.      if (REDIS_OK != redisGetReply(c,(void**)&reply)) {  
    145.          printf("Failed to execute command[%s] with Pipeline. ",command3);  
    146.          freeReplyObject(reply);  
    147.          redisFree(c);  
    148.      }  
    149.      freeReplyObject(reply);  
    150.      printf("Succeed to execute command[%s] with Pipeline. ",command3);  
    151.    
    152.      if (REDIS_OK != redisGetReply(c,(void**)&reply)) {  
    153.          printf("Failed to execute command[%s] with Pipeline. ",command4);  
    154.          freeReplyObject(reply);  
    155.          redisFree(c);  
    156.      }  
    157.      freeReplyObject(reply);  
    158.      printf("Succeed to execute command[%s] with Pipeline. ",command4);  
    159.    
    160.      if (REDIS_OK != redisGetReply(c,(void**)&reply)) {  
    161.          printf("Failed to execute command[%s] with Pipeline. ",command5);  
    162.          freeReplyObject(reply);  
    163.          redisFree(c);  
    164.      }  
    165.      freeReplyObject(reply);  
    166.      printf("Succeed to execute command[%s] with Pipeline. ",command5);  
    167.      //由于所有通过pipeline提交的命令结果均已为返回,如果此时继续调用redisGetReply,  
    168.      //将会导致该函数阻塞并挂起当前线程,直到有新的通过管线提交的命令结果返回。  
    169.      //最后不要忘记在退出前释放当前连接的上下文对象。  
    170.      redisFree(c);  
    171.      return;  
    172.  }  
    173.    
    174.  int main()  
    175.  {  
    176.      doTest();  
    177.      return 0;  
    178.  }  

    述文件命名为:mytest.c
    [root@loalhost hj]# gcc mytest.c -o mytest -lhiredis
    [root@localhost hj]# ./mytest 
    Succeed to execute command[set stest1 value9].
    The length of 'stest1' is 6.
    Succeed to execute command[strlen stest1].
    The value of 'stest1' is value9.
    Succeed to execute command[get stest1].
    Succeed to execute command[get stest2].
    The value is value9.
    Succeed to execute command[mget stest1 stest2].
    Begin to test pipeline.
    Succeed to execute command[set stest1 value9] 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.



    参考网址:

    http://tech.it168.com/a2011/0830/1239/000001239923_all.shtml

    http://www.cnblogs.com/stephen-liu74/category/354125.html

  • 相关阅读:
    python详解json模块
    postman---post请求数据类型
    postman---postman发送请求
    SpringBoot之集成通用Mapper
    Mybatis-generator/通用Mapper/Mybatis-Plus对比
    spring-data-JPA repository自定义方法规则
    JPA之@GeneratedValue注解
    Java工具类NumberUtils使用
    shell函数
    Maven的生命周期
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318321.html
Copyright © 2011-2022 走看看