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