Memcached windows 下安裝與測試

來源:互聯網
上載者:User
Memcached 是一個高效能的分布式記憶體對象緩衝系統,用於動態Web應用以減輕資料庫負載。它通過在記憶體中快取資料和對象來減少讀取資料庫的次數,從而提供動態、資料庫驅動網站的速度。Memcached基於一個儲存鍵/值對的hashmap。其守護進程(daemon )是用C寫的,但是用戶端可以用任何語言來編寫,並通過memcached協議與守護進程通訊。但是它並不提供冗餘(例如,複製其hashmap條目);當某個伺服器S停止運行或崩潰了,所有存放在S上的鍵/值對都將丟失。

Memcached官方:http://danga.com/memcached/

關於Memcached的介紹請參考:Memcached深度分析

下載Windows的Server端

下載地址:http://code.jellycan.com/memcached/

安裝Memcache Server(也可以不安裝直接啟動)

1. 下載memcached的windows穩定版,解壓放某個盤下面,比如在c:\memcached
2. 在CMD下輸入 "c:\memcached\memcached.exe -d install" 安裝.
3. 再輸入:"c:\memcached\memcached.exe -d start" 啟動。NOTE: 以後memcached將作為windows的一個服務每次開機時自動啟動。這樣伺服器端已經安裝完畢了。

如果下載的是二進位的版本,直接運行就可以了,可以加上參數來加以設定。


常用設定:
-p <num>          監聽的連接埠
-l <ip_addr>      串連的IP地址, 預設是本機
-d start          啟動memcached服務
-d restart        重起memcached服務
-d stop|shutdown  關閉正在啟動並執行memcached服務
-d install        安裝memcached服務
-d uninstall      卸載memcached服務
-u <username>     以<username>的身份運行 (僅在以root啟動並執行時候有效)
-m <num>          最大記憶體使用量,單位MB。預設64MB
-M                記憶體耗盡時返回錯誤,而不是刪除項
-c <num>          最大同時串連數,預設是1024
-f <factor>       塊大小增長因子,預設是1.25
-n <bytes>        最小分配空間,key+value+flags預設是48
-h                顯示協助

然後就可以用.net 的memcached用戶端來試一下了。

C# 下可用的API(每個用戶端API中都有詳細的說明和注釋)

https://sourceforge.net/projects/memcacheddotnet/
http://www.codeplex.com/EnyimMemcached/ - Client developed in .NET 2.0 keeping performance and extensibility in

mind. (Supports consistent hashing.) 
http://code.google.com/p/beitmemcached/ - Client developed by BeIT with many new features

轉載出處: http://www.yaosansi.com/

----------------------------------------------------------------------------------------

Client調用:

下載範例程式碼網址:http://sourceforge.net/projects/memcacheddotnet/

C#/.NET memcached client library. This library can be used by .NET projects to access memcached servers. Ported from the Java memcached library located athttp://www.whalin.com/memcached/.

/**  * MemcachedBench.cs  *  * Copyright (c) 2005  * Tim Gebhardt <tim@gebhardtcomputing.com>  *   * Based off of code written by  * Greg Whalin <greg@meetup.com>  * for his Java Memcached client:  * http://www.whalin.com/memcached/  *   *  * See the memcached website:  * http://www.danga.com/memcached/  *  * This module is Copyright (c) 2005 Tim Gebhardt.  * All rights reserved.  *  * This library is free software; you can redistribute it and/or  * modify it under the terms of the GNU Lesser General Public  * License as published by the Free Software Foundation; either  * version 2.1 of the License, or (at your option) any later  * version.  *  * This library is distributed in the hope that it will be  * useful, but WITHOUT ANY WARRANTY; without even the implied  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR  * PURPOSE.  See the GNU Lesser General Public License for more  * details.  *  * You should have received a copy of the GNU Lesser General Public  * License along with this library; if not, write to the Free Software  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  *  * @author Tim Gebhardt<tim@gebhardtcomputing.com>   * @version 1.0  */namespace Memcached.MemcachedBench {     using System;     using System.Collections;       using Memcached.ClientLibrary;       public class MemcachedBench      {         /// <summary>         /// Arguments:          ///     arg[0] = the number of runs to do         ///     arg[1] = the run at which to start benchmarking         /// </summary>         /// <param name="args"></param>         [STAThread]         public static void Main(String[] args)          {             int runs = 100;             int start = 200;             if(args.Length > 1)             {                 runs = int.Parse(args[0]);                 start = int.Parse(args[1]);             }               //可以設定多個伺服器列表             //string[] serverlist = { "127.0.0.1:11211" , "140.192.34.73:11211" };             string[] serverlist = { "127.0.0.1:11211" };    //, "140.192.34.73:11211" };               // initialize the pool for memcache servers             SockIOPool pool = SockIOPool.GetInstance();             pool.SetServers(serverlist);               pool.InitConnections = 3;             pool.MinConnections = 3;             pool.MaxConnections = 5;               pool.SocketConnectTimeout = 1000;             pool.SocketTimeout = 3000;               pool.MaintenanceSleep = 30;             pool.Failover = true;               pool.Nagle = false;             pool.Initialize();               // initialize the pool for memcache servers //          SockIOPool pool = SockIOPool.Instance; //          pool.Servers = serverlist; // //          pool.InitConn = 5; //          pool.MinConn = 5; //          pool.MaxConn = 50; //          pool.MaintSleep = 30; //          pool.SocketTO = 1000; // //          pool.Nagle = false; //          pool.Initialize();   // //          // get client instance             MemcachedClient mc = new MemcachedClient();             mc.EnableCompression = false;   //          MemcachedClient mc = new MemcachedClient(); //          mc.CompressEnable = false; //          mc.CompressThreshold = 0; //          mc.Serialize = true;               string keyBase = "testKey";             string obj = "這是我的字串This is a test of an object blah blah es, serialization does not seem to slow things down so much.  The gzip compression is horrible horrible performance, so we only use it for very large objects.  I have not done any heavy benchmarking recently";               long begin = DateTime.Now.Ticks;             for(int i = start; i < start+runs; i++)              {                 mc.Set(keyBase + i, obj);             }             long end = DateTime.Now.Ticks;             long time = end - begin;               Console.WriteLine(runs + " 設定花費的時間-sets: " + new TimeSpan(time).ToString() + "ms");               begin = DateTime.Now.Ticks;             int hits = 0;             int misses = 0;             for(int i = start; i < start+runs; i++)              {                 string str = (string) mc.Get(keyBase + i);                 Console.WriteLine("key={0},value={1}",keyBase+i,str);                 if(str != null)                     ++hits;                 else                    ++misses;             }             end = DateTime.Now.Ticks;             time = end - begin;               Console.WriteLine(runs + "讀取花費的時間- gets: " + new TimeSpan(time).ToString() + "ms");             Console.WriteLine("Cache hits,成功: " + hits.ToString());             Console.WriteLine("Cache misses,失敗: " + misses.ToString());               IDictionary stats = mc.Stats();             foreach(string key1 in stats.Keys)             {                 Console.WriteLine(key1);                 Hashtable values = (Hashtable)stats[key1];                 foreach(string key2 in values.Keys)                 {                     Console.WriteLine(key2 + ":" + values[key2]);                 }                 Console.WriteLine();             }               SockIOPool.GetInstance().Shutdown();             Console.ReadKey();         }     } }

伺服器端:http://files.cnblogs.com/wucg/memcached-1.2.6-win32-bin.zip

下載Client庫檔案及樣本,vs2008,.netframework 1.0,2.0 http://files.cnblogs.com/wucg/clientlib.zip


 

 

 另外一篇文章有詳細介紹:http://hi.baidu.com/qkyh1225/item/ef4d3fdfd63a5fed3dc2cbb0

 1. 下載windows版本的Memcached:http://code.jellycan.com/memcached/



2.解壓之後放在硬碟的目錄下,如:D:\memcached . 然後在運行中輸入cmd進入命令列,進入到Memcached.exe 所在的目錄,例如:D:\memcached,然後輸入:


Cmd代碼  收藏代碼
到memcached根目錄
1、安裝
memcached.exe –d install 

2、啟動
memcached.exe -d start

此時memcached已經註冊為開機啟動服務

完成安裝。

 

 經實驗使用命令修改連接埠無效,相應的解決方案是修改註冊表:

通過修改註冊表來達到這個修改連接埠的目的。

在 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server 下面找到一個 ImagePath 的字串項,正好是服務的執行路徑的字串,雙擊該串,在後面加入 -l 192.168.1.135 -m 45 -p 12345 (訪問ip為:192.168.1.135 使用45M記憶體,12345為連接埠),再啟動服務。




現在來試試是否可以串連:

使用方法為“telnet ip 連接埠號碼”,登入後使用“stats”命令
cmd下
telnet 192.168.1.135 12345(注意:這裡只能用IP 不能用 localhost 或127.0.0.1)

然後使用:stats命令可以看到memcached的使用方式如下:

STAT uptime 760
STAT time 1311913149
STAT version 1.2.1
STAT pointer_size 32
STAT curr_items 0
STAT total_items 0
STAT bytes 0
STAT curr_connections 4
STAT total_connections 6
STAT connection_structures 5
STAT cmd_get 0
STAT cmd_set 0
STAT get_hits 0
STAT get_misses 0
STAT bytes_read 63
STAT bytes_written 793
STAT limit_maxbytes 10485760
END




3、memcached的基本設定 :



-p 監聽的連接埠


-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 顯示協助



4、最主要的一點,在windows下安裝memcache通過命令列修改參數好像不起什麼作用,需要進入註冊表修改啟動項參數才有用,具體操作如下:進入註冊表,找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server , 在其中有一個ImagePath 項,值為"d:\memcached\memcached.exe" -d runservice ,在後面加上-l 127.0.0.1 -m 3000 -c 2048 最後ImagePath 項值應該像這樣:


註冊表值代碼  收藏代碼

   1. d:\memcached\memcached.exe" -d runservice -l 127.0.0.1 -m 3000 -c 2048 

d:\memcached\memcached.exe" -d runservice -l 127.0.0.1 -m 3000 -c 2048



5、memcached –d start 啟動memcached服務,這樣就將memcached的最大記憶體修改為了3000MB。



對Memcached快取服務的狀態查詢,可以先telnet串連上服務:telnet 127.0.0.1 11211 ,然後使用 stats命令查看快取服務的狀態,會返回如下的資料:



time: 1255537291 伺服器當前的unix時間戳記


total_items: 54 從伺服器啟動以後儲存的items總數量


connection_structures: 19 伺服器分配的串連構造數


version: 1.2.6 memcache版本


limit_maxbytes: 67108864 分配給memcache的記憶體大小(位元組)


cmd_get: 1645 get命令(擷取)總請求次數


evictions: 0 為擷取空閑記憶體而刪除的items數(分配給memcache的空間用滿後需
要刪除舊的items來得到空間分配給新的items)


total_connections: 19 從伺服器啟動以後曾經開啟過的串連數


bytes: 248723 當前伺服器儲存items佔用的位元組數


threads: 1 當前線程數


get_misses: 82 總未叫用次數


pointer_size: 32 當前作業系統的指標大小(32位系統一般是32bit)


bytes_read: 490982 總讀取位元組數(請求位元組數)


uptime: 161 伺服器已經啟動並執行秒數


curr_connections: 18 當前開啟著的串連數


pid: 2816 memcache伺服器的進程ID


bytes_written: 16517259 總發送位元組數(結果位元組數)


get_hits: 1563 總叫用次數


cmd_set: 54 set命令(儲存)總請求次數


curr_items: 28 伺服器當前儲存的items數量



關於memcache 的一下疑問:

memcached能接受的key的最大長度是多少。
key 的最大長度是250個字元。需要注意的是,250是memcached伺服器端內部的限制,如果您使用的用戶端支援”key的首碼”或類似特性,那麼 key(首碼+原始key)的最大長度是可以超過250個字元的。我們推薦使用使用較短的key,因為可以節省記憶體和頻寬。

memcached對item的到期時間有什麼限制。
到期時間最大可以達到30天。memcached把傳入的到期時間(時間段)解釋成時間點後,一旦到了這個時間點,memcached就把item置為失效狀態。這是一個簡單但obscure的機制。

memcached最大能儲存多大的單個item。
1MB。如果你的資料大於1MB,可以考慮在用戶端壓縮或拆分到多個key中。

為什麼單個item的大小被限制在1M byte之內。
啊…這是一個大家經常問的問題。

簡單的回答:因為記憶體 Clerk的演算法就是這樣的

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.