Redis通信协议优化

1、命令简化

分析:redis通信协议中的命令,用的是原始的set、get、hset、hget等字符串,可以用0x01、0x02、0x03、0x04等单字节代替。

好处:节省网络传输流量,减少dump文件和aof文件的大小。

坏处:不易阅读(这个好象不是问题。。。)。

2、命令分隔符简化

分析:redis通信协议中的命令分隔符,用的是"\r\n",同HTTP协议,可以用"\r"或"\n"代替。

好处:节省网络传输流量,减少dump文件和aof文件的大小。

坏处:好象没有。

3、命令大小写优化(这与1有关,如果1做了,就不会有此条)

分析:redis通信协议文档中,并未说明协议中命令用大写好,还是小写好,但仔细阅读其源代码,会发现,用小写最好。

好处:减少大写转小写次数,加速命令查找。

坏处:无。

参考:

// 以下代码来自redis.c

/* A case insensitive version used for the command lookup table. */
int dictSdsKeyCaseCompare(void *privdata, const void *key1,
        const void *key2)
{
    DICT_NOTUSED(privdata);

    return strcasecmp(key1, key2) == 0;
}


/* Command table. sds string -> command struct pointer. */
dictType commandTableDictType = {
    dictSdsCaseHash,           /* hash function */
    NULL,                      /* key dup */
    NULL,                      /* val dup */
    dictSdsKeyCaseCompare,     /* key compare */
    dictSdsDestructor,         /* key destructor */
    NULL                       /* val destructor */
};

void initServerConfig() {

    // ...

    /* Command table -- we intiialize it here as it is part of the
     * initial configuration, since command names may be changed via
     * redis.conf using the rename-command directive. */
    server.commands = dictCreate(&commandTableDictType,NULL);
    populateCommandTable();
    server.delCommand = lookupCommandByCString("del");
    server.multiCommand = lookupCommandByCString("multi");

    // ...
}


// 以下代码来自linux kernel 3.4.4内核中的lib/string.c文件

#ifndef __HAVE_ARCH_STRCASECMP
int strcasecmp(const char *s1, const char *s2)
{
	int c1, c2;

	do {
		c1 = tolower(*s1++);
		c2 = tolower(*s2++);
	} while (c1 == c2 && c1 != 0);
	return c1 - c2;
}
EXPORT_SYMBOL(strcasecmp);
#endif

关键函数strcasecmp比较的时候,是先将s1和s2中的对应字符转化为小写再比较的。

参考链接:

http://redis.io/topics/protocol

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值