编程小贴士

给你的编程提供小点子


Memcache及telnent命令详解

1、启动Memcache 常用参数
memcached 1.4.3
-p <num> 设置端口号(默认不设置为: 11211)
-U <num> UDP监听端口 (默认: 11211, 0 时关闭)
-l <ip_addr> 绑定地址 (默认:所有都允许,无论内外网或者本机更换IP,有安全隐患,若设置为127.0.0.1就只能本机访问)
-d duli进程运行
-u <username> 绑定使用指定用于运行进程 <username>
-m <num> 允许最大内存用量,单位M (默认: 64 MB)
-P <file> 将PID写入文件<file>,这样可以使得后边进行快速进程终止, 需要与 -d 一起使用
如:
在linux下:./usr/local/bin/memcached -d -u jb-mc -l 192.168.1.197 -m 2048 -p 12121
在window下:d:\App_Serv\memcached\memcached.exe -d RunService -l 127.0.0.1 -p 11211 -m 500
在windows下注册为服务后运行:
sc.exe create jb-Memcached binpath= “d:\App_Serv\memcached\memcached.exe -d RunService -p 11211 -m 500″ start= auto
net start jb-Memcached

2、telnet连接

telnet 127.0.0.1 11211

3、写入memcache

3.1 memcached Telnet Interface

Command    Description    Example
get    Reads a value    get mykey
set    Set a key unconditionally    set mykey 0 60 5
add    Add a new key    add newkey 0 60 5
replace    Overwrite existing key    replace key 0 60 5
append    Append data to existing key    append key 0 60 15
prepend    Prepend data to existing key    prepend key 0 60 15
incr    Increments numerical key value by given number    incr mykey 2
decr    Decrements numerical key value by given number    decr mykey 5
delete    Deletes an existing key    delete mykey
flush_all    Invalidate specific items immediately    flush_all
Invalidate all items in n seconds    flush_all 900
stats    Prints general statistics    stats
Prints memory statistics    stats slabs
Prints memory statistics    stats malloc
Print higher level allocation statistics    stats items
stats detail
stats sizes
Resets statistics    stats reset
version    Prints server version.    version
verbosity    Increases log level    verbosity
quit    Terminate telnet session    quit

3.2 telnet请求命令格式

<command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n
a) <command name> 可以是”set”, “add”, “replace”。
“set”表示按照相应的<key>存储该数据,没有的时候增加,有的覆盖。
“add”表示按照相应的<key>添加该数据,但是如果该<key>已经存在则会操作失败。
“replace”表示按照相应的<key>替换数据,但是如果该<key>不存在则操作失败

b) <key> 客户端需要保存数据的key。

c) <flags> 是一个16位的无符号的整数(以十进制的方式表示)。
该标志将和需要存储的数据一起存储,并在客户端get数据时返回。
客户可以将此标志用做特殊用途,此标志对服务器来说是不透明的。

d) <exptime> 过期的时间。
若为0表示存储的数据永远不过时(但可被服务器算法:LRU 等替换)。
如果非0(unix时间或者距离此时的秒数),当过期后,服务器可以保证用户得不到该数据(以服务器时间为标准)。

e) <bytes> 需要存储的字节数(不包含最后的”\r\n”),当用户希望存储空数据时,<bytes>可以为0

f) 最后客户端需要加上”\r\n”作为”命令头”的结束标志。
<data block>\r\n

紧接着”命令头”结束之后就要发送数据块(即希望存储的数据内容),最后加上”\r\n”作为此次通讯的结束。

3.3 telnet响应命令

结果响应:reply
当以上数据发送结束之后,服务器将返回一个应答。可能有如下的情况:

a) “STORED\r\n”:表示存储成功
b) “NOT_STORED\r\n” : 表示存储失败,但是该失败不是由于错误。
通常这是由于”add”或者”replace”命令本身的要求所引起的,或者该项在删除队列之中。

如: set key 33 0 4\r\n
ffff\r\n

4、获取/检查KeyValue
get <key>*\r\n
a) <key>* 表示一个或者多个key(以空格分开)
b) “\r\n” 命令头的结束

结果响应:reply
服务器端将返回0个或者多个的数据项。每个数据项都是由一个文本行和一个数据块组成。当所有的数据项都接收完毕将收到”END\r\n”
每一项的数据结构:
VALUE <key> <flags> <bytes>\r\n
<data block>\r\n

a) <key> 希望得到存储数据的key
b) <falg> 发送set命令时设置的标志项
c) <bytes> 发送数据块的长度(不包含”\r\n”)
d) “\r\n” 文本行的结束标志
e) <data block> 希望接收的数据项。
f) “\r\n” 接收一个数据项的结束标志。

如果有些key出现在get命令行中但是没有返回相应的数据,这意味着服务器中不存在这些项,这些项过时了,或者被删除了
如:get aa
VALUE aa 33 4
ffff
END

5、删除KeyValue:
delete <key> <time>\r\n

a) <key> 需要被删除数据的key
b) <time> 客户端希望服务器将该数据删除的时间(unix时间或者从现在开始的秒数)
c) “\r\n” 命令头的结束

6、检查Memcache服务器状态:
stats\r\n
在这里可以看到memcache的获取次数,当前连接数,写入次数,已经命中率等;

pid : 进程id
uptime :总的运行时间,秒数
time : 当前时间
version : 版本号
……
curr_items : 当前缓存中的KeyValue数量
total_items : 曾经总共经过缓存的KeyValue数量
bytes : 所有的缓存使用的内存量
curr_connections 当前连接数
….
cmd_get : 总获取次数
cmd_set : 总的写入次数
get_hits : 总的命中次数
miss_hits : 获取失败次数
…..
bytes_read : 总共读取的流量字节数
bytes_written : 总的写入流量字节
limit_maxbytes : 最大允许使用的内存量,字节

7、高级缓存细节查看方法:
stats reset
清空统计数据

stats malloc
显示内存分配数据

stats cachedump slab_id limit_num
显示某个slab中的前limit_num个key列表,显示格式如下
ITEM key_name [ value_length b; expire_time|access_time s]
其中,memcached 1.2.2及以前版本显示的是 访问时间(timestamp)
1.2.4以上版本,包括1.2.4显示 过期时间(timestamp)
如果是永不过期的key,expire_time会显示为服务器启动的时间

stats cachedump 7 2
ITEM copy_test1 [250 b; 1207795754 s]
ITEM copy_test [248 b; 1207793649 s]

stats slabs
显示各个slab的信息,包括chunk的大小、数目、使用情况等

stats items
显示各个slab中item的数目和最老item的年龄(最后一次访问距离现在的秒数)

stats detail [on|off|dump]
设置或者显示详细操作记录

参数为on,打开详细操作记录
参数为off,关闭详细操作记录
参数为dump,显示详细操作记录(每一个键值get、set、hit、del的次数)

8、清空所有键值
flush_all
注:flush并不会将items删除,只是将所有的items标记为expired,因此这时memcache依旧占用所有内存。

9、退出
quit\r\n

131 Responses to “ Memcache及telnent命令详解 ”

  1. me suba说道:

    e7FaAn wow, awesome article post. Really Great.

  2. animal xxx说道:

    Thanks for sharing, this is a fantastic blog post.Much thanks again. Awesome.

  3. Kickboxing说道:

    Loving the information on this site, you have done great job on the blog posts.

  4. please visit the sites we comply with, which includes this a single, as it represents our picks through the web

  5. You completed various You completed various nice points there. I did a search on the subject matter and found mainly folks will agree with your blog.

  6. Your current blogs continually have much of really up to date info. Where do you come up with this? Just declaring you are very innovative. Thanks again

  7. Very good article post.Really thank you! Much obliged.

  8. may dong phuc说道:

    I reckon something truly special in this internet site.

  9. soon it wilpl be well-known, due to itss feature contents.

  10. Click to read说道:

    very nice publish, i definitely love this website, keep on it

  11. find more info说道:

    Right now it appears like BlogEngine is the preferred blogging platform out there right now. (from what I ave read) Is that what you are using on your blog?

  12. tunasbola.org说道:

    Whoa! This blog looks just like my old one! It as on a entirely different topic but it has pretty much the same layout and design. Outstanding choice of colors!

  13. This website truly has all the info I needed concerning this subject and didn at know who to ask.

  14. There may be noticeably a bundle to learn about this. I assume you made certain nice factors in options also.

  15. here说道:

    Right now it appears like Drupal is the best blogging platform available right now. (from what I ave read) Is that what you are using on your blog?

  16. w88 world说道:

    you are really a good webmaster. The site loading speed is incredible. It seems that you are doing any unique trick. Also, The contents are masterpiece. you ave done a excellent job on this topic!

  17. We stumbled over here from a different web page and thought I might as well check things out. I like what I see so i am just following you. Look forward to checking out your web page repeatedly.

  18. Well I really enjoyed studying it. This tip procured by you is very effective for proper planning.

  19. Penn Sport USA说道:

    You forgot iBank. Syncs seamlessly to the Mac version. LONGTIME Microsoft Money user haven\\\ at looked back.

  20. Wow, what a video it is! Truly good feature video, the lesson given in this video is really informative.

  21. cheap taxi说道:

    marc jacobs outlet store ??????30????????????????5??????????????? | ????????

  22. bitstarz review说道:

    It is best to participate in a contest for one of the best blogs on the web. I will recommend this website!

  23. Im obliged for the post.Really looking forward to read more. Awesome.

  24. status online说道:

    pretty helpful stuff, overall I imagine this is well worth a bookmark, thanks

  25. more details说道:

    Your style is very unique in comparison to other people I ave read stuff from. I appreciate you for posting when you ave got the opportunity, Guess I all just bookmark this page.

  26. I was suggested this web site by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my trouble. You are incredible! Thanks!

  27. some really superb blog posts on this internet site , thankyou for contribution.

  28. It as onerous to search out educated individuals on this topic, however you sound like you know what you are speaking about! Thanks

  29. Share说道:

    I think other website proprietors should take this website as an model, very clean and excellent user friendly style and design, let alone the content. You are an expert in this topic!

  30. tractors说道:

    Very polite guide and superb articles, very miniature as well we need.

  31. Very informative article.Really looking forward to read more.

  32. Clicking Here说道:

    instances, an offset mortgage provides the borrower with the flexibility forced to benefit irregular income streams or outgoings.

  33. Nwokolo说道:

    Thanks a lot for the article.Thanks Again. Awesome.

  34. klm voucher说道:

    Im thankful for the blog.Really thank you!

  35. Pretty! This was an incredibly wonderful post. Thank you for providing this info.

  36. online. Please let me know if you have any kind of suggestions or tips for new

  37. hampton bay说道:

    You made some nice points there. I looked on the internet for the issue and found most persons will consent with your site.

  38. trash cans说道:

    My blog; how to burn belly fat how to burn belly fat [Tyree]

  39. hip hop说道:

    Wow, superb blog layout! How long have you been blogging for?

  40. Wonderful opinions you ave got here.. I appreciate you discussing your perspective.. Fantastic views you might have here.. Definitely handy viewpoint, many thanks for giving..

  41. Muchos Gracias for your post. Fantastic.

  42. kho hang si ann说道:

    Thanks for sharing, this is a fantastic article.Thanks Again. Cool.

  43. Valuable info. Lucky me I found your web site by chance, and I am surprised why this coincidence did not happened earlier! I bookmarked it.

  44. user in his/her mind that how a user can know it. So that as why this article is amazing. Thanks!

  45. Thank you for your blog post.Much thanks again. Keep writing.

  46. Top Sales Tools说道:

    Very informative blog post.Really looking forward to read more. Cool.

  47. Many thanks for sharing this excellent post. Very interesting ideas! (as always, btw)

  48. Kickboxing说道:

    same unwanted rehashed material. Excellent read!

  49. I value the article post.Thanks Again. Fantastic.

  50. On every weekend, we all friends together used to watch show, because fun is also necessary in life.

  51. I regard something really interesting about your web site so I saved to fav.

  52. play说道:

    Really appreciate you sharing this article post.Really thank you! Awesome.

  53. Vs model说道:

    Your mode of explaining the whole thing in this post is in fact good, every one be able to simply be aware of it, Thanks a lot.

  54. adidas nmd r1说道:

    I as well as my buddies were actually reading through the good key points on your web blog then instantly got a horrible feeling I never thanked the site owner for those tips. Those boys became as a result thrilled to see all of them and have now without a doubt been taking advantage of them. Thank you for truly being quite considerate as well as for making a choice on this sort of impressive things most people are really desirous to learn about. My very own honest regret for not expressing appreciation to you earlier.

  55. Jackeline说道:

    Im obliged for the blog article.Thanks Again. Awesome.

  56. medical podcasts说道:

    Thanks for writing such a good article, I stumbled onto your website and read a few articles. I like your way of writing

  57. share说道:

    It as fantastic that you are getting thoughts from

  58. girls说道:

    You are mistaken. I suggest it to discuss.

  59. When I start your Rss feed it seems to be a lot of garbage, is the issue on my side?

  60. Yeah bookmaking this wasn at a high risk decision outstanding post!.

  61. baking mets说道:

    Very good blog.Really looking forward to read more. Really Great.

  62. baking mets说道:

    It is my opinion other website proprietors ought to choose this blog being an model,really cool great straightforward style,Too since information.You are an expert inside this subject!

  63. Jay-Z说道:

    Thank you for your article.Really looking forward to read more. Want more.

  64. My brother suggested I might like this blog. He was entirely right. This post truly made my day. You can not imagine simply how much time I had spent for this information! Thanks!

  65. belly fat说道:

    very nice publish, i certainly love this web site, carry on it

  66. Crochet bolero说道:

    There is apparently a lot to identify about this. I assume you made various nice points in features also.

  67. Thank you ever so for you post. Really Great.

  68. Your style is so unique in comparison to other folks I ave read stuff from. Thank you for posting when you have the opportunity, Guess I all just bookmark this page.

  69. try this说道:

    Stay up the great work! You recognize, lots of persons

  70. blue nike说道:

    I’а†ve read several good stuff here. Certainly value bookmarking for revisiting. I surprise how so much effort you place to create the sort of great informative website.

  71. value说道:

    Right now it appears like Drupal could be the preferred blogging platform available at this time. (from what I ave read) Is the fact that what you are making use of on your weblog?

  72. Thanks so much for the post.Thanks Again. Fantastic.

  73. Just to let you know your blog looks a little bit unusual on Firefox on my notebook with Linux.

  74. Some truly nice stuff on this internet site, I enjoy it.

  75. School admission说道:

    Really informative article post.Really thank you! Want more.

  76. School admission说道:

    Say, you got a nice blog article.Thanks Again. Keep writing.

  77. Really enjoyed this post.Thanks Again. Much obliged.

  78. You have mentioned very interesting details! ps nice web site.

  79. Very nice post. I just stumbled upon your blog and wanted to say that I ave really enjoyed browsing your blog posts. After all I all be subscribing to your rss feed and I hope you write again soon!

  80. This is one awesome blog post.Really thank you! Awesome.

  81. Regards for this post, I am a big fan of this internet site would like to proceed updated.

  82. visit website说道:

    wow, awesome article post.Really thank you! Really Cool.

  83. pretty handy material, overall I feel this is worth a bookmark, thanks

  84. Really informative blog post.Really thank you! Fantastic.

  85. I value the blog article.Really looking forward to read more.

  86. I really liked your post.Much thanks again. Really Great.

  87. Thanks for sharing, this is a fantastic blog article.Really looking forward to read more. Much obliged.

  88. tips de ventas说道:

    Thank you for producing the powerful, dependable, educational and as well as easy tips about your topic.

  89. I think other web site proprietors should take this website as an model, very clean and fantastic user friendly style and design, as well as the content. You are an expert in this topic!

  90. pretty handy material, overall I consider this is worthy of a bookmark, thanks

  91. Real estate说道:

    The issue is something which too few people are speaking intelligently about.

  92. Onyekachi说道:

    Thanks again for the article.Much thanks again. Fantastic.

  93. resorts说道:

    Its hard to find good help I am forever saying that its difficult to find quality help, but here is

  94. Thanks so much for the blog article.Really looking forward to read more. Much obliged.

  95. This very blog is no doubt cool and informative. I have chosen many interesting things out of this blog. I ad love to visit it again soon. Thanks a bunch!

  96. That as a great post. Thank you so much.

  97. uvb treatment There are a lot of blogging sites dedicated to celebrities (ex. Perez Hilton), love, fashion, travel, and food. But, how do I start one of my own specialty?.

  98. Really informative post.Much thanks again.

  99. for more info说道:

    Really enjoyed this article.Much thanks again. Great.

  100. I value the post.Really looking forward to read more. Keep writing.

  101. uncertainty very quickly it will be famous, due to its feature contents.

  102. may ap trung ga说道:

    You made some clear points there. I looked on the internet for the issue and found most persons will go along with with your site.

  103. Chapel说道:

    Say, you got a nice post.Really thank you! Really Great.

  104. know more说道:

    That is a really good tip particularly to those new to the blogosphere. Brief but very accurate information Many thanks for sharing this one. A must read post!

  105. It as arduous to find knowledgeable individuals on this matter, however you sound like you already know what you are speaking about! Thanks

  106. Wow, great article.Really looking forward to read more.

  107. see this website说道:

    There as noticeably a bundle to find out about this. I assume you made certain nice factors in features also.

  108. Major thanks for the article post.Really thank you! Will read on

  109. Very good post! We are linking to this great post on our website. Keep up the good writing.

  110. Okey说道:

    It as remarkable to go to see this web site and reading the views of all mates concerning this article, while I am also zealous of getting experience. Look at my web page free antivirus download

  111. this website说道:

    Paragraph writing is also a fun, if you be familiar with then you can write

  112. Thank you ever so for you article post.Thanks Again. Will read on

  113. app说道:

    Just that is necessary. I know, that together we can come to a right answer.

  114. take a look at说道:

    Nothing is more admirable than the fortitude with which millionaires tolerate the disadvantages of their wealth..

  115. I value the blog article.Much thanks again.

  116. This is a beautiful picture with very good lighting

  117. I do not even know how I ended up here, but I thought this post
    was great. I do not know who you are but definitely you
    are going to a famous blogger if you are not already
    ;) Cheers!

  118. Wow, marvelous blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is fantastic, as well as the content!

  119. Rusty Glasper说道:

    Should you even so undecided: download all the earphones, person all the down to some sort of Biggest score as well as get regarding stopper these people in to a Zune following that a music player and see the one that comments easier to a person, in addition to which experts claim vent making you smirk whole lot. You’ll are familiar with explaining perfect for you.

  120. This very blog is without a doubt educating as well as amusing. I have picked helluva helpful tips out of this source. I ad love to come back every once in a while. Thanks a lot!

  121. Very good blog.Much thanks again. Much obliged.

  122. very handful of internet sites that take place to become in depth beneath, from our point of view are undoubtedly well worth checking out

  123. more info说道:

    Really informative blog post. Fantastic.

  124. Thanks for the article post.Really looking forward to read more. Really Cool.

  125. lawyer miami fl说道:

    Nice Site , guys! Good Articles aswell. Bookmarked

  126. other details说道:

    Thanks-a-mundo for the blog post.Much thanks again. Will read on

  127. go here说道:

    Your style is really unique compared to other people I ave read stuff from. I appreciate you for posting when you ave got the opportunity, Guess I all just bookmark this blog.

  128. sunday说道:

    Very good article post.Much thanks again. Fantastic.

  129. Way cool! Some very valid points! I appreciate you writing this write-up and also the rest of the website is also very good.

  130. Say, you got a nice article post.Thanks Again. Great.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>