您的位置:首页 > 数据库 > Memcache

Memcache 的.NET调用

2011-03-24 10:29 211 查看
1. 安装memcache
(1) 下载http://beitmemcached.googlecode.com/files/Memcached_1.2.5.zip
(2) 安装
memcached.exe -d install
memcached.exe -d start
(3)memcached的基本设置
-p 监听的端口,默认是11211
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助
-vv 用very vrebose模式启动,调试信息和错误输出到控制台

2.使用memcache client
(1)安装http://code.google.com/p/beitmemcached/
(2)使用,beitmemcached代码中有示例,不再烦诉

3.相关命令
(1)存取数据
$ telnet localhost 11211
Trying 127.0.0.1…
Connected to localhost.localdomain (127.0.0.1).
Escape character is ‘^]’.
set foo 0 0 3 (保存命令)
bar (数据)
STORED (结果)
get foo (取得命令)
VALUE foo 0 3 (数据)
bar (数据)
(2)查看memcached的内部状态
$ telnet localhost 11211
Connected to localhost.
Escape character is ‘^]’.
stats
STAT pid 481
STAT uptime 16574
STAT time 1213687612
STAT version 1.2.5
STAT pointer_size 32
STAT rusage_user 0.102297
STAT rusage_system 0.214317
STAT curr_items 0
STAT total_items 0
STAT bytes 0
STAT curr_connections 6
STAT total_connections 8
STAT connection_structures 7
STAT cmd_get 0
STAT cmd_set 0
STAT get_hits 0
STAT get_misses 0
STAT evictions 0
STAT bytes_read 20
STAT bytes_written 465
STAT limit_maxbytes 67108864
STAT threads 4
END
Quit
另外,如果安装了libmemcached这个面向C/C++语言的客户端库,就会安装 memstat 这个命令。使用方法很简单,可以用更少的步骤获得与telnet相同的信息,还能一次性从多台服务器获得信息。
$ memstat –servers=server1,server2,server3,…libmemcached可以从下面的地址获得:
http://tangent.org/552/libmemcached.html
memcache的stats命令包括:
1. stats
2. stats reset
3. stats malloc
4. stats maps
5. stats sizes
6. stats slabs
7. stats items
8. stats cachedump slab_id limit_num
9. stats detail [on|off|dump]
(3)查看slabs的使用状况
使用memcached的创造着Brad写的名为memcached-tool的Perl脚本,可以方便地获得slab的使用情况(它将memcached的返回值整理成容易阅读的格式)。可以从下面的地址获得脚本:
http://code.sixapart.com/svn/memcached/trunk/server/scripts/memcached-tool
使用方法也极其简单:
$ memcached-tool 主机名:端口 选项查看slabs使用状况时无需指定选项,因此用下面的命令即可:
$ memcached-tool 主机名:端口获得的信息如下所示:
# Item_Size Max_age 1MB_pages Count Full?
1 104 B 1394292 s 1215 12249628 yes
2 136 B 1456795 s 52 400919 yes
3 176 B 1339587 s 33 196567 yes
4 224 B 1360926 s 109 510221 yes
5 280 B 1570071 s 49 183452 yes
6 352 B 1592051 s 77 229197 yes
7 440 B 1517732 s 66 157183 yes
8 552 B 1460821 s 62 117697 yes
9 696 B 1521917 s 143 215308 yes
10 872 B 1695035 s 205 246162 yes
11 1.1 kB 1681650 s 233 221968 yes
12 1.3 kB 1603363 s 241 183621 yes
13 1.7 kB 1634218 s 94 57197 yes
14 2.1 kB 1695038 s 75 36488 yes
15 2.6 kB 1747075 s 65 25203 yes
16 3.3 kB 1760661 s 78 24167 yes
各列的含义为:
列 含义
# slab class编号
Item_Size Chunk大小
Max_age LRU内最旧的记录的生存时间
1MB_pages 分配给Slab的页数
Count Slab内的记录数
Full? Slab内是否含有空闲chunk
从这个脚本获得的信息对于调优非常方便,强烈推荐使用。
4,注意事项
(1)memcache对key和value的大小都有限制。
key :250个字符以内。
value:大小在1M以内。
(2)memcached在windows下性能远远不如linux (未验证)

附录:
1. http://www.cnblogs.com/hq2008/archive/2008/10/14/1310823.html 2. http://avenger.name/blog/memcache-namespace/ 3. http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt 4. http://www.danga.com/memcached/ 5.http://www.cnblogs.com/sunli/archive/2008/11/01/1324153.html
6.Memcache 协议(中英对照)http://www.gaobo.info/read.php/447.htm
7. http://blog.csdn.net/heiyeshuwu/archive/2006/11/13/1380838.aspx 8. http://ahuaxuan.javaeye.com/blog/256658
PS:.NET下的简单实现想法,微软企业库缓存+WCF,分布式在客户端实现,使用CRC32取余算法,实现起来不难,不过不知道效率如何?

2010-08-06 10:52
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: