开发者社区 问答 正文

hiredis的执行命令的问题?报错

我要从redis中取有序集里面score最大的member,对,只需要一条。

std::string CRedis::zrevrangeGetLastScore(std::string &key)
{
    redisReply* m_pReply = (redisReply*)redisCommand(m_pConn, "zrevrange %s 0 -1", key.c_str());
    if (NULL != m_pReply)
    {
    	if (m_pReply->type != REDIS_REPLY_NIL)
    	{
    		m_sReply = m_pReply->element[0]->str;
    		freeReplyObject(m_pReply);
    		return m_sReply;
    	}
    	else
    	{
    		return "empty";
    	}
    }
    return "empty";
}

问题来了:

当我给变量key赋值为有序集里不存在的值时,此时g++编译器报错“吐核”。

当我给key赋值有序集中存在的值时,能够正常返回score最大的member。

 

type类型判断可能有点问题,我试了很多种,变量key不存在有序集中,他的结果集redisReply*应该是非空,非错误的,那么属于哪种类型?

展开
收起
爱吃鱼的程序员 2020-06-06 15:05:54 585 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
                        <p>不应该这样判断, 应该把</p> 
    
    m_pReply->type != REDIS_REPLY_NIL

    改成

    m_pReply->type == REDIS_REPLY_ARRAY

     

                            这样需要请求两次了, 返回的是array类型的, 你判断一下大小就行
                        
    
                            这样改的话,每次的结果都会吐核,因为就算查出来的结果是空的,它也是REDIS_REPLY_ARRAY类型的,这我就很无语了。
                        
    
                        <p>换了一下思路,解决问题了。先用其他语句先判断它是否存在,不存在就不查找,也就不会崩溃了。诶,好冷清</p> 
    
    std::string CRedis::zrevrangeGetLastScore(std::string &key)
    {
    	if (0 == isExistZrevrange(key))
    	{
    		return "empty";
    	}
    
        redisReply* m_pReply = (redisReply*)redisCommand(m_pConn, "zrevrange %s 0 -1", key.c_str());
        if (NULL != m_pReply)
        {
        	if (m_pReply->type != REDIS_REPLY_NIL)
        	{
        		m_sReply = m_pReply->element[0]->str;
        		freeReplyObject(m_pReply);
        		return m_sReply;
        	}
        	else
        	{
        		return "empty";
        	}
        }
        return "empty";
    }		
    
    long long CRedis::isExistZrevrange(std::string &key)
    {
    	redisReply* m_pReply = (redisReply*)redisCommand(m_pConn, "ZCARD %s", key.c_str());
    	if (NULL != m_pReply)
    	{
    		if (m_pReply->type == REDIS_REPLY_INTEGER)
        	{
        		freeReplyObject(m_pReply);
        		return m_pReply->integer;
        	}
        	else
       		{
       			return 0;
       		}
    	}
    	return 0;
    }

     

                        <div class='ref'><h4>引用来自“LoSingSang”的评论</h4><p>换了一下思路,解决问题了。先用其他语句先判断它是否存在,不存在就不查找,也就不会崩溃了。诶,好冷清</p> 
    
    std::string CRedis::zrevrangeGetLastScore(std::string &key)
    {
    	if (0 == isExistZrevrange(key))
    	{
    		return "empty";
    	}
    
        redisReply* m_pReply = (redisReply*)redisCommand(m_pConn, "zrevrange %s 0 -1", key.c_str());
        if (NULL != m_pReply)
        {
        	if (m_pReply->type != REDIS_REPLY_NIL)
        	{
        		m_sReply = m_pReply->element[0]->str;
        		freeReplyObject(m_pReply);
        		return m_sReply;
        	}
        	else
        	{
        		return "empty";
        	}
        }
        return "empty";
    }		
    
    long long CRedis::isExistZrevrange(std::string &key)
    {
    	redisReply* m_pReply = (redisReply*)redisCommand(m_pConn, "ZCARD %s", key.c_str());
    	if (NULL != m_pReply)
    	{
    		if (m_pReply->type == REDIS_REPLY_INTEGER)
        	{
        		freeReplyObject(m_pReply);
        		return m_pReply->integer;
        	}
        	else
       		{
       			return 0;
       		}
    	}
    	return 0;
    }

     

    2020-06-06 15:06:12
    赞同 展开评论