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_STRCASECMPint 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