Chan Chen Coding...

          memcached協(xié)議

          在memcache協(xié)議中發(fā)送的數(shù)據(jù)分兩種:文本行 和 自由數(shù)據(jù)。 文本行被用于來自客戶端的命令和服務(wù)器的回應(yīng)。自由數(shù)據(jù)用于客戶端從服務(wù)器端存取數(shù)據(jù)時。存儲在memcached中的數(shù)據(jù)通過鍵值來標識。鍵值是一個文本字符串,對于需要存取這項數(shù)據(jù)的客戶端而言,它必須是唯一的。

          協(xié)議
          Protocol
          memcached 的客戶端使用TCP鏈接 與 服務(wù)器通訊。(UDP接口也同樣有效,參考后文的 “UDP協(xié)議” )一個運行中的memcached服務(wù)器監(jiān)視一些(可設(shè)置)端口。客戶端連接這些端口,發(fā)送命令到服務(wù)器,讀取回應(yīng),最后關(guān)閉連接。Clients of memcached communicate with server through TCP connections. (A UDP interface is also available; details are below under “UDP protocol.”) A given running memcached server listens on some (configurable) port; clients connect to that port, send commands to the server, read responses, and eventually close the connection.
          結(jié)束會話不需要發(fā)送任何命令。當不再需memcached服務(wù)時,要客戶端可以在任何時候關(guān)閉連接。需要注意的是,鼓勵客戶端緩存這些連接,而不是每次需要存取數(shù)據(jù)時都重新打開連接。這是因為memcached 被特意設(shè)計成及時開啟很多連接也能夠高效的工作(數(shù)百個,上千個如果需要的話)。緩存這些連接,可以消除建立連接所帶來的開銷(/*/相對而言,在服務(wù)器端建立一個新連接的準備工作所帶來的開銷,可以忽略不計。)。There is no need to send any command to end the session. A client may just close the connection at any moment it no longer needs it. Note, however, that clients are encouraged to cache their connections rather than reopen them every time they need to store or retrieve data. This is because memcached is especially designed to work very efficiently with a very large number (many hundreds, more than a thousand if necessary) of open connections. Caching connections will eliminate the overhead associated with establishing a TCP connection (the overhead of preparing for a new connection on the server side is insignificant compared to this).
          在memcache協(xié)議中發(fā)送的數(shù)據(jù)分兩種:文本行 和 自由數(shù)據(jù)。 文本行被用于來自客戶端的命令和服務(wù)器的回應(yīng)。自由數(shù)據(jù)用于客戶端從服務(wù)器端存取數(shù)據(jù)時。同樣服務(wù)器會以字節(jié)流的方式傳回自由數(shù)據(jù)。/*/服務(wù)器不用關(guān)心自由數(shù)據(jù)的字節(jié)順序。自由數(shù)據(jù)的特征沒有任何限制;但是通過前文提到的文本行,這項數(shù)據(jù)的接受者(服務(wù)器或客戶端),便能夠精確地獲知所發(fā)送的數(shù)據(jù)庫的長度。There are two kinds of data sent in the memcache protocol: text lines
          and unstructured data. Text lines are used for commands from clients
          and responses from servers. Unstructured data is sent when a client
          wants to store or retrieve data. The server will transmit back
          unstructured data in exactly the same way it received it, as a byte
          stream. The server doesn’t care about byte order issues in
          unstructured data and isn’t aware of them. There are no limitations on
          characters that may appear in unstructured data; however, the reader
          of such data (either a client or a server) will always know, from a
          preceding text line, the exact length of the data block being
          transmitted.
          文本行固定以“\r\n”(回車符緊跟一個換行符)結(jié)束。 自由數(shù)據(jù)也是同樣會以“\r\n”結(jié)束,但是 \r(回車符)、\n(換行符),以及任何其他8位字符,均可出現(xiàn)在數(shù)據(jù)中。因此,當客戶端從服務(wù)器取回數(shù)據(jù)時,必須使用數(shù)據(jù)區(qū)塊的長度來確定數(shù)據(jù)區(qū)塊的結(jié)束位置,而不要依據(jù)數(shù)據(jù)區(qū)塊末尾的“\r\n”,即使它們固定存在于此。Text lines are always terminated by \r\n. Unstructured data is _also_
          terminated by \r\n, even though \r, \n or any other 8-bit characters
          may also appear inside the data. Therefore, when a client retrieves
          data from a server, it must use the length of the data block (which it
          will be provided with) to determine where the data block ends, and not
          the fact that \r\n follows the end of the data block, even though it
          does.
            
          鍵值
          Keys
          存儲在memcached中的數(shù)據(jù)通過鍵值來標識。鍵值是一個文本字符串,對于需要存取這項數(shù)據(jù)的客戶端而言,它必須是唯一的。鍵值當前的長度限制設(shè)定為250字符(當然,客戶端通常不會用到這么長的鍵);鍵值中不能使用制表符和其他空白字符(例如空格,換行等)。Data stored by memcached is identified with the help of a key. A key
          is a text string which should uniquely identify the data for clients
          that are interested in storing and retrieving it. Currently the
          length limit of a key is set at 250 characters (of course, normally
          clients wouldn’t need to use such long keys); the key must not include
          control characters or whitespace.
            
          命令
          Commands
          所有命令分為3種類型There are three types of commands.
          存儲命令(有3項:’set’、’add’、’repalce’)指示服務(wù)器儲存一些由鍵值標識的數(shù)據(jù)。客戶端發(fā)送一行命令,后面跟著數(shù)據(jù)區(qū)塊;然后,客戶端等待接收服務(wù)器回傳的命令行,指示成功與否。Storage commands (there are three: “set”, “add” and “replace”) ask the
          server to store some data identified by a key. The client sends a
          command line, and then a data block; after that the client expects one
          line of response, which will indicate success or faulure.
          取回命令(只有一項:’get’)指示服務(wù)器返回與所給鍵值相符合的數(shù)據(jù)(一個請求中右一個或多個鍵值)。客戶端發(fā)送一行命令,包括所有請求的鍵值;服務(wù)器每找到一項內(nèi)容,都會發(fā)送回客戶端一行關(guān)于這項內(nèi)容的信息,緊跟著是對應(yīng)的數(shù)據(jù)區(qū)塊;直到服務(wù)器以一行“END”回應(yīng)命令結(jié)束。Retrieval commands (there is only one: “get”) ask the server to
          retrieve data corresponding to a set of keys (one or more keys in one
          request). The client sends a command line, which includes all the
          requested keys; after that for each item the server finds it sends to
          the client one response line with information about the item, and one
          data block with the item’s data; this continues until the server
          finished with the “END” response line.
          /*?*/其他的命令都不能攜帶自由數(shù)據(jù)。在這些命令中,客戶端發(fā)送一行命令,然后等待(由命令所決定)一行回應(yīng),或最終以一行“END”結(jié)束的多行命令。All other commands don’t involve unstructured data. In all of them,
          the client sends one command line, and expects (depending on the
          command) either one line of response, or several lines of response
          ending with “END” on the last line.
          一行命令固定以命令名稱開始,接著是以空格隔開的參數(shù)(如果有參數(shù)的話)。命令名稱大小寫敏感,并且必須小寫。A command line always starts with the name of the command, followed by
          parameters (if any) delimited by whitespace. Command names are
          lower-case and are case-sensitive.
            
          一些客戶端發(fā)送給服務(wù)器的命令會包含一些時限(針對內(nèi)容或客戶端請求的操作)。這時,時限的具體內(nèi)容既可以是Unix時間戳(從1970年1月1日開始的秒鐘數(shù)),或當前時間開始的秒鐘數(shù)。對后者而言,不能超過 60*60*24*30(30天);如果超出,服務(wù)器將會理解為Unix時間戳,而不是從當前時間起的秒偏移。Some commands involve a client sending some kind of expiration time
          (relative to an item or to an operation requested by the client) to
          the server. In all such cases, the actual value sent may either be
          Unix time (number of seconds since January 1, 1970, as a 32-bit
          value), or a number of seconds starting from current time. In the
          latter case, this number of seconds may not exceed 60*60*24*30 (number
          of seconds in 30 days); if the number sent by a client is larger than
          that, the server will consider it to be real Unix time value rather
          than an offset from current time.
            
          錯誤字串
          Error strings
          每一個由客戶端發(fā)送的命令,都可能收到來自服務(wù)器的錯誤字串回復(fù)。這些錯誤字串會以三種形式出現(xiàn):Each command sent by a client may be answered with an error string
          from the server. These error strings come in three types:
          - “ERROR\r\n”
          意味著客戶端發(fā)送了不存在的命令名稱。means the client sent a nonexistent command name.
          - “CLIENT_ERROR <error>\r\n”
          意味著輸入的命令行里存在一些客戶端錯誤,例如輸入未遵循協(xié)議。<error>部分是人類易于理解的錯誤解說……means some sort of client error in the input line, i.e. the input
          doesn’t conform to the protocol in some way. <error> is a
          human-readable error string.
          - “SERVER_ERROR <error>\r\n”
          意味著一些服務(wù)器錯誤,導(dǎo)致命令無法執(zhí)行。<error>部分是人類易于理解的錯誤解說。在一些嚴重的情形下(通常應(yīng)該不會遇到),服務(wù)器將在發(fā)送這行錯誤后關(guān)閉連接。這是服務(wù)器主動關(guān)閉連接的唯一情況。means some sort of server error prevents the server from carrying
          out the command. <error> is a human-readable error string. In cases
          of severe server errors, which make it impossible to continue
          serving the client (this shouldn’t normally happen), the server will
          close the connection after sending the error line. This is the only
          case in which the server closes a connection to a client.
          在后面每項命令的描述中,這些錯誤行不會再特別提到,但是客戶端必須考慮到這些它們存在的可能性。In the descriptions of individual commands below, these error lines
          are not again specifically mentioned, but clients must allow for their
          possibility.
            
          存儲命令
          Storage commands
          首先,客戶端會發(fā)送一行像這樣的命令:First, the client sends a command line which looks like this:
          <command name> <key> <flags> <exptime> <bytes>\r\n
          - <command name> 是 set, add, 或者 repalce- <command name> is “set”, “add” or “replace”
          • set 意思是 “儲存此數(shù)據(jù)”
          • add 意思是 “儲存此數(shù)據(jù),只在服務(wù)器*未*保留此鍵值的數(shù)據(jù)時”
          • replace意思是 “儲存此數(shù)據(jù),只在服務(wù)器*曾*保留此鍵值的數(shù)據(jù)時”
          • “set” means “store this data”.
          • “add” means “store this data, but only if the server *doesn’t* already
            hold data for this key”.
          • “replace” means “store this data, but only if the server *does*
            already hold data for this key”.
          - <key> 是接下來的客戶端所要求儲存的數(shù)據(jù)的鍵值- <key> is the key under which the client asks to store the data
          - <flags> 是在取回內(nèi)容時,與數(shù)據(jù)和發(fā)送塊一同保存服務(wù)器上的任意16位無符號整形(用十進制來書寫)。客戶端可以用它作為“位域”來存儲一些特定的信息;它對服務(wù)器是不透明的。- <flags> is an arbitrary 16-bit unsigned integer (written out in
          decimal) that the server stores along with the data and sends back
          when the item is retrieved. Clients may use this as a bit field to
          store data-specific information; this field is opaque to the server.
          - <exptime> 是終止時間。如果為0,該項永不過期(雖然它可能被刪除,以便為其他緩存項目騰出位置)。如果非0(Unix時間戳或當前時刻的秒偏移),到達終止時間后,客戶端無法再獲得這項內(nèi)容。- <exptime> is expiration time. If it’s 0, the item never expires
          (although it may be deleted from the cache to make place for other
          items). If it’s non-zero (either Unix time or offset in seconds from
          current time), it is guaranteed that clients will not be able to
          retrieve this item after the expiration time arrives (measured by
          server time).
          - <bytes> 是隨后的數(shù)據(jù)區(qū)塊的字節(jié)長度,不包括用于分野的“\r\n”。它可以是0(這時后面跟隨一個空的數(shù)據(jù)區(qū)塊)。- <bytes> is the number of bytes in the data block to follow, *not*
          including the delimiting \r\n. <bytes> may be zero (in which case
          it’s followed by an empty data block).
            
          在這一行以后,客戶端發(fā)送數(shù)據(jù)區(qū)塊。After this line, the client sends the data block:
          <data block>\r\n
          - <data block> 是大段的8位數(shù)據(jù),其長度由前面的命令行中的<bytes>指定。- <data block> is a chunk of arbitrary 8-bit data of length <bytes>
          from the previous line.
          發(fā)送命令行和數(shù)據(jù)區(qū)塊以后,客戶端等待回復(fù),可能的回復(fù)如下:After sending the command line and the data blockm the client awaits
          the reply, which may be:
          - “STORED\r\n”
          表明成功.to indicate success.
          - “NOT_STORED\r\n”
          表明數(shù)據(jù)沒有被存儲,但不是因為發(fā)生錯誤。這通常意味著add 或 replace命令的條件不成立,或者,項目已經(jīng)位列刪除隊列(參考后文的“delete”命令)。to indicate the data was not stored, but not
          because of an error. This normally means that either that the
          condition for an “add” or a “replace” command wasn’t met, or that the
          item is in a delete queue (see the “delete” command below).
            
          取回命令
          Retrieval command
          一行取回命令如下:The retrieval command looks like this:
          get <key>*\r\n
          - <key>* 表示一個或多個鍵值,由空格隔開的字串- <key>* means one or more key strings separated by whitespace.
          這行命令以后,客戶端的等待0個或多個項目,每項都會收到一行文本,然后跟著數(shù)據(jù)區(qū)塊。所有項目傳送完畢后,服務(wù)器發(fā)送以下字串:After this command, the client expects zero or more items, each of
          which is received as a text line followed by a data block. After all
          the items have been transmitted, the server sends the string
          “END\r\n”
          來指示回應(yīng)完畢。to indicate the end of response.
          服務(wù)器用以下形式發(fā)送每項內(nèi)容:Each item sent by the server looks like this:
          VALUE <key> <flags> <bytes>\r\n
          <data block>\r\n
          - <key> 是所發(fā)送的鍵名- <key> is the key for the item being sent
          - <flags> 是存儲命令所設(shè)置的記號- <flags> is the flags value set by the storage command
          - <bytes> 是隨后數(shù)據(jù)塊的長度,*不包括* 它的界定符“\r\n”- <bytes> is the length of the data block to follow, *not* including
          its delimiting \r\n
          - <data block> 是發(fā)送的數(shù)據(jù)- <data block> is the data for this item.
          如果在取回請求中發(fā)送了一些鍵名,而服務(wù)器沒有送回項目列表,這意味著服務(wù)器沒這些鍵名(可能因為它們從未被存儲,或者為給其他內(nèi)容騰出空間而被刪除,或者到期,或者被已客戶端刪除)。If some of the keys appearing in a retrieval request are not sent back
          by the server in the item list this means that the server does not
          hold items with such keys (because they were never stored, or stored
          but deleted to make space for more items, or expired, or explicitly
          deleted by a client).
            
          刪除
          Deletion
          命令“delete”允許從外部刪除內(nèi)容:The command “delete” allows for explicit deletion of items:
          delete <key> <time>\r\n
          - <key> 是客戶端希望服務(wù)器刪除的內(nèi)容的鍵名- <key> is the key of the item the client wishes the server to delete
          - <time> 是一個單位為秒的時間(或代表直到某一刻的Unix時間),在該時間內(nèi)服務(wù)器會拒絕對于此鍵名的“add”和“replace”命令。此時內(nèi)容被放入delete隊列,無法再通過“get”得到該內(nèi)容,也無法是用“add”和“replace”命令(但是“set”命令可用)。直到指定時間,這些內(nèi)容被最終從服務(wù)器的內(nèi)存中徹底清除。- <time> is the amount of time in seconds (or Unix time until which)
          the client wishes the server to refuse “add” and “replace” commands
          with this key. For this amount of item, the item is put into a
          delete queue, which means that it won’t possible to retrieve it by
          the “get” command, but “add” and “replace” command with this key
          will also fail (the “set” command will succeed, however). After the
          time passes, the item is finally deleted from server memory.
          <time>參數(shù) 是可選的,缺省為0(表示內(nèi)容會立刻清除,并且隨后的存儲命令均可用)。The parameter <time> is optional, and, if absent, defaults to 0
          (which means that the item will be deleted immediately and further
          storage commands with this key will succeed).
          此命令有一行回應(yīng):The response line to this command can be one of:
          - “DELETED\r\n”
          表示執(zhí)行成功to indicate success
          - “NOT_FOUND\r\n”
          表示沒有找到這項內(nèi)容to indicate that the item with this key was not found.
          參考隨后的“flush_all”命令使所有內(nèi)容無效See the “flush_all” command below for immediate invalidation
          of all existing items.
            
          增加/減少
          Increment/Decrement
          命令 “incr” 和 “decr”被用來修改數(shù)據(jù),當一些內(nèi)容需要 替換、增加 或減少時。這些數(shù)據(jù)必須是十進制的32位無符號整新。如果不是,則當作0來處理。修改的內(nèi)容必須存在,當使用“incr”/“decr”命令修改不存在的內(nèi)容時,不會被當作0處理,而是操作失敗。Commands “incr” and “decr” are used to change data for some item
          in-place, incrementing or decrementing it. The data for the item is
          treated as decimal representation of a 32-bit unsigned integer. If the
          current data value does not conform to such a representation, the
          commands behave as if the value were 0. Also, the item must already
          exist for incr/decr to work; these commands won’t pretend that a
          non-existent key exists with value 0; instead, they will fail.
          客戶端發(fā)送命令行:The client sends the command line:
          incr <key> <value>\r\n

          decr <key> <value>\r\n
          - <key> 是客戶端希望修改的內(nèi)容的建名- <key> is the key of the item the client wishes to change
          - <value> 是客戶端要增加/減少的總數(shù)。- <value> is the amount by which the client wants to increase/decrease
          the item. It is a decimal representation of a 32-bit unsigned integer.
          回復(fù)為以下集中情形:The response will be one of:
          - “NOT_FOUND\r\n”
          指示該項內(nèi)容的值,不存在。to indicate the item with this value was not found
          - <value>\r\n ,<value>是 增加/減少 。- <value>\r\n , where <value> is the new value of the item’s data,
          after the increment/decrement operation was carried out.
          注意”decr”命令發(fā)生下溢:如果客戶端嘗試減少的結(jié)果小于0時,結(jié)果會是0。”incr” 命令不會發(fā)生溢出。Note that underflow in the “decr” command is caught: if a client tries
          to decrease the value below 0, the new value will be 0. Overflow in
          the “incr” command is not checked.
          ……

           

          Note also that decrementing a number such that it loses length isn’t
          guaranteed to decrement its returned length. The number MAY be
          space-padded at the end, but this is purely an implementation
          optimization, so you also shouldn’t rely on that.
            
          狀態(tài)
          Statistics
          命令”stats” 被用于查詢服務(wù)器的運行狀態(tài)和其他內(nèi)部數(shù)據(jù)。有兩種格式。不帶參數(shù)的:The command “stats” is used to query the server about statistics it
          maintains and other internal data. It has two forms. Without
          arguments:
           stats\r\n
          這會在隨后輸出各項狀態(tài)、設(shè)定值和文檔。另一種格式帶有一些參數(shù):it causes the server to output general-purpose statistics and
          settings, documented below. In the other form it has some arguments:
          stats <args>\r\n
          通過<args>,服務(wù)器傳回各種內(nèi)部數(shù)據(jù)。因為隨時可能發(fā)生變動,本文不提供參數(shù)的種類及其傳回數(shù)據(jù)。Depending on <args>, various internal data is sent by the server. The
          kinds of arguments and the data sent are not documented in this vesion
          of the protocol, and are subject to change for the convenience of
          memcache developers.
            
          各種狀態(tài)
          General-purpose statistics
          受到無參數(shù)的”stats”命令后,服務(wù)器發(fā)送多行內(nèi)容,如下:Upon receiving the “stats” command without arguments, the server sents
          a number of lines which look like this:
          STAT <name> <value>\r\n
          服務(wù)器用以下一行來終止這個清單:The server terminates this list with the line
          END\r\n
          在每行狀態(tài)中,<name> 是狀態(tài)的名字,<value> 使狀態(tài)的數(shù)據(jù)。 以下清單,是所有的狀態(tài)名稱,數(shù)據(jù)類型,和數(shù)據(jù)代表的含義。In each line of statistics, <name> is the name of this statistic, and
          <value> is the data. The following is the list of all names sent in
          response to the “stats” command, together with the type of the value
          sent for this name, and the meaning of the value.
          在“類型”一列中,”32u”表示32位無符號整型,”64u”表示64位無符號整型,”32u:32u”表示用冒號隔開的兩個32位無符號整型。In the type column below, “32u” means a 32-bit unsigned integer, “64u”
          means a 64-bit unsigner integer. ‘32u:32u’ means two 32-but unsigned
          integers separated by a colon.
          名稱/Name類型/Type含義/Meaning
          pid32u服務(wù)器進程IDProcess id of this server process
          uptime32u服務(wù)器運行時間,單位秒Number of seconds this server has been running
          time32u服務(wù)器當前的UNIX時間current UNIX time according to the server
          versionstring服務(wù)器的版本號Version string of this server
          rusage_user32u:32u該進程累計的用戶時間
          (秒:微妙)
          Accumulated user time for this process
          (seconds:microseconds)
          rusage_system32u:32u該進程累計的系統(tǒng)時間
          (秒:微妙)
          Accumulated system time for this process
          (seconds:microseconds)
          curr_items32u服務(wù)器當前存儲的內(nèi)容數(shù)量Current number of items stored by the server
          total_items32u服務(wù)器啟動以來存儲過的內(nèi)容總數(shù)Total number of items stored by this server
          ever since it started
          bytes64u服務(wù)器當前存儲內(nèi)容所占用的字節(jié)數(shù)Current number of bytes used by this server
          to store items
          curr_connections32u連接數(shù)量Number of open connections
          total_connections32u服務(wù)器運行以來接受的連接總數(shù)Total number of connections opened since
          the server started running
          connection_structures32u服務(wù)器分配的連接結(jié)構(gòu)的數(shù)量Number of connection structures allocated
          by the server
          cmd_get32u取回請求總數(shù)Cumulative number of retrieval requests
          cmd_set32u存儲請求總數(shù)Cumulative number of storage requests
          get_hits32u請求成功的總次數(shù)Number of keys that have been requested and
          found present
          get_misses32u請求失敗的總次數(shù)Number of items that have been requested
          and not found
          bytes_read64u服務(wù)器從網(wǎng)絡(luò)讀取到的總字節(jié)數(shù)Total number of bytes read by this server
          from network
          bytes_written64u服務(wù)器向網(wǎng)絡(luò)發(fā)送的總字節(jié)數(shù)Total number of bytes sent by this server to
          network
          limit_maxbytes32u服務(wù)器在存儲時被允許使用的字節(jié)總數(shù)Number of bytes this server is allowed to
          use for storage.
            
          其它命令
          Other commands
          “flush_all”命令有一個可選的數(shù)字參數(shù)。它總是執(zhí)行成功,服務(wù)器會發(fā)送“OK\r\n”回應(yīng)。它的效果是使已經(jīng)存在的項目立即失效(缺省),或在指定的時間后。此后執(zhí)行取回命令,將不會有任何內(nèi)容返回(除非重新存儲同樣的鍵名)。flush_all 實際上沒有立即釋放項目所占用的內(nèi)存,而是在隨后陸續(xù)有新的項目被儲存時執(zhí)行。flush_all 效果具體如下:它導(dǎo)致所有更新時間早于flush_all所設(shè)定時間的項目,在被執(zhí)行取回命令時命令被忽略。“flush_all” is a command with an optional numeric argument. It always succeeds, and the server sends “OK\r\n” in response. Its effect is to invalidate all existing items immediately (by default) or after the expiration specified. After invalidation none of the items will be returned in response to a retrieval command (unless it’s stored again under the same key *after* flush_all has invalidated the items). flush_all doesn’t actually free all the memory taken up by existing items; that will happen gradually as new items are stored. The most precise definition of what flush_all does is the following: it causes all items whose update time is earlier than the time at which flush_all was set to be executed to be ignored for retrieval purposes.
          “version”命令沒有參數(shù):“version” is a command with no arguments:
          version\r\n
          在回應(yīng)中,服務(wù)器發(fā)送:In response, the server sends
          “VERSION <version>\r\n”
          <version> 是服務(wù)器的版本字串。where <version> is the version string for the server.
          “quit”命令沒有參數(shù):“quit” is a command with no arguments:
          quit\r\n
          接收此命令后,服務(wù)器關(guān)閉連接。不過,客戶端可以在不再需要時,簡單地關(guān)閉連接就行,并不一定需要發(fā)送這個命令。Upon receiving this command, the server closes the connection. However, the client may also simply close the connection when it no longer needs it, without issuing this command.
            
          UDP 協(xié)議
          UDP protocol
          當來自客戶端的連接數(shù)遠大于TCP連接的上限時,可以使用基于UDP的接口。UDP接口不能保證傳輸?shù)轿唬灾挥性诓灰蟪晒Φ牟僮髦惺褂茫槐热绫挥糜谝粋€“get”請求時,會因不當?shù)木彺嫣幚矶l(fā)生錯誤或回應(yīng)有遺失。For very large installations where the number of clients is high enough that the number of TCP connections causes scaling difficulties, there is also a UDP-based interface. The UDP interface does not provide guaranteed delivery, so should only be used for operations that aren’t required to succeed; typically it is used for “get” requests where a missing or incomplete response can simply be treated as a cache miss.
          每個UDP數(shù)據(jù)包都包含一個簡單的幀頭,數(shù)據(jù)之后的內(nèi)容與TCP協(xié)議的描述類似。在執(zhí)行所產(chǎn)生的數(shù)據(jù)流中,請求必須被包含在單獨的一個UDP數(shù)據(jù)包中,但是回應(yīng)可能跨越多個數(shù)據(jù)包。(只有“get”和“set”請求例外,跨越了多個數(shù)據(jù)包)Each UDP datagram contains a simple frame header, followed by data in the same format as the TCP protocol described above. In the current implementation, requests must be contained in a single UDP datagram, but responses may span several datagrams. (The only common requests that would span multiple datagrams are huge multi-key “get” requests and “set” requests, both of which are more suitable to TCP transport for reliability reasons anyway.)
          幀頭有8字節(jié)長,如下(均由16位整數(shù)組成,網(wǎng)絡(luò)字節(jié)順序,高位在前):The frame header is 8 bytes long, as follows (all values are 16-bit integers in network byte order, high byte first):
          • 0-1 請求ID
          • 2-3 序號
          • 4-5 該信息的數(shù)據(jù)包總數(shù)
          • 6-7 保留位,必須為0
          • 0-1 Request ID
          • 2-3 Sequence number
          • 4-5 Total number of datagrams in this message
          • 6-7 Reserved for future use; must be 0
          請求ID有客戶端提供。一般它會是一個從隨機基數(shù)開始的遞增值,不過客戶端想用什么樣的請求ID都可以。服務(wù)器的回應(yīng)會包含一個和請求中的同樣的ID。客戶端使用請求ID來區(qū)分每一個回應(yīng)。任何一個沒有請求ID的數(shù)據(jù)包,可能是之前的請求遭到延遲而造成的,應(yīng)該被丟棄。The request ID is supplied by the client. Typically it will be a monotonically increasing value starting from a random seed, but the client is free to use whatever request IDs it likes. The server’s response will contain the same ID as the incoming request. The client uses the request ID to differentiate between responses to outstanding requests if there are several pending from the same server; any datagrams with an unknown request ID are probably delayed responses to an earlier request and should be discarded.
          序號的返回是從0到n-1,n是該條信息的數(shù)據(jù)包數(shù)量。The sequence number ranges from 0 to n-1, where n is the total number of datagrams in the message. The client should concatenate the payloads of the datagrams for a given response in sequence number order; the resulting byte stream will contain a complete response in the same format as the TCP protocol (including terminating \r\n sequences).


          -----------------------------------------------------
          Silence, the way to avoid many problems;
          Smile, the way to solve many problems;

          posted on 2012-04-01 19:21 Chan Chen 閱讀(1053) 評論(0)  編輯  收藏 所屬分類: Architecture

          主站蜘蛛池模板: 垦利县| 安仁县| 灵武市| 阿克陶县| 怀集县| 西丰县| 博湖县| 义马市| 贵定县| 吉林省| 瑞金市| 汤阴县| 永寿县| 伊宁市| 彭阳县| 阿勒泰市| 固原市| 新乐市| 泸西县| 榕江县| 德阳市| 河南省| 平山县| 山阳县| 平阴县| 饶河县| 巧家县| 砀山县| 塔城市| 东山县| 青冈县| 大方县| 海宁市| 天水市| 广德县| 定西市| 镇沅| 洪洞县| 建阳市| 太湖县| 博白县|