• hiredis的代码示例


    redis的哈希的存取

    redis 收发数据最简单的例子

    适配器 

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

    1. void *writeInfoToRedis(const SessionInfo &info)
    2. {
    3. redisContext *context;
    4. redisReply *reply;
    5. struct timeval timeout = {1, 500000};
    6. context = redisConnectWithTimeout("127.0.0.1", 6379, timeout);
    7. if (context == NULL || context->err)
    8. {
    9. if (context == NULL)
    10. {
    11. cout << "line = 604, Connection error == " << context->errstr << ", context->err = " << context->err << endl;
    12. redisFree(context);
    13. }
    14. else
    15. {
    16. TRACE_LOG(EM_LEVEL_ERR, "Connection error: can't allocate redis context\n");
    17. }
    18. return NULL;
    19. }
    20. cout << "Connection 成功 " << endl;
    21. reply = (redisReply *)redisCommand(context, "AUTH z4afort");
    22. if (reply->type == REDIS_REPLY_ERROR)
    23. {
    24. cout << "[Redis] Auth failed" << endl;
    25. redisFree(context);
    26. freeReplyObject(reply);
    27. return NULL;
    28. }
    29. string ssoSessionId = "11111111112";
    30. string accNoFlag = "accno";
    31. string accNo = "admin";
    32. string phonenum = "15490878888";
    33. string requestTime = "2022-03-25 16:27:41";
    34. char arrAccNoFlag[20] = "accno";
    35. char arrAccNo[20] = "admin";
    36. reply = (redisReply *)redisCommand(context, "hset %s %b %b", ssoSessionId.c_str(), accNoFlag.data(),accNoFlag.length(),accNo.data(),accNo.length());
    37. if (NULL == reply)
    38. {
    39. cout << "[ Redis] hset ssoSessionId failed,reply->str =" << reply->str << ",reply->type = " << reply->type << endl;
    40. }
    41. else
    42. {
    43. cout << "[ Redis] hset ssoSessionId succedd " << endl;
    44. }
    45. redisFree(context);
    46. freeReplyObject(reply);
    47. return NULL;
    48. }

  • 相关阅读:
    推荐一个开源的项目工时系统:无鱼工时系统
    【专栏】RPC系列(实战)-摸清RPC骨架
    Jmeter实现接口文件上传、阅览和下载
    Google Analytics优缺点分析
    B样条插值:Python实现给定一些坐标,在这些坐标中间插入一些坐标,使得它们更连贯
    Git 提交规范
    RTP相关
    荐书 | 为什么喜欢的女生这么难追?
    Docker笔记
    FBI:皇家勒索软件要求350名受害者支付2.75亿美元
  • 原文地址:https://blog.csdn.net/Edidaughter/article/details/125480010