2009年3月16日 星期一

memcached介绍

今天翻译了一小段,接下来会慢慢翻译完的。。。

protocol
-----------
协议

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的客户端通过TCP连接和服务端进行通信(UDP接口也是可用的,具体可以参考UDP协议)。已经运行memcached的server会 监听(配置的)端口;客户端连接那个端口,发送命令,读取回复,最终关闭连接。

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).
要终结一个对话,并不需要客户端发送任何的命令。客户端可以在它不再需要通信的时候关闭这个连接。当然,我们鼓励客户端将他们的连接缓存起来而不是当他们每次需要存储或者取数据的时候都重新打开一个连接,因为memcached设计的初衷就是为了提供一个高效的、支持大量连接的东东^_^。连接缓存化(caching connections)可以使得因为建立TCP连接而出现的过载现象消失。

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.
Memcache协议里面存在这两种可以被传输(发送)的数据:文本行(text lines)和未结构化数据(unstructure data)。文本行主要用于客户端向服务器发送命令和服务器返回信息给客户端。未结构化数据则是在客户端想要存取数据的时候才被使用(发送)。服务器会将未结构化数据以一种比特流的方式传输给客户端(这种方式和它从客户端接受数据的方式是一样的)。服务器并不关心比特流的所导致的未结构化数据,也不会意识到他未结构化的数据,未结构化数据中的字符也没有任何限制。当然,不管是客户端还是服务器来的数据,读者(reader)总是可以通过预先处理的文本行(可能例如client发送一个get_length(object)来获得这个数据块的长度,然后server处理以后直接返回长度,再传输数据)来得知被传输的数据块的长度。

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.
文本行总是由\r\n作为结尾。未结构化数据也可以使用\r\n,即使是是\r,\n甚至是任何8bit长的字符都能出现在未结构化数据中。所以当客户端从服务器获得数据的同时,它必须使用服务器返回的数据块长度来确定什么时候数据块结束了,而不是用\r\n来决定数据块的完结,虽然事实上我们就使用\r\n来作为数据块的末尾的。

Keys
----
键值
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.
存储在memcached中的数据是通过键值来识别的。所谓的键值就是唯一的文本字符串,这个字符串必须在用户存取数据的时候能唯一的确定数据。当前键值的长度被限制在250个字符,键值不能包括控制符和空格。


Commands
--------
命令行

There are three types of commands.
总共有三种命令
Storage commands (there are six: "set", "add", "replace", "append"
"prepend" and "cas") 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.

存储命令(总共有6个:set,add,replace,append,prepend和cas)是让服务器根据某个键值去存储数据。客户端首先发送一个命令,然后数据块;接着服务器会返回信息,告诉客户端时不时成功了。

Retrieval commands (there are two: "get" and "gets") 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.
读取命令(总共有两个:get和gets)让服务器根据一系列的键值去获得数据。客户端首先发送一个包含键值(可以有多个)的命令,之后对于服务器所找个的每个item,服务器会返回给客户端这个item的信息,然后是item的数据库,知道服务器返回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.
其他所有的命令都不包括未结构化数据。客户端先发送命令,然后期望获得一个或者多个回应,这些回应会以END作为结尾。

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.
命令行通常是以命令作为开头,后面跟着各种的参素。命令都是小写字母而且是大消息敏感的。

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) 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 \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. is a
human-readable error string.
意味着客户端在输入命令的时候出现了错误,例如输入没有在某些地方没有遵从协议。

- "SERVER_ERROR \r\n"

means some sort of server error prevents the server from carrying
out the command. 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:
首先,客户端发送一个类似于下面的命令
[noreply]\r\n
cas [noreply]\r\n

- is "set", "add", "replace", "append" or "prepend"
命令名称可以使set,add,replace,append或者prepend

"set" means "store this data".
Set是存储数据
"add" means "store this data, but only if the server *doesn't* already
hold data for this key".
Add是如果是这个键值没有对应的数据,那么可以存储这个数据

"replace" means "store this data, but only if the server *does*
already hold data for this key".
Replace是如果这个键值有对应的数据,那么可以替换这个数据

"append" means "add this data to an existing key after existing data".
Append是指在原有的数据后面添加

"prepend" means "add this data to an existing key before existing data".
Prepend是指在原有的数据前面添加

The append and prepend commands do not accept flags or exptime.
They update existing data portions, and ignore new flag and exptime
settings.
Append和prepend这两个命令都不接受标记(flasg)和过期时间(exptime)。
他们会更新现存数据,同时忽略新的标记和过期时间。

"cas" is a check and set operation which means "store this data but
only if no one else has updated since I last fetched it."
Cas是一个检查和设置操作,这个命令仅仅在数据被我取出以后,没有人更新过这个数据的时候,才可以存储数据,也就是说如果A取了这个数据,B更新了,A就不能存储了。

- is the key under which the client asks to store the data
是客户端要求存储数据时用的键值

- 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.
Note that in memcached 1.2.1 and higher, flags may be 32-bits, instead
of 16, but you might want to restrict yourself to 16 bits for
compatibility with older versions.
是一个任意的16位的无符号整数(用10进制表示),当客户端取数据的时候,服务器会将这个flag和数据一起传输给客户端。客户端也许会用这个作为1bit的字段来存储数据的特定信息,这个字段对服务器是不可见的。在1.2.1以后的版本,flags可能会是32位的,为了兼容,可能还是要使用16位的flags

- 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).
就是过期时间。如果它的值是0,那么永不过期(当然可能因为内存没有空间导致这个item被删除)。如果不是0而且过期了,客户端就不能在获得这个数据了。

- is the number of bytes in the data block to follow, *not*
including the delimiting \r\n. may be zero (in which case
it's followed by an empty data block).

- is a unique 64-bit value of an existing entry.
Clients should use the value returned from the "gets" command
when issuing "cas" updates.

- "noreply" optional parameter instructs the server to not send the
reply. NOTE: if the request line is malformed, the server can't
parse "noreply" option reliably. In this case it may send the error
to the client, and not reading it on the client side will break
things. Client should construct only valid requests.
Noreply选项可以上服务器不返回任何消息。

After this line, the client sends the data block:
命令行过后,客户端就开始发送数据块了:

\r\n

- is a chunk of arbitrary 8-bit data of length
from the previous line.
数据块是长度为8bit的数据

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" 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).

- "EXISTS\r\n" to indicate that the item you are trying to store with
a "cas" command has been modified since you last fetched it.
数据自从被你取出来以后被其他人修改过了,所以不能在更新了。

- "NOT_FOUND\r\n" to indicate that the item you are trying to store
with a "cas" command did not exist or has been deleted.
数据不存在了,可能被删掉了。

Retrieval command:
------------------

The retrieval commands "get" and "gets" operates like this:

get *\r\n
gets *\r\n

- * means one or more key strings separated by whitespace.

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:
这个是item的表现形式:

VALUE []\r\n
\r\n

- is the key for the item being sent

- is the flags value set by the storage command

- is the length of the data block to follow, *not* including
its delimiting \r\n

- is a unique 64-bit integer that uniquely identifies
this specific item.
一个64位长度的整型来唯一标示item

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

The command "delete" allows for explicit deletion of items:

delete [

1 則留言:

  1. http://www.ccvita.com/306.html
    这个地方貌似有人整理过了。。。比较全。

    回覆刪除