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取餘演算法,實現起來不難,不過不知道效率如何? |