redis使用笔记

小明 2025-05-01 09:43:17 13

redis使用笔记

  • 1、Redis简介
    • 1.1 含义
    • 1.2 功能
    • 1.3 特点
    • 2. 常用的数据结构
      • 2.1 HASH
      • 3 redis接口定义
        • 3.1 redisReply
        • 3.2 redisContext
        • 3.3 redisCommand
        • 4 实践操作
          • 4.1 遇到问题
            • 4.1.1 Get哈希的时候返回error
            • 4.1.2 长度一���为0,str没法打印(未解决)

              1、Redis简介

              最近项目需要使用redis,第一次操作做下工作笔记,以方便后续遇到问题时回头整理复盘,本篇我会持续补充持续更新~

              1.1 含义

              Redis(Remote Dictionary Server),是一种基于内存的数据结构存储服务。

              1.2 功能

              可以用作于数据库,缓存和消息队列,而且是开源的。

              1.3 特点

              • 基于内存的存储,操作速度非常快;
              • 丰富的数据结构,包括字符串、列表、集合、有序集合等;
              • 支持持久化存储;
              • 集群支持;
              • 灵活性;
              • 易于使用;

                2. 常用的数据结构

                2.1 HASH

                HMSET key field value [field value ...]
                

                如果key存在,则更新value的值,如果key不存在,则更新key-value的键值对

                HMSET usertable name "Alice" age 30 gender "female"
                

                解释:将name Alice、age 30、gender female设置到usertable里面。同理,有set就必然会有get,含义就不再赘述,直接看代码:

                HMGET key field [field ...]
                HMGET usertable name age gender // 举例
                

                即从usertable里面获取name age gender这三个哈希字段,这只是命令的源码,实际开发过程中还要配合hiredis的接口进行操作访问:

                3 redis接口定义

                3.1 redisReply

                typedef struct redisReply {
                    int type;
                    long long integer;
                    double dval;
                    size_t len;
                    char *str;
                    char vtype[4];
                    size_t elements;
                    struct redisReply **element;
                } redisReply;
                

                type的枚举类型如下:

                #define REDIS_REPLY_STRING 1 // 字符串
                #define REDIS_REPLY_ARRAY 2 // 数组
                #define REDIS_REPLY_INTEGER 3 // 整形
                #define REDIS_REPLY_NIL 4 // 不存在的类型
                #define REDIS_REPLY_STATUS 5 // 状态回复类型
                #define REDIS_REPLY_ERROR 6 // 错误恢复类型
                #define REDIS_REPLY_DOUBLE 7
                #define REDIS_REPLY_BOOL 8
                #define REDIS_REPLY_MAP 9
                #define REDIS_REPLY_SET 10
                #define REDIS_REPLY_ATTR 11
                #define REDIS_REPLY_PUSH 12
                #define REDIS_REPLY_BIGNUM 13
                #define REDIS_REPLY_VERB 14
                

                3.2 redisContext

                一般是用来与redis服务器建连,常见用法如下:

                redisContext *context = redisConnect("127.0.0.1", 6379);
                if ((context == nullptr) || (context->err)) {
                      if (context) {
                          qDebug() 
                          qDebug() 
                	const char* hostIP = "xxx.xxx.xxx.xxx";
                    qDebug() 
                        if (context) {
                            qDebug() 
                            qDebug() 
                        qDebug() 
                        qDebug() 
                        printf("rely str is %s\n", reply-str);
                        printf("rely len is %s\n", reply-len);
                    }
                
The End
微信