1. Memcached介紹
2. Windows版本下載與安裝
2.1 下載地址
memcached 1.4.4 for Windows 32-bit
2.2 安裝Memcache爲系統服務
-
下載memcached的windows穩定版,解壓放某個盤下面,比如在
c:\memcached
-
在CMD下輸入 “
c:\memcached\memcached.exe -d install
“ 安裝. -
再輸入:”
c:\memcached\memcached.exe -d start
“ 啓動。
也可以不安裝直接啓動
以後memcached將作爲windows的一個服務每次開機時自動啓動。這樣服務器端已經安裝完畢了。
2.3 常用命令
2 3 4 5 6 7 8 9 10 11 12 13 14
|
-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 顯示幫助
|
2
|
然後就可以用.net 的memcached客戶端來試一下了。
3. 客戶端使用
3.1 C#下可用的library
每個客戶端API中都有詳細的說明和註釋- .NET memcached client library https://sourceforge.net/projects/memcacheddotnet/
- EnyimMemcached http://www.codeplex.com/EnyimMemcached/ - Client developed in .NET 2.0 keeping performance and extensibility inmind. (Supports consistent hashing.)
- beitmemcached h.NET memcached client library http://code.google.com/p/beitmemcached/ - Client developed by BeIT with many new features
博客園使用的組合爲:
couchbase+EnyimMemcached
http://www.couchbase.com/
3.2 一致性hash算法
.NET實現(註釋參考JAVA版本)link
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
public class KetamaNodeLocator { //原文中的JAVA類TreeMap實現了Comparator方法,這裏我圖省事,直接用了net下的SortedList,其中Comparer接口方法) private SortedList<long, string> ketamaNodes = new SortedList<long, string>(); private HashAlgorithm hashAlg; private int numReps = 160; //此處參數與JAVA版中有區別,因爲使用的靜態方法,所以不再傳遞HashAlgorithm alg參數 public KetamaNodeLocator(List<string> nodes, int nodeCopies) { ketamaNodes = new SortedList<long, string>(); numReps = nodeCopies; //對所有節點,生成nCopies個虛擬結點 foreach (string node in nodes) { //每四個虛擬結點爲一組 for (int i = 0; i < numReps / 4; i++) { //getKeyForNode方法爲這組虛擬結點得到惟一名稱 byte[] digest = HashAlgorithm.computeMd5(node + i); /** Md5是一個16字節長度的數組,將16字節的數組每四個字節一組,分別對應一個虛擬結點,這就是爲什麼上面把虛擬結點四個劃分一組的原因*/ for (int h = 0; h < 4; h++) { long m = HashAlgorithm.hash(digest, h); ketamaNodes[m] = node; } } } } public string GetPrimary(string k) { byte[] digest = HashAlgorithm.computeMd5(k); string rv = GetNodeForKey(HashAlgorithm.hash(digest, 0)); return rv; } string GetNodeForKey(long hash) { string rv; long key = hash; //如果找到這個節點,直接取節點,返回 if (!ketamaNodes.ContainsKey(key)) { //得到大於當前key的那個子Map,然後從中取出第一個key,就是大於且離它最近的那個key 說明詳見: http://www.javaeye.com/topic/684087 var tailMap = from coll in ketamaNodes where coll.Key > hash select new { coll.Key }; if (tailMap == null || tailMap.Count() == 0) key = ketamaNodes.FirstOrDefault().Key; else key = tailMap.FirstOrDefault().Key; } rv = ketamaNodes[key]; return rv; } }
|
下面的代碼與JAVA中有所不同,它使用靜態方法實現
靜態方法實現link
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
public class HashAlgorithm { public static long hash(byte[] digest, int nTime) { long rv = ((long)(digest[3 + nTime * 4] & 0xFF) << 24) | ((long)(digest[2 + nTime * 4] & 0xFF) << 16) | ((long)(digest[1 + nTime * 4] & 0xFF) << 8) | ((long)digest[0 + nTime * 4] & 0xFF); return rv & 0xffffffffL; /* Truncate to 32-bits */ } /** * Get the md5 of the given key. */ public static byte[] computeMd5(string k) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] keyBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(k)); md5.Clear(); //md5.update(keyBytes); //return md5.digest(); return keyBytes; } }
|
4. Linux系統安裝memcached
1 2 3 4 5 6
|
wget http://memcached.org/latest tar -zxvf memcached-1.x.x.tar.gz cd memcached-1.x.x ./configure --prefix=/usr/local/memcached make && make test sudo make install
|
Ubuntu & Debian
apt-get install memcached
Redhat/Fedora
yum install memcached
FreeBSD
portmaster databases/memcached
參考
- memcached官方 http://www.memcached.org/
- https://code.google.com/p/memcached/
- Memcached深度分析
- 一致性Hash算法(KetamaHash)的c#實現http://www.cnblogs.com/daizhj/archive/2010/08/24/1807324.html