[轉] Hiredis: redis c client使用註記

來源:互聯網
上載者:User

標籤:

  1. 編譯
  2. 使用
    • 初始化
    • 串連redis資料庫

      redisContext * pConn = redisConnect(redisIp.c_str(), redisPort);
      if (m_cLocal == NULL)
      {
      return 1;
      }

      if(pConn->err)
      {
      cout << "Connection to redis[" << redisIp << "] error: " << pConn->errstr;
      redisFree(pConn);
      return 2;
      }

    • 執行命令

      string cmd="set foo bar;
      redisReply * reply=(redisReply *)redisCommand(pConn, cmd.c_str());
      if(reply == NULL)
      {
      cout << "redis[" << redisIp << "] excute " << cmd << " failed, error is: " << pConn->errstr;
      redisFree(pConn);
      pConn=NULL;
      return 3;
      }
      freeReplyObject(reply);

    • 解析結果
      • 不同的命令返回不同類型的結果,需要根據reply->type欄位進行解析
  3. 注意事項

    Once an error is returned the context cannot be reused and you should set up a new connection.

    上面樣本中執行命令時,如果返回了錯誤,則串連不可用,需要重建立立串連。

 

    更多文章,歡迎訪問我的網站:零一積流。

[轉] Hiredis: redis c client使用註記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.