V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
weixiaoyao
V2EX  ›  Redis

hiredis 库如何实现事务

  •  
  •   weixiaoyao · Nov 22, 2018 · 4943 views
    This topic created in 2712 days ago, the information mentioned may be changed or developed.

    近期使用 hiredis 库,用到了 pipeline 的方式,但是 pipiline 无法解决多客户端时可能操作同样 key 的竞争问题,想使用事务方式,但是不知道该怎么实现,有用过的朋友可以指点下么,谢谢了。 下面是用 redisAppendCommand 和 redisGetReply 实现的 pipeline 方式

    //输入命令写入缓冲区
    int nCmdCount = vecPipelineCmd.size();
    for (int i = 0; i < nCmdCount; i++)
    {
    	redisAppendCommand(m_pContext, vecPipelineCmd[i].c_str());
    }
    
    //批量发送命令并获取批量回复结果,redisGetReply 的调用次数必须和 redisAppendCommand 一致
    nSuccessCnt = 0;
    for (int i = 0; i < nCmdCount; i++)
    {
    	bool bStatus = false;
    	string strRsp("");
    	redisReply* reply = NULL;
    
    	if(redisGetReply(m_pContext, (void**)&reply) == REDIS_OK && reply != NULL && reply->type == REDIS_REPLY_STRING)
    	{
    		bStatus = true;
    		strRsp = reply->str;
    		nSuccessCnt++;
    	}
    
    	//释放 reply 的内存空间
    	freeReplyObject(reply);
    
    	vecPipelineStatus.push_back(bStatus);
    	vecPipelineRsp.push_back(strRsp);
    }
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1286 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:59 · PVG 07:59 · LAX 16:59 · JFK 19:59
    ♥ Do have faith in what you're doing.