[deal_cache.c]分析

deal_cache.c封装了所有处理缓存数据库存储业务

int deal_cache_setLifecycle(cJSON *root)

  • 给缓存中的sessionid设置生命周期

  • 底层调用的也是封装好的redis接口

      int deal_cache_setLifecycle(cJSON *root)
      {
          /*
              {
                  cmd:     "setLifcycle",
                  key:     "online-driver-[sessionid]",
                  lifecycle: 600
              }
           */
              int ret = 0;
      
              cJSON* key          = cJSON_GetObjectItem(root, "key");
              cJSON* lifecycle    = cJSON_GetObjectItem(root, "lifecycle");
              
              LOG(LOG_MODULE_SERVER_DATA, LOG_PROC_CACHE, "key = %s, lifecycle = %d\n", key->valuestring, lifecycle->valueint);
      
              redisContext* conn = rop_connectdb_nopwd(g_db_config.cache_ip,
                                                       g_db_config.cache_port);
              if (conn == NULL) {
                  LOG(LOG_MODULE_SERVER_DATA, LOG_PROC_CACHE, "redis connect error %s:%s\n", g_db_config.cache_ip, g_db_config.cache_port); 
                  ret = -1;
                  goto END;
              
              }
              //给该key设置声明周期
              ret = rop_set_key_lifetime(conn, key->valuestring, lifecycle->valueint);
      
      END:
              rop_disconnect(conn);
              return ret;
      } 
    

int deal_cache_setString(cJSON *root)

  • 处理set string操作,

      int deal_cache_setString(cJSON *root)
      {
              int ret = 0;
              cJSON* key      = cJSON_GetObjectItem(root, "key");
              cJSON* value    = cJSON_GetObjectItem(root, "value");
              cJSON* unlimited= cJSON_GetObjectItem(root, "unlimited");
      
          LOG(LOG_MODULE_SERVER_DATA, LOG_PROC_CACHE, "key = %s\n", key->valuestring);
          LOG(LOG_MODULE_SERVER_DATA, LOG_PROC_CACHE, "value = %s\n", value->valuestring);
          LOG(LOG_MODULE_SERVER_DATA, LOG_PROC_CACHE, "unlimited = %s\n", unlimited->valuestring);
    
    
          redisContext* conn = rop_connectdb_nopwd(g_db_config.cache_ip,
                                                   g_db_config.cache_port);
          if (conn == NULL) {
              LOG(LOG_MODULE_SERVER_DATA, LOG_PROC_CACHE, "redis connect error %s:%s\n", g_db_config.cache_ip, g_db_config.cache_port); 
              ret = -1;
              goto END;
          
          }
          
          ret = rop_set_string(conn, key->valuestring, value->valuestring);
    
    
          if (strcmp(unlimited->valuestring, "no") == 0) {
              cJSON* lifecycle= cJSON_GetObjectItem(root, "lifecycle");
              LOG(LOG_MODULE_SERVER_DATA, LOG_PROC_CACHE, "lifecycle = %d\n", lifecycle->valueint);
              //给该key设置声明周期
              rop_set_key_lifetime(conn, key->valuestring, lifecycle->valueint);
          }
      
      END:
              rop_disconnect(conn);
              return ret;
      }
    

int deal_cache_existKey(cJSON *root)

  • 判断key是否存在

  • 这些底层的redis接口参数是固定的,因此协议也要在这个基础上才能做二次封装

      int deal_cache_existKey(cJSON *root)
      {
      
          /*
              {
                  cmd:     "existKey",
                  key:     "online-driver-[sessionid]",
              }
           */
              int ret = 0;
      
              cJSON* key          = cJSON_GetObjectItem(root, "key");
              
              LOG(LOG_MODULE_SERVER_DATA, LOG_PROC_CACHE, "key = %s\n", key->valuestring);
      
              redisContext* conn = rop_connectdb_nopwd(g_db_config.cache_ip,
                                                       g_db_config.cache_port);
              if (conn == NULL) {
                  LOG(LOG_MODULE_SERVER_DATA, LOG_PROC_CACHE, "redis connect error %s:%s\n", g_db_config.cache_ip, g_db_config.cache_port); 
                  ret = -1;
                  goto END;
              
              }
      
              //判断key是否存在
              if (rop_is_key_exist(conn, key->valuestring) == 1) {
                  ret = 0;
              }
              else {
                  ret = -1;
              }
      
      END:
              rop_disconnect(conn);
              return ret;
      }
    

int deal_cache_setHash(cJSON *root)

  • 对于setHash,应用服务器将key,fields和values封装一起发过来,fields和values都是数组(里面都字符串(不是对象))

  • 在解析Jason时,RFIELDS rfields 数组类型, RVALUES rvalues数组类型将解析出的值按照索引一个个对应的解析出来(field与values对应)

  • ret = rop_hash_set_append(conn, key->valuestring, rfields, rvalues, array_size);这个接口将两个数组里的值对应的setHash

     int deal_cache_setHash(cJSON *root)
     {
             int ret = 0;
             int i = 0;
     
             /*
                 cmd:     "setHash",
                 key:     "online-driver-[sessionid]",
                 fields:   ["username","orderid"],
                 values:   ["盖伦", "NONE"]
              */
             cJSON* key      = cJSON_GetObjectItem(root, "key");
             cJSON* fields   = cJSON_GetObjectItem(root, "fields");
             cJSON* values   = cJSON_GetObjectItem(root, "values");
             int array_size = cJSON_GetArraySize(fields);
     
             RFIELDS rfields  = malloc (FIELD_ID_SIZE *array_size);
             RVALUES rvalues = malloc(VALUES_ID_SIZE *array_size);
     
     
     
             for (i = 0;i < array_size; i++) {
                 strncpy(rfields[i], cJSON_GetArrayItem(fields, i)->valuestring, FIELD_ID_SIZE-1);
                 strncpy(rvalues[i], cJSON_GetArrayItem(values, i)->valuestring, VALUES_ID_SIZE-1);
             }
     
     
             redisContext* conn = rop_connectdb_nopwd(g_db_config.cache_ip,
                                                      g_db_config.cache_port);
             if (conn == NULL) {
                 LOG(LOG_MODULE_SERVER_DATA, LOG_PROC_CACHE, "redis connect error %s:%s\n", g_db_config.cache_ip, g_db_config.cache_port); 
                 ret = -1;
                 goto END;
             
             }
             
             ret = rop_hash_set_append(conn, key->valuestring, rfields, rvalues, array_size);
     
     
     END:
             rop_disconnect(conn);
             if (rfields != NULL) {
                 free(rfields);
             }
             if (rvalues != NULL) {
                 free(rvalues);
             }
             return ret;
     }
    
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页