📄 protocol.txt
字号:
Protocol--------Clients of memcached communicate with server through TCP connections.(A UDP interface is also available; details are below under "UDPprotocol.") A given running memcached server listens on some(configurable) port; clients connect to that port, send commands tothe server, read responses, and eventually close the connection.There is no need to send any command to end the session. A client mayjust close the connection at any moment it no longer needs it. Note,however, that clients are encouraged to cache their connections ratherthan reopen them every time they need to store or retrieve data. Thisis because memcached is especially designed to work very efficientlywith a very large number (many hundreds, more than a thousand ifnecessary) of open connections. Caching connections will eliminate theoverhead associated with establishing a TCP connection (the overheadof preparing for a new connection on the server side is insignificantcompared to this).There are two kinds of data sent in the memcache protocol: text linesand unstructured data. Text lines are used for commands from clientsand responses from servers. Unstructured data is sent when a clientwants to store or retrieve data. The server will transmit backunstructured data in exactly the same way it received it, as a bytestream. The server doesn't care about byte order issues inunstructured data and isn't aware of them. There are no limitations oncharacters that may appear in unstructured data; however, the readerof such data (either a client or a server) will always know, from apreceding text line, the exact length of the data block beingtransmitted.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 charactersmay also appear inside the data. Therefore, when a client retrievesdata from a server, it must use the length of the data block (which itwill be provided with) to determine where the data block ends, and notthe fact that \r\n follows the end of the data block, even though itdoes.Keys----Data stored by memcached is identified with the help of a key. A keyis a text string which should uniquely identify the data for clientsthat are interested in storing and retrieving it. Currently thelength limit of a key is set at 250 characters (of course, normallyclients wouldn't need to use such long keys); the key must not includecontrol characters or whitespace.Commands--------There are three types of commands. Storage commands (there are three: "set", "add" and "replace") ask theserver to store some data identified by a key. The client sends acommand line, and then a data block; after that the client expects oneline of response, which will indicate success or faulure.Retrieval commands (there is only one: "get") ask the server toretrieve data corresponding to a set of keys (one or more keys in onerequest). The client sends a command line, which includes all therequested keys; after that for each item the server finds it sends tothe client one response line with information about the item, and onedata block with the item's data; this continues until the serverfinished with the "END" response line.All other commands don't involve unstructured data. In all of them,the client sends one command line, and expects (depending on thecommand) either one line of response, or several lines of responseending with "END" on the last line.A command line always starts with the name of the command, followed byparameters (if any) delimited by whitespace. Command names arelower-case and are case-sensitive.Expiration times----------------Some commands involve a client sending some kind of expiration time(relative to an item or to an operation requested by the client) tothe server. In all such cases, the actual value sent may either beUnix time (number of seconds since January 1, 1970, as a 32-bitvalue), or a number of seconds starting from current time. In thelatter case, this number of seconds may not exceed 60*60*24*30 (numberof seconds in 30 days); if the number sent by a client is larger thanthat, the server will consider it to be real Unix time value ratherthan an offset from current time.Error strings-------------Each command sent by a client may be answered with an error stringfrom 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" 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" 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 linesare not again specifically mentioned, but clients must allow for theirpossibility.Storage commands----------------First, the client sends a command line which looks like this:<command name> <key> <flags> <exptime> <bytes>\r\n- <command name> is "set", "add" or "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> is the key under which the client asks to store the data- <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> 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> 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> 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 awaitsthe reply, which may be:- "STORED\r\n", to indicate success.- "NOT_STORED\r\n" to indicate the data was not stored, but notbecause of an error. This normally means that either that thecondition for an "add" or a "replace" command wasn't met, or that theitem is in a delete queue (see the "delete" command below).Retrieval command:------------------The retrieval command looks like this:get <key>*\r\n- <key>* means one or more key strings separated by whitespace.After this command, the client expects zero or more items, each ofwhich is received as a text line followed by a data block. After allthe 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> is the key for the item being sent- <flags> is the flags value set by the storage command- <bytes> is the length of the data block to follow, *not* including its delimiting \r\n- <data block> is the data for this item.If some of the keys appearing in a retrieval request are not sent backby the server in the item list this means that the server does nothold items with such keys (because they were never stored, or storedbut deleted to make space for more items, or expired, or explicitlydeleted by a client).Deletion--------The command "delete" allows for explicit deletion of items:delete <key> <time>\r\n- <key> is the key of the item the client wishes the server to delete- <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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -