Redis

Redis 如何设置包含空格的Value

2016-12-01  本文已影响4077人  小小小码农

Redis对于一个做后端的人来说,应该再熟悉不过了,但是最近工作中折腾一个问题许久,其实问题不难,已经不是第一次遇到,以前总没有寻根问源,用其他方式规避了问题,导致一而再的犯同样的错误,着实需要反思下,因此有了此文。

问题

Redis中 string 类型数据结构在设置一对 key-value 的时候, value 中有空格。我使用的 Redis API 接口是已经封装了一层的,只用传入一个命令字符串就行,因此我会先将命令字符串拼接好,然后调用相应的接口即可,然后呢,操作就失败了。。。

原因

Redis的 API 接口在解析字符串的时候,会将空格作为分隔符分割命令,从而导致命令的参数个不对。
比如: SET k "V V",分割后就变为了 SETKVV 4个块,最终 Redis 执行命令的时候就会判定语法错误,因为参数超过了SET命令的限制要求。

命令字符串

刚遇到这个问题,第一反应就是去处理命令字符串,将字符串中value部分特殊处理,比如用"将有空格的字符串包含起来,自以为这样就不会被分割,实际结果就是狠狠的打脸,依然不行,究其原因就是:拼接的时候采用的prinf格式标准方式拼接,的确会对"包含的字符串特殊处理,但是当命令字符串再次传入 Redis API 中的时候就是一普通字符串了,依旧不行。

那还有其他方式么,当然有,将字符串转码,取出的时候再解码,但这种方式明显会恶心到自己,是万万不能接受的,所以我就决定花了一点时间去看了看 Redis 对应 API 的源码。

redisCommand API

Redis执行命令的 API 为 redisCommand。

void *redisCommand(redisContext *c, const char *format, ...)

仔细看了下源码发现,该 API 提供了和printf一样的标准格式化输入,也就是说,直接使用redisCommand是可以完美处理有空格的value,只是因为我们封装了一层就导致了格式化失效了,遇到空格就被分割了,最终导致命令语法错误。

标准输入格式

redisCommand 的输入格式与 prinf 基本相同,甚至可以说是增强版的,下面一个个说明。

|数据长度|d i|u o x X|
|:--:|:-:|:-:|:-:|:
|none|int|unsigned int|
|hh|signed char|unsigned char|
|h|short int|unsigned short int|
|l|long int| unsigned long int|
|ll|long long int| unsigned long long int|

注意:上表表示不同的前缀表示的数据范围不同

示例

#include "hiredis.h"
#include <stdio.h>
#include <string.h>


#define HOST "127.0.0.1"
#define PORT 6379

void CheckSetResult(redisReply* pReply, char *key)
{
    if(pReply != NULL && pReply->type == REDIS_REPLY_ERROR )
    {
        printf("SET[%s] error:%s\n", key, pReply->str);
    }
    if(pReply != NULL)
    {
        freeReplyObject(pReply);
    }
}

void GetKey(redisContext* pRedisConn, char *key)
{
    redisReply* pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "GET %s", key));
    if(NULL == pReply)
    {
        printf("Get Key[%s] error\n", key);
    }
    printf("Get Key[%s] value[%s]\n", key, pReply->str);
}

int main(int argc, char *argv[])
{
    redisContext* pRedisConn = redisConnect(HOST, PORT);
    if(pRedisConn->err)
    {
        printf("connect redis server fail!\n");
        return -1;
    }
    printf("connect redis server success!\n");

    char *test_key = "test_key";
    char *value = "teat value";
    redisReply* pReply = NULL;
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %s", test_key, value));
    CheckSetResult(pReply, test_key);

    char *test_key_b = "test_key_b";
    char *value_b = "test value b";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %b", test_key_b, value_b, strlen(value_b)));
    CheckSetResult(pReply, test_key_b);


    char *test_key_hh_d = "test_key_hh_d";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %hhd", test_key_hh_d, -161));
    CheckSetResult(pReply, test_key_hh_d);

    char *test_key_hh_i = "test_key_hh_i";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %hhi", test_key_hh_i, -161));
    CheckSetResult(pReply, test_key_hh_i);

    char *test_key_hh_o = "test_key_hh_o";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %hho", test_key_hh_o, -161));
    CheckSetResult(pReply, test_key_hh_o);

    char *test_key_hh_u = "test_key_hh_u";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %hhu", test_key_hh_u, -161));
    CheckSetResult(pReply, test_key_hh_u);

    char *test_key_hh_x = "test_key_hh_x";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %hhx", test_key_hh_x, -161));
    CheckSetResult(pReply, test_key_hh_x);

    char *test_key_hh_X = "test_key_hh_X";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %hhX", test_key_hh_X, -161));
    CheckSetResult(pReply, test_key_hh_X);


    char *test_key_h_d = "test_key_h_d";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %hd", test_key_h_d,  -161));
    CheckSetResult(pReply, test_key_h_d);

    char *test_key_h_i = "test_key_h_i";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %hi", test_key_h_i, -161));
    CheckSetResult(pReply, test_key_h_i);

    char *test_key_h_o = "test_key_h_o";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %ho", test_key_h_o, -161));
    CheckSetResult(pReply, test_key_h_o);

    char *test_key_h_u = "test_key_h_u";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %hu", test_key_h_u, -161));
    CheckSetResult(pReply, test_key_h_u);

    char *test_key_h_x = "test_key_h_x";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %hx", test_key_h_x, -161));
    CheckSetResult(pReply, test_key_h_x);

    char *test_key_h_X = "test_key_h_X";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %hX", test_key_h_X, -161));
    CheckSetResult(pReply, test_key_h_X);

    char *test_key_ll_d = "test_key_ll_d";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %lld", test_key_ll_d,  -161));
    CheckSetResult(pReply, test_key_h_d);

    char *test_key_ll_i = "test_key_ll_i";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %lli", test_key_ll_i,  -161));
    CheckSetResult(pReply, test_key_h_i);

    char *test_key_ll_o = "test_key_ll_o";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %llo", test_key_ll_o,  -161));
    CheckSetResult(pReply, test_key_h_o);

    char *test_key_ll_u = "test_key_ll_u";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %llu", test_key_ll_u,  -161));
    CheckSetResult(pReply, test_key_h_u);

    char *test_key_ll_x = "test_key_ll_x";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %llx", test_key_ll_x,  -161));
    CheckSetResult(pReply, test_key_h_x);

    char *test_key_ll_X = "test_key_ll_X";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %llX", test_key_ll_X,  -161));
    CheckSetResult(pReply, test_key_h_X);

    char *test_key_l_d = "test_key_l_d";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %ld", test_key_l_d,    -161));
    CheckSetResult(pReply, test_key_l_d);

    char *test_key_l_i = "test_key_l_i";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %li", test_key_l_i,   (long)-161));
    CheckSetResult(pReply, test_key_l_i);

    char *test_key_l_o = "test_key_l_o";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %lo", test_key_l_o,   -161));
    CheckSetResult(pReply, test_key_l_o);

    char *test_key_l_u = "test_key_l_u";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %lu", test_key_l_u,   -161));
    CheckSetResult(pReply, test_key_l_u);

    char *test_key_l_x = "test_key_l_x";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %lx", test_key_l_x,   -161));
    CheckSetResult(pReply, test_key_l_x);

    char *test_key_l_X = "test_key_l_X";
    pReply = reinterpret_cast<redisReply *>(redisCommand(pRedisConn, "SET %s %lX", test_key_l_X,   -161));
    CheckSetResult(pReply, test_key_l_X);

    printf("\n");
    GetKey(pRedisConn, test_key);
    printf("\n");
    GetKey(pRedisConn, test_key_b);



    printf("\n");
    GetKey(pRedisConn, test_key_hh_d);
    GetKey(pRedisConn, test_key_hh_i);
    GetKey(pRedisConn, test_key_hh_u);
    GetKey(pRedisConn, test_key_hh_o);
    GetKey(pRedisConn, test_key_hh_x);
    GetKey(pRedisConn, test_key_hh_X);

    printf("\n");
    GetKey(pRedisConn, test_key_h_d);
    GetKey(pRedisConn, test_key_h_i);
    GetKey(pRedisConn, test_key_h_u);
    GetKey(pRedisConn, test_key_h_o);
    GetKey(pRedisConn, test_key_h_x);
    GetKey(pRedisConn, test_key_h_X);

    printf("\n");
    GetKey(pRedisConn, test_key_ll_d);
    GetKey(pRedisConn, test_key_ll_i);
    GetKey(pRedisConn, test_key_ll_u);
    GetKey(pRedisConn, test_key_ll_o);
    GetKey(pRedisConn, test_key_ll_x);
    GetKey(pRedisConn, test_key_ll_X);

    printf("\n");
    GetKey(pRedisConn, test_key_l_d);
    GetKey(pRedisConn, test_key_l_i);
    GetKey(pRedisConn, test_key_l_u);
    GetKey(pRedisConn, test_key_l_o);
    GetKey(pRedisConn, test_key_l_x);
    GetKey(pRedisConn, test_key_l_X);


    redisFree(pRedisConn);
    return 1;
}

输出为:

connect redis server success!  

Get Key[test_key] value[teat value]  

Get Key[test_key_b] value[test value b]  

Get Key[test_key_hh_d] value[95]  
Get Key[test_key_hh_i] value[95]  
Get Key[test_key_hh_u] value[95]  
Get Key[test_key_hh_o] value[137]  
Get Key[test_key_hh_x] value[5f]  
Get Key[test_key_hh_X] value[5F]  

Get Key[test_key_h_d] value[-161]  
Get Key[test_key_h_i] value[-161]  
Get Key[test_key_h_u] value[65375]  
Get Key[test_key_h_o] value[177537]  
Get Key[test_key_h_x] value[ff5f]  
Get Key[test_key_h_X] value[FF5F]  

Get Key[test_key_ll_d] value[4294967135]  
Get Key[test_key_ll_i] value[4294967135]  
Get Key[test_key_ll_u] value[4294967135]  
Get Key[test_key_ll_o] value[37777777537]  
Get Key[test_key_ll_x] value[ffffff5f]   
Get Key[test_key_ll_X] value[FFFFFF5F]  

Get Key[test_key_l_d] value[4294967135]  
Get Key[test_key_l_i] value[-161]  
Get Key[test_key_l_u] value[4294967135]  
Get Key[test_key_l_o] value[37777777537]  
Get Key[test_key_l_x] value[ffffff5f]   
Get Key[test_key_l_X] value[FFFFFF5F]  

细心的各位一定注意到了,输入都为-161,但是只有一个原样输出了-161,并且那个是强制转换了成了long型才输出的。这个就涉及到了类型转换了。

基本原则如下:

最后一张图说明:


总结

面对问题,想着回避,不去解决总结,总有一天又会再次遇见。自己偷懒挖的坑还得自己填。

上一篇下一篇

猜你喜欢

热点阅读