最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Redis教程(十五):C語(yǔ)言連接操作代碼實(shí)例

 更新時(shí)間:2015年05月04日 08:49:52   投稿:junjie  
這篇文章主要介紹了Redis教程(十五):C語(yǔ)言連接操作代碼實(shí)例,本篇博客是該系列博客中的最后一篇,在這里將給出基于Redis客戶端組件訪問(wèn)并操作Redis服務(wù)器的代碼示例,需要的朋友可以參考下

在之前的博客中已經(jīng)非常詳細(xì)的介紹了Redis的各種操作命令、運(yùn)行機(jī)制和服務(wù)器初始化參數(shù)配置。本篇博客是該系列博客中的最后一篇,在這里將給出基于Redis客戶端組件訪問(wèn)并操作Redis服務(wù)器的代碼示例。然而需要說(shuō)明的是,由于Redis官方并未提供基于C接口的Windows平臺(tái)客戶端,因此下面的示例僅可運(yùn)行于Linux/Unix平臺(tái)。但是對(duì)于使用其它編程語(yǔ)言的開(kāi)發(fā)者而言,如C#和Java,Redis則提供了針對(duì)這些語(yǔ)言的客戶端組件,通過(guò)該方式,同樣可以達(dá)到基于Windows平臺(tái)與Redis服務(wù)器進(jìn)行各種交互的目的。

該篇博客中使用的客戶端來(lái)自于Redis官方網(wǎng)站,是Redis推薦的基于C接口的客戶端組件,見(jiàn)如下鏈接:
https://github.com/antirez/hiredis
在下面的代碼示例中,將給出兩種最為常用的Redis命令操作方式,既普通調(diào)用方式和基于管線的調(diào)用方式。

注:在閱讀代碼時(shí)請(qǐng)留意注釋。

#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 = 10000;
  struct timeval tv;
  tv.tv_sec = timeout / 1000;
  tv.tv_usec = timeout * 1000;
  //以帶有超時(shí)的方式鏈接Redis服務(wù)器,同時(shí)獲取與Redis連接的上下文對(duì)象。
  //該對(duì)象將用于其后所有與Redis操作的函數(shù)。
  redisContext* c = redisConnectWithTimeout("192.168.149.137",6379,tv);
  if (c->err) {
    redisFree(c);
    return;
  }
  const char* command1 = "set stest1 value1";
  redisReply* r = (redisReply*)redisCommand(c,command1);
  //需要注意的是,如果返回的對(duì)象是NULL,則表示客戶端和服務(wù)器之間出現(xiàn)嚴(yán)重錯(cuò)誤,必須重新鏈接。
  //這里只是舉例說(shuō)明,簡(jiǎn)便起見(jiàn),后面的命令就不再做這樣的判斷了。
  if (NULL == r) {
    redisFree(c);
    return;
  }
  //不同的Redis命令返回的數(shù)據(jù)類型不同,在獲取之前需要先判斷它的實(shí)際類型。
  //至于各種命令的返回值信息,可以參考Redis的官方文檔,或者查看該系列博客的前幾篇
  //有關(guān)Redis各種數(shù)據(jù)類型的博客。:)
  //字符串類型的set命令的返回值的類型是REDIS_REPLY_STATUS,然后只有當(dāng)返回信息是"OK"
  //時(shí),才表示該命令執(zhí)行成功。后面的例子以此類推,就不再過(guò)多贅述了。
  if (!(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK") == 0)) {
    printf("Failed to execute command[%s].\n",command1);
    freeReplyObject(r);
    redisFree(c);
    return;
  }
  //由于后面重復(fù)使用該變量,所以需要提前釋放,否則內(nèi)存泄漏。
  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);
  //這里需要先說(shuō)明一下,由于stest2鍵并不存在,因此Redis會(huì)返回空結(jié)果,這里只是為了演示。
  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都會(huì)給出結(jié)果,只是第二個(gè)值為nil。
  //由于有多個(gè)值返回,因?yàn)榉祷貞?yīng)答的類型是數(shù)組類型。
  if (r->type != REDIS_REPLY_ARRAY) {
    printf("Failed to execute command[%s].\n",command5);
    freeReplyObject(r);
    redisFree(c);
    //r->elements表示子元素的數(shù)量,不管請(qǐng)求的key是否存在,該值都等于請(qǐng)求是鍵的數(shù)量。
    assert(2 == r->elements);
    return;
  }
  for (int i = 0; i < r->elements; ++i) {
    redisReply* childReply = r->element[i];
    //之前已經(jīng)介紹過(guò),get命令返回的數(shù)據(jù)類型是string。
    //對(duì)于不存在key的返回值,其類型為REDIS_REPLY_NIL。
    if (childReply->type == REDIS_REPLY_STRING)
      printf("The value is %s.\n",childReply->str);
  }
  //對(duì)于每一個(gè)子應(yīng)答,無(wú)需使用者單獨(dú)釋放,只需釋放最外部的redisReply即可。
  freeReplyObject(r);
  printf("Succeed to execute command[%s].\n",command5);

  printf("Begin to test pipeline.\n");
  //該命令只是將待發(fā)送的命令寫(xiě)入到上下文對(duì)象的輸出緩沖區(qū)中,直到調(diào)用后面的
  //redisGetReply命令才會(huì)批量將緩沖區(qū)中的命令寫(xiě)出到Redis服務(wù)器。這樣可以
  //有效的減少客戶端與服務(wù)器之間的同步等候時(shí)間,以及網(wǎng)絡(luò)IO引起的延遲。
  //至于管線的具體性能優(yōu)勢(shì),可以考慮該系列博客中的管線主題。
  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;
  //對(duì)pipeline返回結(jié)果的處理方式,和前面代碼的處理方式完全一直,這里就不再重復(fù)給出了。
  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);
  //由于所有通過(guò)pipeline提交的命令結(jié)果均已為返回,如果此時(shí)繼續(xù)調(diào)用redisGetReply,
  //將會(huì)導(dǎo)致該函數(shù)阻塞并掛起當(dāng)前線程,直到有新的通過(guò)管線提交的命令結(jié)果返回。
  //最后不要忘記在退出前釋放當(dāng)前連接的上下文對(duì)象。
  redisFree(c);
  return;
}

int main() 
{
  doTest();
  return 0;
}

//輸出結(jié)果如下:
//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.

相關(guān)文章

  • Redis如何統(tǒng)計(jì)用戶訪問(wèn)量

    Redis如何統(tǒng)計(jì)用戶訪問(wèn)量

    這篇文章主要介紹了Redis如何統(tǒng)計(jì)用戶訪問(wèn)量問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • redis基本類型和使用方法詳解

    redis基本類型和使用方法詳解

    這篇文章主要介紹了redis基本類型和使用方法詳解,需要的朋友可以參考下
    2020-02-02
  • Redis教程(七):Key操作命令詳解

    Redis教程(七):Key操作命令詳解

    這篇文章主要介紹了Redis教程(七):Key操作命令詳解,本文講解了Key操作命令概述、相關(guān)命令列表、命令使用示例等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Redis分布式鎖存在的問(wèn)題(推薦)

    Redis分布式鎖存在的問(wèn)題(推薦)

    有很多基于Redis實(shí)現(xiàn)的分布式鎖方案或者庫(kù),但是有些庫(kù)并沒(méi)有解決分布式環(huán)境下的一些問(wèn)題陷阱,這篇文章主要介紹了Redis分布式鎖存在的問(wèn)題,需要的朋友可以參考下
    2022-12-12
  • 詳解如何利用Redis實(shí)現(xiàn)生成唯一ID

    詳解如何利用Redis實(shí)現(xiàn)生成唯一ID

    隨著下單流量逐漸上升,為了降低數(shù)據(jù)庫(kù)的訪問(wèn)壓力,需要通過(guò)請(qǐng)求唯一ID+redis分布式鎖來(lái)防止接口重復(fù)提交。今天我們就一起來(lái)看探討一下,如何通過(guò)服務(wù)端來(lái)完成請(qǐng)求唯一?ID?的生成
    2022-11-11
  • 一文快速搞懂Redis的幾種數(shù)據(jù)類型方式

    一文快速搞懂Redis的幾種數(shù)據(jù)類型方式

    這篇文章主要介紹了一文快速搞懂Redis的幾種數(shù)據(jù)類型方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • Redis異常測(cè)試盤(pán)點(diǎn)分析

    Redis異常測(cè)試盤(pán)點(diǎn)分析

    這篇文章主要為大家介紹了Redis異常測(cè)試盤(pán)點(diǎn)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • Redis設(shè)置Hash數(shù)據(jù)類型的過(guò)期時(shí)間

    Redis設(shè)置Hash數(shù)據(jù)類型的過(guò)期時(shí)間

    在Redis中,我們可以使用Hash數(shù)據(jù)結(jié)構(gòu)來(lái)存儲(chǔ)一組鍵值對(duì),而有時(shí)候,我們可能需要設(shè)置這些鍵值對(duì)的過(guò)期時(shí)間,本文主要介紹了Redis設(shè)置Hash數(shù)據(jù)類型的過(guò)期時(shí)間,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • Redis實(shí)現(xiàn)訂單自動(dòng)過(guò)期功能的示例代碼

    Redis實(shí)現(xiàn)訂單自動(dòng)過(guò)期功能的示例代碼

    這篇文章主要介紹了Redis實(shí)現(xiàn)訂單自動(dòng)過(guò)期功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • 基于Redis實(shí)現(xiàn)分布式鎖以及任務(wù)隊(duì)列

    基于Redis實(shí)現(xiàn)分布式鎖以及任務(wù)隊(duì)列

    這篇文章主要介紹了基于Redis實(shí)現(xiàn)分布式鎖以及任務(wù)隊(duì)列,需要的朋友可以參考下
    2015-11-11

最新評(píng)論

泰安市| 视频| 赣州市| 贡嘎县| 沈丘县| 牙克石市| 攀枝花市| 昆明市| 张家界市| 青龙| 台安县| 云和县| 泰和县| 石首市| 自治县| 新宾| 柳州市| 辰溪县| 包头市| 石楼县| 临西县| 城口县| 沅江市| 维西| 阿克| 连平县| 新平| 漳浦县| 额济纳旗| 丰城市| 区。| 洛阳市| 陕西省| 大冶市| 顺平县| 嘉定区| 霍邱县| 临武县| 武川县| 永定县| 萨迦县|