協議 | Protocol |
memcached 的客戶端使用TCP鏈接 與 服務器通訊。(UDP接口也同樣有效,參考后文的 “UDP協議” )一個運行中的memcached服務器監視一些(可設置)端口。客戶端連接這些端口,發送命令到服務器,讀取回應,最后關閉連接。 | 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. |
結束會話不需要發送任何命令。當不再需memcached服務時,要客戶端可以在任何時候關閉連接。需要注意的是,鼓勵客戶端緩存這些連接,而不是每次需要存取數據時都重新打開連接。這是因為memcached 被特意設計成及時開啟很多連接也能夠高效的工作(數百個,上千個如果需要的話)。緩存這些連接,可以消除建立連接所帶來的開銷(/*/相對而言,在服務器端建立一個新連接的準備工作所帶來的開銷,可以忽略不計。)。 | 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協議中發送的數據分兩種:文本行 和 自由數據。 文本行被用于來自客戶端的命令和服務器的回應。自由數據用于客戶端從服務器端存取數據時。同樣服務器會以字節流的方式傳回自由數據。/*/服務器不用關心自由數據的字節順序。自由數據的特征沒有任何限制;但是通過前文提到的文本行,這項數據的接受者(服務器或客戶端),便能夠精確地獲知所發送的數據庫的長度。 | 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”(回車符緊跟一個換行符)結束。 自由數據也是同樣會以“\r\n”結束,但是 \r(回車符)、\n(換行符),以及任何其他8位字符,均可出現在數據中。因此,當客戶端從服務器取回數據時,必須使用數據區塊的長度來確定數據區塊的結束位置,而不要依據數據區塊末尾的“\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中的數據通過鍵值來標識。鍵值是一個文本字符串,對于需要存取這項數據的客戶端而言,它必須是唯一的。鍵值當前的長度限制設定為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’)指示服務器儲存一些由鍵值標識的數據。客戶端發送一行命令,后面跟著數據區塊;然后,客戶端等待接收服務器回傳的命令行,指示成功與否。 | 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’)指示服務器返回與所給鍵值相符合的數據(一個請求中右一個或多個鍵值)。客戶端發送一行命令,包括所有請求的鍵值;服務器每找到一項內容,都會發送回客戶端一行關于這項內容的信息,緊跟著是對應的數據區塊;直到服務器以一行“END”回應命令結束。 | 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. |
/*?*/其他的命令都不能攜帶自由數據。在這些命令中,客戶端發送一行命令,然后等待(由命令所決定)一行回應,或最終以一行“END”結束的多行命令。 | 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. |
一行命令固定以命令名稱開始,接著是以空格隔開的參數(如果有參數的話)。命令名稱大小寫敏感,并且必須小寫。 | 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. |
| |
一些客戶端發送給服務器的命令會包含一些時限(針對內容或客戶端請求的操作)。這時,時限的具體內容既可以是Unix時間戳(從1970年1月1日開始的秒鐘數),或當前時間開始的秒鐘數。對后者而言,不能超過 60*60*24*30(30天);如果超出,服務器將會理解為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 |
每一個由客戶端發送的命令,都可能收到來自服務器的錯誤字串回復。這些錯誤字串會以三種形式出現: | 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” |
意味著客戶端發送了不存在的命令名稱。 | means the client sent a nonexistent command name. |
- “CLIENT_ERROR <error>\r\n” |
意味著輸入的命令行里存在一些客戶端錯誤,例如輸入未遵循協議。<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” |
意味著一些服務器錯誤,導致命令無法執行。<error>部分是人類易于理解的錯誤解說。在一些嚴重的情形下(通常應該不會遇到),服務器將在發送這行錯誤后關閉連接。這是服務器主動關閉連接的唯一情況。 | 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 |
首先,客戶端會發送一行像這樣的命令: | 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 意思是 “儲存此數據”
- add 意思是 “儲存此數據,只在服務器*未*保留此鍵值的數據時”
- replace意思是 “儲存此數據,只在服務器*曾*保留此鍵值的數據時”
| - “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> 是接下來的客戶端所要求儲存的數據的鍵值 | - <key> is the key under which the client asks to store the data |
- <flags> 是在取回內容時,與數據和發送塊一同保存服務器上的任意16位無符號整形(用十進制來書寫)。客戶端可以用它作為“位域”來存儲一些特定的信息;它對服務器是不透明的。 | - <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時間戳或當前時刻的秒偏移),到達終止時間后,客戶端無法再獲得這項內容。 | - <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> 是隨后的數據區塊的字節長度,不包括用于分野的“\r\n”。它可以是0(這時后面跟隨一個空的數據區塊)。 | - <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). |
| |
在這一行以后,客戶端發送數據區塊。 | After this line, the client sends the data block: |
<data block>\r\n |
- <data block> 是大段的8位數據,其長度由前面的命令行中的<bytes>指定。 | - <data block> is a chunk of arbitrary 8-bit data of length <bytes> from the previous line. |
發送命令行和數據區塊以后,客戶端等待回復,可能的回復如下: | 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” |
表明數據沒有被存儲,但不是因為發生錯誤。這通常意味著add 或 replace命令的條件不成立,或者,項目已經位列刪除隊列(參考后文的“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個或多個項目,每項都會收到一行文本,然后跟著數據區塊。所有項目傳送完畢后,服務器發送以下字串: | 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” |
來指示回應完畢。 | to indicate the end of response. |
服務器用以下形式發送每項內容: | Each item sent by the server looks like this: |
VALUE <key> <flags> <bytes>\r\n <data block>\r\n |
- <key> 是所發送的鍵名 | - <key> is the key for the item being sent |
- <flags> 是存儲命令所設置的記號 | - <flags> is the flags value set by the storage command |
- <bytes> 是隨后數據塊的長度,*不包括* 它的界定符“\r\n” | - <bytes> is the length of the data block to follow, *not* including its delimiting \r\n |
- <data block> 是發送的數據 | - <data block> is the data for this item. |
如果在取回請求中發送了一些鍵名,而服務器沒有送回項目列表,這意味著服務器沒這些鍵名(可能因為它們從未被存儲,或者為給其他內容騰出空間而被刪除,或者到期,或者被已客戶端刪除)。 | 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”允許從外部刪除內容: | The command “delete” allows for explicit deletion of items: |
delete <key> <time>\r\n |
- <key> 是客戶端希望服務器刪除的內容的鍵名 | - <key> is the key of the item the client wishes the server to delete |
- <time> 是一個單位為秒的時間(或代表直到某一刻的Unix時間),在該時間內服務器會拒絕對于此鍵名的“add”和“replace”命令。此時內容被放入delete隊列,無法再通過“get”得到該內容,也無法是用“add”和“replace”命令(但是“set”命令可用)。直到指定時間,這些內容被最終從服務器的內存中徹底清除。 | - <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>參數 是可選的,缺省為0(表示內容會立刻清除,并且隨后的存儲命令均可用)。 | 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). |
此命令有一行回應: | The response line to this command can be one of: |
- “DELETED\r\n” |
表示執行成功 | to indicate success |
- “NOT_FOUND\r\n” |
表示沒有找到這項內容 | to indicate that the item with this key was not found. |
參考隨后的“flush_all”命令使所有內容無效 | See the “flush_all” command below for immediate invalidation of all existing items. |
| |
增加/減少 | Increment/Decrement |
命令 “incr” 和 “decr”被用來修改數據,當一些內容需要 替換、增加 或減少時。這些數據必須是十進制的32位無符號整新。如果不是,則當作0來處理。修改的內容必須存在,當使用“incr”/“decr”命令修改不存在的內容時,不會被當作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. |
客戶端發送命令行: | The client sends the command line: |
incr <key> <value>\r\n 或 decr <key> <value>\r\n |
- <key> 是客戶端希望修改的內容的建名 | - <key> is the key of the item the client wishes to change |
- <value> 是客戶端要增加/減少的總數。 | - <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. |
回復為以下集中情形: | The response will be one of: |
- “NOT_FOUND\r\n” |
指示該項內容的值,不存在。 | 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”命令發生下溢:如果客戶端嘗試減少的結果小于0時,結果會是0。”incr” 命令不會發生溢出。 | 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. |
| |
狀態 | Statistics |
命令”stats” 被用于查詢服務器的運行狀態和其他內部數據。有兩種格式。不帶參數的: | 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 |
這會在隨后輸出各項狀態、設定值和文檔。另一種格式帶有一些參數: | 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>,服務器傳回各種內部數據。因為隨時可能發生變動,本文不提供參數的種類及其傳回數據。 | 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. |
| |
各種狀態 | General-purpose statistics |
受到無參數的”stats”命令后,服務器發送多行內容,如下: | Upon receiving the “stats” command without arguments, the server sents a number of lines which look like this: |
STAT <name> <value>\r\n |
服務器用以下一行來終止這個清單: | The server terminates this list with the line |
END\r\n |
在每行狀態中,<name> 是狀態的名字,<value> 使狀態的數據。 以下清單,是所有的狀態名稱,數據類型,和數據代表的含義。 | 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 | pid | 32u | 服務器進程ID | Process id of this server process | uptime | 32u | 服務器運行時間,單位秒 | Number of seconds this server has been running | time | 32u | 服務器當前的UNIX時間 | current UNIX time according to the server | version | string | 服務器的版本號 | Version string of this server | rusage_user | 32u:32u | 該進程累計的用戶時間 (秒:微妙) | Accumulated user time for this process (seconds:microseconds) | rusage_system | 32u:32u | 該進程累計的系統時間 (秒:微妙) | Accumulated system time for this process (seconds:microseconds) | curr_items | 32u | 服務器當前存儲的內容數量 | Current number of items stored by the server | total_items | 32u | 服務器啟動以來存儲過的內容總數 | Total number of items stored by this server ever since it started | bytes | 64u | 服務器當前存儲內容所占用的字節數 | Current number of bytes used by this server to store items | curr_connections | 32u | 連接數量 | Number of open connections | total_connections | 32u | 服務器運行以來接受的連接總數 | Total number of connections opened since the server started running | connection_structures | 32u | 服務器分配的連接結構的數量 | Number of connection structures allocated by the server | cmd_get | 32u | 取回請求總數 | Cumulative number of retrieval requests | cmd_set | 32u | 存儲請求總數 | Cumulative number of storage requests | get_hits | 32u | 請求成功的總次數 | Number of keys that have been requested and found present | get_misses | 32u | 請求失敗的總次數 | Number of items that have been requested and not found | bytes_read | 64u | 服務器從網絡讀取到的總字節數 | Total number of bytes read by this server from network | bytes_written | 64u | 服務器向網絡發送的總字節數 | Total number of bytes sent by this server to network | limit_maxbytes | 32u | 服務器在存儲時被允許使用的字節總數 | Number of bytes this server is allowed to use for storage. |
|
|
| |
其它命令 | Other commands |
“flush_all”命令有一個可選的數字參數。它總是執行成功,服務器會發送“OK\r\n”回應。它的效果是使已經存在的項目立即失效(缺省),或在指定的時間后。此后執行取回命令,將不會有任何內容返回(除非重新存儲同樣的鍵名)。flush_all 實際上沒有立即釋放項目所占用的內存,而是在隨后陸續有新的項目被儲存時執行。flush_all 效果具體如下:它導致所有更新時間早于flush_all所設定時間的項目,在被執行取回命令時命令被忽略。 | “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”命令沒有參數: | “version” is a command with no arguments: |
version\r\n |
在回應中,服務器發送: | In response, the server sends |
“VERSION <version>\r\n” |
<version> 是服務器的版本字串。 | where <version> is the version string for the server. |
“quit”命令沒有參數: | “quit” is a command with no arguments: |
quit\r\n |
接收此命令后,服務器關閉連接。不過,客戶端可以在不再需要時,簡單地關閉連接就行,并不一定需要發送這個命令。 | 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 協議 | UDP protocol |
當來自客戶端的連接數遠大于TCP連接的上限時,可以使用基于UDP的接口。UDP接口不能保證傳輸到位,所以只有在不要求成功的操作中使用;比如被用于一個“get”請求時,會因不當的緩存處理而發生錯誤或回應有遺失。 | 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數據包都包含一個簡單的幀頭,數據之后的內容與TCP協議的描述類似。在執行所產生的數據流中,請求必須被包含在單獨的一個UDP數據包中,但是回應可能跨越多個數據包。(只有“get”和“set”請求例外,跨越了多個數據包) | 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字節長,如下(均由16位整數組成,網絡字節順序,高位在前): | 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 該信息的數據包總數
- 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有客戶端提供。一般它會是一個從隨機基數開始的遞增值,不過客戶端想用什么樣的請求ID都可以。服務器的回應會包含一個和請求中的同樣的ID。客戶端使用請求ID來區分每一個回應。任何一個沒有請求ID的數據包,可能是之前的請求遭到延遲而造成的,應該被丟棄。 | 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是該條信息的數據包數量。 | 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). |