亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

該篇博客中使用的客戶端來自于Redis官方網(wǎng)站,是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 = 10000;
  struct timeval tv;
  tv.tv_sec = timeout / 1000;
  tv.tv_usec = timeout * 1000;
  //以帶有超時的方式鏈接Redis服務器,同時獲取與Redis連接的上下文對象。
  //該對象將用于其后所有與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);
  //需要注意的是,如果返回的對象是NULL,則表示客戶端和服務器之間出現(xiàn)嚴重錯誤,必須重新鏈接。
  //這里只是舉例說明,簡便起見,后面的命令就不再做這樣的判斷了。
  if (NULL == r) {
    redisFree(c);
    return;
  }
  //不同的Redis命令返回的數(shù)據(jù)類型不同,在獲取之前需要先判斷它的實際類型。
  //至于各種命令的返回值信息,可以參考Redis的官方文檔,或者查看該系列博客的前幾篇
  //有關Redis各種數(shù)據(jù)類型的博客。:)
  //字符串類型的set命令的返回值的類型是REDIS_REPLY_STATUS,然后只有當返回信息是"OK"
  //時,才表示該命令執(zhí)行成功。后面的例子以此類推,就不再過多贅述了。
  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 '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。
  //由于有多個值返回,因為返回應答的類型是數(shù)組類型。
  if (r->type != REDIS_REPLY_ARRAY) {
    printf("Failed to execute command[%s].\n",command5);
    freeReplyObject(r);
    redisFree(c);
    //r->elements表示子元素的數(shù)量,不管請求的key是否存在,該值都等于請求是鍵的數(shù)量。
    assert(2 == r->elements);
    return;
  }
  for (int i = 0; i < r->elements; ++i) {
    redisReply* childReply = r->element[i];
    //之前已經(jīng)介紹過,get命令返回的數(shù)據(jù)類型是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");
  //該命令只是將待發(fā)送的命令寫入到上下文對象的輸出緩沖區(qū)中,直到調用后面的
  //redisGetReply命令才會批量將緩沖區(qū)中的命令寫出到Redis服務器。這樣可以
  //有效的減少客戶端與服務器之間的同步等候時間,以及網(wǎng)絡IO引起的延遲。
  //至于管線的具體性能優(yōu)勢,可以考慮該系列博客中的管線主題。
  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提交的命令結果均已為返回,如果此時繼續(xù)調用redisGetReply,
  //將會導致該函數(shù)阻塞并掛起當前線程,直到有新的通過管線提交的命令結果返回。
  //最后不要忘記在退出前釋放當前連接的上下文對象。
  redisFree(c);
  return;
}

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

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

相關文章

  • Redis設置Hash數(shù)據(jù)類型的過期時間

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

    在Redis中,我們可以使用Hash數(shù)據(jù)結構來存儲一組鍵值對,而有時候,我們可能需要設置這些鍵值對的過期時間,本文主要介紹了Redis設置Hash數(shù)據(jù)類型的過期時間,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • 在Redis中如何保存時間序列數(shù)據(jù)詳解

    在Redis中如何保存時間序列數(shù)據(jù)詳解

    與發(fā)生時間相關的一組數(shù)據(jù),就是時間序列數(shù)據(jù),這些數(shù)據(jù)的特點是沒有嚴格的關系模型,記錄的信息可以表示成鍵和值的關系,這篇文章主要給大家介紹了關于在Redis中如何保存時間序列數(shù)據(jù)的相關資料,需要的朋友可以參考下
    2021-10-10
  • Redis實現(xiàn)分布式鎖的示例代碼

    Redis實現(xiàn)分布式鎖的示例代碼

    日常開發(fā)中,秒殺下單、搶紅包等等業(yè)務場景,都需要用到分布式鎖,本文主要介紹了Redis實現(xiàn)分布式鎖的示例代碼,感興趣的可以了解一下
    2023-10-10
  • Redis過期刪除策略與內存淘汰策略

    Redis過期刪除策略與內存淘汰策略

    這篇文章主要介紹了Redis過期刪除策略與內存淘汰策略,文章圍繞主題展開詳細的內容戒殺,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Redis分布式鎖的實現(xiàn)方式(redis面試題)

    Redis分布式鎖的實現(xiàn)方式(redis面試題)

    這篇文章主要介紹了Redis分布式鎖的實現(xiàn)方式(面試常見),需要的朋友可以參考下
    2020-01-01
  • redis擊穿 雪崩 穿透超詳細解決方案梳理

    redis擊穿 雪崩 穿透超詳細解決方案梳理

    這篇文章主要為大家介紹了Redis擊穿穿透雪崩產(chǎn)生原因及解決思路的解決方案參考,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進步
    2022-03-03
  • 淺談RedisTemplate和StringRedisTemplate的區(qū)別

    淺談RedisTemplate和StringRedisTemplate的區(qū)別

    本文主要介紹了RedisTemplate和StringRedisTemplate的區(qū)別及個人見解,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Redis數(shù)據(jù)結構原理淺析

    Redis數(shù)據(jù)結構原理淺析

    這篇文章主要為大家介紹了Redis數(shù)據(jù)結構原理淺析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • 關于Redis數(shù)據(jù)庫三種持久化方案介紹

    關于Redis數(shù)據(jù)庫三種持久化方案介紹

    大家好,本篇文章主要講的是關于Redis數(shù)據(jù)庫三種持久化方案介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Linux下Redis安裝教程詳解

    Linux下Redis安裝教程詳解

    這篇文章主要為大家詳細介紹了Linux下Redis安裝教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-09-09

最新評論