• 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. }

  • 相关阅读:
    大数据专业学起来会不会很累?
    计算机毕业设计ssm+vue基本微信小程序的“香草屋”饮料奶茶点单小程序
    Django思维导图-路由
    MySQL 事务常见面试题总结 | JavaGuide 审核中
    vue3.0 axios封装
    对你的第一个推荐计划的期望
    记录一次关于Rank()排序函数问题
    一三六、从零到一实现自动化部署
    C++之继承<1>【详解】
    构造方法的私有化和枚举enum
  • 原文地址:https://blog.csdn.net/Edidaughter/article/details/125480010