hiredis的代码示例

redis的哈希的存取

redis 收发数据最简单的例子

适配器

/* This is the reply object returned by redisCommand() */
typedef struct redisReply {
    int type; /* REDIS_REPLY_* */
    long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
    double dval; /* The double when type is REDIS_REPLY_DOUBLE */
    size_t len; /* Length of string */
    char *str; /* Used for REDIS_REPLY_ERROR, REDIS_REPLY_STRING
                  and REDIS_REPLY_DOUBLE (in additionl to dval). */
    char vtype[4]; /* Used for REDIS_REPLY_VERB, contains the null
                      terminated 3 character content type, such as "txt". */
    size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
    struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
} redisReply;

void *writeInfoToRedis(const SessionInfo &info)
{
    redisContext *context;
    redisReply *reply;
    struct timeval timeout = {1, 500000};
    context = redisConnectWithTimeout("127.0.0.1", 6379, timeout);
    if (context == NULL || context->err)
    {
        if (context == NULL)
        {
            cout << "line = 604, Connection error == " << context->errstr << ", context->err = " << context->err << endl;
            redisFree(context);
        }
        else
        {
            TRACE_LOG(EM_LEVEL_ERR, "Connection error: can't allocate redis context\n");
        }
        return NULL;
    }
    cout << "Connection  成功 " << endl;
    reply = (redisReply *)redisCommand(context, "AUTH z4afort");
    if (reply->type == REDIS_REPLY_ERROR)
    {
        cout << "[Redis] Auth failed" << endl;
        redisFree(context);
        freeReplyObject(reply);
        return NULL;
    }

    string ssoSessionId = "11111111112";
    string accNoFlag = "accno";
    string accNo = "admin";
    string phonenum = "15490878888";
    string requestTime = "2022-03-25 16:27:41";
    char arrAccNoFlag[20] = "accno";
    char arrAccNo[20] = "admin";
    reply = (redisReply *)redisCommand(context, "hset %s %b %b", ssoSessionId.c_str(), accNoFlag.data(),accNoFlag.length(),accNo.data(),accNo.length());
    if (NULL == reply)
    {
        cout << "[ Redis] hset  ssoSessionId  failed,reply->str =" << reply->str << ",reply->type = " << reply->type << endl;
    }
    else
    {
        cout << "[ Redis] hset  ssoSessionId   succedd " << endl;
    }

    redisFree(context);
    freeReplyObject(reply);
    return NULL;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值