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

  1. 編譯
  2. 使用
    • 初始化
    • 鏈接redis數據庫

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

      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);get

    • 解析結果
      • 不一樣的命令返回不一樣類型的結果,須要根據reply->type字段進行解析
  3. 注意事項

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

    上面示例中執行命令時,若是返回了錯誤,則鏈接不可用,須要從新創建鏈接。string

 

    更多文章,歡迎訪問個人站點:零一積流it

相關文章
相關標籤/搜索