LSX-blog

Feed Rss

【转载】Memcaches相关介绍信息

11.25.2011, Linux, by .

协议
memcached 的客户端使用TCP链接 与 服务器通讯。(UDP接口也同样有效,参考后文的 “UDP协议” )一个运行中的memcached服务器监视一些(可设置)端口。客户端连接这些端口,发送命令到服务器,读取回应,最后关闭连接。

结束会话不需要发送任何命令。当不再需memcached服务时,要客户端可以在任何时候关闭连接。需要注意的是,鼓励客户端缓存这些连接,而不是每次需要存取数据时都重新打开连接。这是因为memcached 被特意设计成及时开启很多连接也能够高效的工作(数百个,上千个如果需要的话)。缓存这些连接,可以消除建立连接所带来的开销(/*/相对而言,在服务器端建立一个新连接的准备工作所带来的开销,可以忽略不计。)。

在memcache协议中发送的数据分两种:文本行 和 自由数据。 文本行被用于来自客户端的命令和服务器的回应。自由数据用于客户端从服务器端存取数据时。同样服务器会以字节流的方式传回自由数据。/*/服务器不用关心自由数据的字节顺序。自由数据的特征没有任何限制;但是通过前文提到的文本行,这项数据的接受者(服务器或客户端),便能够精确地获知所发送的数据库的长度。

文本行固定以“\r\n”(回车符紧跟一个换行符)结束。 自由数据也是同样会以“\r\n”结束,但是 \r(回车符)、\n(换行符),以及任何其他8位字符,均可出现在数据中。因此,当客户端从服务器取回数据时,必须使用数据区块的长度来确定数据区块的结束位置,而不要依据数据区块末尾的“\r\n”,即使它们固定存在于此。

键值
存储在memcached中的数据通过键值来标识。键值是一个文本字符串,对于需要存取这项数据的客户端而言,它必须是唯一的。键值当前的长度限制设定为250字符(当然,客户端通常不会用到这么长的键);键值中不能使用制表符和其他空白字符(例如空格,换行等)。

命令
所有命令分为3种类型:
存储命令(有3项:’set’、’add’、’repalce’)指示服务器储存一些由键值标识的数据。客户端发送一行命令,后面跟着数据区块;然后,客户端等待接收服务器回传的命令行,指示成功与否。
取回命令(只有一项:’get’)指示服务器返回与所给键值相符合的数据(一个请求中右一个或多个键值)。客户端发送一行命令,包括所有请求的键值;服务器每找到一项内容,都会发送回客户端一行关于这项内容的信息,紧跟着是对应的数据区块;直到服务器以一行“END”回应命令结束。
/*?*/其他的命令都不能携带自由数据。在这些命令中,客户端发送一行命令,然后等待(由命令所决定)一行回应,或最终以一行“END”结束的多行命令。

一行命令固定以命令名称开始,接着是以空格隔开的参数(如果有参数的话)。命令名称大小写敏感,并且必须小写。一些客户端发送给服务器的命令会包含一些时限(针对内容或客户端请求的操作)。这时,时限的具体内容既可以是Unix时间戳(从1970年1月1日开始的秒钟数),或当前时间开始的秒钟数。对后者而言,不能超过 60*60*24*30(30天);如果超出,服务器将会理解为Unix时间戳,而不是从当前时间起的秒偏移。

错误字串
每一个由客户端发送的命令,都可能收到来自服务器的错误字串回复。这些错误字串会以三种形式出现:
– “ERROR\r\n”
意味着客户端发送了不存在的命令名称。
– “CLIENT_ERROR \r\n”
意味着输入的命令行里存在一些客户端错误,例如输入未遵循协议。部分是人类易于理解的错误解说……
– “SERVER_ERROR \r\n”
意味着一些服务器错误,导致命令无法执行。部分是人类易于理解的错误解说。在一些严重的情形下(通常应该不会遇到),服务器将在发送这行错误后关闭连接。这是服务器主动关闭连接的唯一情况。
在后面每项命令的描述中,这些错误行不会再特别提到,但是客户端必须考虑到这些它们存在的可能性。

存储命令
首先,客户端会发送一行像这样的命令:
\r\n
是 set, add, 或者 repalce

set 意思是 “储存此数据”
add 意思是 “储存此数据,只在服务器*未*保留此键值的数据时”
replace意思是 “储存此数据,只在服务器*曾*保留此键值的数据时”

– 是接下来的客户端所要求储存的数据的键值
– 是在取回内容时,与数据和发送块一同保存服务器上的任意16位无符号整形(用十进制来书写)。客户端可以用它作为“位域”来存储一些特定的信息;它对服务器是不透明的。
– 是终止时间。如果为0,该项永不过期(虽然它可能被删除,以便为其他缓存项目腾出位置)。如果非0(Unix时间戳或当前时刻的秒偏移),到达终止时间后,客户端无法再获得这项内容。
– 是随后的数据区块的字节长度,不包括用于分野的“\r\n”。它可以是0(这时后面跟随一个空的数据区块)。
在这一行以后,客户端发送数据区块。
\r\n
– 是大段的8位数据,其长度由前面的命令行中的指定。

发送命令行和数据区块以后,客户端等待回复,可能的回复如下:
– “STORED\r\n”
表明成功.
– “NOT_STORED\r\n”
表明数据没有被存储,但不是因为发生错误。这通常意味着add 或 replace命令的条件不成立,或者,项目已经位列删除队列(参考后文的“delete”命令)。

取回命令
一行取回命令如下:
get *\r\n
– * 表示一个或多个键值,由空格隔开的字串
这行命令以后,客户端的等待0个或多个项目,每项都会收到一行文本,然后跟着数据区块。所有项目传送完毕后,服务器发送以下字串:
“END\r\n”
来指示回应完毕。
服务器用以下形式发送每项内容:
VALUE \r\n
\r\n
– 是所发送的键名
– 是存储命令所设置的记号
– 是随后数据块的长度,*不包括* 它的界定符“\r\n”
– 是发送的数据

如果在取回请求中发送了一些键名,而服务器没有送回项目列表,这意味着服务器没这些键名(可能因为它们从未被存储,或者为给其他内容腾出空间而被删除,或者到期,或者被已客户端删除)。

删除
命令“delete”允许从外部删除内容:
delete

参考随后的“flush_all”命令使所有内容无效

增加/减少
命令 “incr” 和 “decr”被用来修改数据,当一些内容需要 替换、增加 或减少时。这些数据必须是十进制的32位无符号整新。如果不是,则当作0来处理。修改的内容必须存在,当使用“incr”/“decr”命令修改不存在的内容时,不会被当作0处理,而是操作失败。

客户端发送命令行:
incr \r\n

decr \r\n
– 是客户端希望修改的内容的建名
– 是客户端要增加/减少的总数。

回复为以下集中情形:
– “NOT_FOUND\r\n”
指示该项内容的值,不存在。
– \r\n ,是 增加/减少 。
注意”decr”命令发生下溢:如果客户端尝试减少的结果小于0时,结果会是0。”incr” 命令不会发生溢出。

状态
命令”stats” 被用于查询服务器的运行状态和其他内部数据。有两种格式。不带参数的:
stats\r\n
这会在随后输出各项状态、设定值和文档。另一种格式带有一些参数:
stats \r\n
通过,服务器传回各种内部数据。因为随时可能发生变动,本文不提供参数的种类及其传回数据。

各种状态
受到无参数的”stats”命令后,服务器发送多行内容,如下:
STAT \r\n
服务器用以下一行来终止这个清单:
END\r\n
在每行状态中, 是状态的名字, 使状态的数据。 以下清单,是所有的状态名称,数据类型,和数据代表的含义。
在“类型”一列中,”32u”表示32位无符号整型,”64u”表示64位无符号整型,”32u:32u”表示用冒号隔开的两个32位无符号整型。

名称 类型 含义
pid 32u 服务器进程ID
uptime 32u 服务器运行时间,单位秒
time 32u 服务器当前的UNIX时间
version string 服务器的版本号
rusage_user 32u:32u 该进程累计的用户时间
(秒:微妙)
rusage_system 32u:32u 该进程累计的系统时间
(秒:微妙)
curr_items 32u 服务器当前存储的内容数量
total_items 32u 服务器启动以来存储过的内容总数
bytes 64u 服务器当前存储内容所占用的字节数
curr_connections 32u 连接数量
total_connections 32u 服务器运行以来接受的连接总数
connection_structures 32u 服务器分配的连接结构的数量
cmd_get 32u 取回请求总数
cmd_set 32u 存储请求总数
get_hits 32u 请求成功的总次数
get_misses 32u 请求失败的总次数
bytes_read 64u 服务器从网络读取到的总字节数
bytes_written 64u 服务器向网络发送的总字节数
limit_maxbytes 32u 服务器在存储时被允许使用的字节总数

其它命令
“flush_all”命令有一个可选的数字参数。它总是执行成功,服务器会发送“OK\r\n”回应。它的效果是使已经存在的项目立即失效(缺省),或在指定的时间后。此后执行取回命令,将不会有任何内容返回(除非重新存储同样的键名)。flush_all 实际上没有立即释放项目所占用的内存,而是在随后陆续有新的项目被储存时执行。flush_all 效果具体如下:它导致所有更新时间早于flush_all所设定时间的项目,在被执行取回命令时命令被忽略。
“version”命令没有参数:
version\r\n
在回应中,服务器发送:
“VERSION \r\n”
是服务器的版本字串。
“quit”命令没有参数:
quit\r\n
接收此命令后,服务器关闭连接。不过,客户端可以在不再需要时,简单地关闭连接就行,并不一定需要发送这个命令。
UDP 协议
当来自客户端的连接数远大于TCP连接的上限时,可以使用基于UDP的接口。UDP接口不能保证传输到位,所以只有在不要求成功的操作中使用;比如被用于一个“get”请求时,会因不当的缓存处理而发生错误或回应有遗失。

每个UDP数据包都包含一个简单的帧头,数据之后的内容与TCP协议的描述类似。在执行所产生的数据流中,请求必须被包含在单独的一个UDP数据包中,但是回应可能跨越多个数据包。(只有“get”和“set”请求例外,跨越了多个数据包)

帧头有8字节长,如下(均由16位整数组成,网络字节顺序,高位在前):

0-1 请求ID
2-3 序号
4-5 该信息的数据包总数
6-7 保留位,必须为0

请求ID有客户端提供。一般它会是一个从随机基数开始的递增值,不过客户端想用什么样的请求ID都可以。服务器的回应会包含一个和请求中的同样的ID。客户端使用请求ID来区分每一个回应。任何一个没有请求ID的数据包,可能是之前的请求遭到延迟而造成的,应该被丢弃。序号的返回是从0到n-1,n是该条信息的数据包数量。

【转载】Memcaches相关介绍信息 有 39 条回应

  1. This piece of writing is really a fastidious one it assists new
    internet users, who are wishing in favor of blogging.

    回复
  2. There is definately a lot to know about this topic.
    I like all the points you’ve made.

    回复
  3. Wonderful blog! I found it while surfing around on Yahoo News.
    Do you have any tips on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Many thanks

    回复
  4. Thank you, I’ve just been searching for information approximately this topic for
    a long time and yours is the greatest I’ve discovered
    so far. However, what in regards to the conclusion? Are
    you positive in regards to the supply?

    回复
  5. Hi! I just wanted to ask if you ever have any problems with hackers?
    My last blog (wordpress) was hacked and I
    ended up losing a few months of hard work due to no back up.

    Do you have any solutions to protect against hackers?

    回复
  6. I quite like looking through an article that can make people think.

    Also, thanks for allowing me to comment!

    回复
  7. I’d like to find out more? I’d like to find out more details.

    回复
  8. excellent submit, very informative. I’m wondering why the other specialists of this sector don’t realize this.
    You must continue your writing. I’m confident, you’ve a huge
    readers’ base already!

    回复
  9. It’s enormous that you are getting ideas from this post as well
    as from our argument made at this place.

    回复
  10. Painter And Decorator Gloucester
    38 Bruton Way
    Gloucester GL1 1DA, United Kingdom
    01452 223268

    回复
  11. I got this web site from my pal who informed me on the topic of this web
    site and now this time I am visiting this web site and reading very informative posts here.

    回复
  12. Hello, this weekend is fastidious for me, as this point in time i am
    reading this enormous educational paragraph here at my home.

    回复
  13. It’s actually very difficult in this full of activity life to listen news on Television, so
    I simply use world wide web for that purpose, and get the
    most up-to-date news.

    回复
  14. This information is invaluable. Where can I find out more?

    回复
  15. Hi to every one, for the reason that I am truly eager of reading this blog’s post to be updated regularly.
    It consists of fastidious stuff.

    回复
  16. It’s actually a great and helpful piece of
    info. I’m satisfied that you just shared this useful info with
    us. Please keep us informed like this. Thank you for sharing.

    回复
  17. If you want to get a great deal from this article
    then you have to apply such techniques to your won blog.

    回复
  18. I all the time used to read paragraph in news papers
    but now as I am a user of net so from now I am using
    net for posts, thanks to web.

    回复
  19. What’s Happening i’m new to this, I stumbled upon this I have discovered It absolutely useful and it has aided me out loads.
    I hope to give a contribution & assist different customers like its aided
    me. Great job.

    回复
  20. The other day, while I was at work, my cousin stole my apple ipad and tested
    to see if it can survive a forty foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views.

    I know this is totally off topic but I had to share it with someone!

    回复
  21. Wonderful beat ! I would like to apprentice while you amend your website, how can i subscribe for
    a blog website? The account helped me a acceptable deal.
    I had been a little bit acquainted of this your
    broadcast offered bright clear concept

    回复
  22. If you are going for most excellent contents like myself, just
    visit this website all the time because it presents quality contents, thanks

    回复
  23. Hi there! I just wanted to ask if you ever have any trouble
    with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work
    due to no back up. Do you have any methods to stop hackers?

    回复
  24. After checking out a few of the articles on your blog, I honestly like your technique of writing a blog.
    I book-marked it to my bookmark webpage list and will be checking back
    soon. Take a look at my website as well and tell me how you
    feel.

    回复
  25. Admiring the commitment you put into your blog and in depth
    information you offer. It’s nice to come across a blog every once in a while that isn’t the same old
    rehashed information. Excellent read! I’ve bookmarked your site and I’m
    including your RSS feeds to my Google account.

    回复
  26. What’s up, always i used to check web site posts here in the early hours
    in the dawn, since i enjoy to learn more and more.

    回复
  27. Hello, i read your blog from time to time and i own a similar
    one and i was just wondering if you get a lot of spam
    comments? If so how do you stop it, any plugin or anything you
    can advise? I get so much lately it’s driving me
    mad so any support is very much appreciated.

    回复
  28. Can you tell us more about this? I’d care to find
    out more details.

    回复
  29. My spouse and I stumbled over here different page and thought I might check
    things out. I like what I see so now i am following you.
    Look forward to looking over your web page for a second
    time.

    回复
  30. Wonderful beat ! I would like to apprentice at the same
    time as you amend your website, how can i subscribe for a blog web site?
    The account aided me a acceptable deal. I have been tiny bit acquainted of this your broadcast offered
    vivid transparent concept

    回复
  31. You have made some really good points there. I looked on the web to learn more about the issue and found most people will go along with your views
    on this site.

    回复
  32. Hello there! Do you know if they make any plugins to help with Search Engine
    Optimization? I’m trying to get my blog to rank for
    some targeted keywords but I’m not seeing very good results.
    If you know of any please share. Thank you!

    回复
  33. This piece of writing will help the internet
    people for creating new blog or even a blog from start to end.

    回复
  34. I was curious if you ever considered changing the structure of your blog?
    Its very well written; I love what youve got to say. But maybe you could a little more in the way of content
    so people could connect with it better. Youve got an awful lot of text for only having
    1 or 2 pictures. Maybe you could space it out better?

    回复
  35. Hello there! Do you know if they make any plugins
    to safeguard against hackers? I’m kinda paranoid about losing everything
    I’ve worked hard on. Any suggestions?

    回复
  36. Just desire to say your article is as surprising. The clearness to your put up is simply cool and i can suppose you’re knowledgeable on this subject.
    Fine with your permission allow me to grasp your RSS feed to stay up to
    date with imminent post. Thank you one million and
    please keep up the rewarding work.

    回复
  37. Howdy! I’m at work browsing your blog from my new iphone 3gs!
    Just wanted to say I love reading your blog and look forward to all your posts!
    Carry on the superb work!

    回复
  38. This piece of writing is really a pleasant one it helps
    new the web users, who are wishing for blogging.

    回复
  39. With havin so much content do you ever run into any issues of plagorism or copyright violation? My
    blog has a lot of completely unique content I’ve either authored myself or outsourced
    but it looks like a lot of it is popping it up all over the internet without my agreement.
    Do you know any techniques to help reduce content from
    being stolen? I’d genuinely appreciate it.

    回复

发表评论

电子邮件地址不会被公开。 必填项已用*标注