Redis 如何设置包含空格的Value

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 基本相同,甚至可以说是增强版的,下面一个个说明。

  • %s:输入一个字符串
  • %b:输入一个字符串的指定长度,当长度大于指定字符串时,超过的部分数据是未定义的。
  • %d:输入一个整数,这个与prinf类似。
    注意:还有其他类型的数据基本与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型才输出的。这个就涉及到了类型转换了。

基本原则如下:

  • 若参与运算的数据类型不同,则先转换成同一类型,然后进行运算。
  • 转换按数据长度增加的方向进行,以保证精度不降低。例如int型和long型运算时,先把int量转成long型后再进行运算。
  • 所有的浮点运算都是以双精度进行的,即使仅含float单精度量运算的表达式,也要先转换成double型,再作运算。
  • char型和short型参与运算时,必须先转换成int型。
  • 在赋值运算中,赋值号两边的数据类型不同时,需要把右边表达式的类型将转换为左边变量的类型。如果右边表达式的数据类型长度比左边长时,将丢失一部分数据,这样会降低精度。

最后一张图说明:


总结

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,497评论 18 139
  • 曾经,我为了提升工作效率和避免时间不被手机占用,将使用频率最高的微信「朋友圈」关掉,其实一直在关闭状态。 我的朋友...
    5d0ca103ab37阅读 465评论 0 0
  • 在四川游玩参观,看到人来人往,大家的脚步都很匆忙,忙着拍完这个景,赶着去下个点。 我在四处转着,遇见了这位老奶奶,...
    Miss了了阅读 327评论 4 4