Redis通訊協定最佳化

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.