標籤:blog class code tar ext width
原文地址:http://onlyonewt.blog.sohu.com/160168896.html
一直在學習關注大訪問量網站的緩衝是如何?,之前看過Memcached的資料,忙於沒有時間來真正測試一下,今天測試下分布式緩衝Memcached
首先要在快取服務器上安裝安裝:memcached(1.2.6 for Win32)
測試程式部署到本地環境,開發工具VS2008 .NET3.5
使用到memcached 1.2.6 for Win32:
memcached-1.2.6-win32-bin.zip
好了,下面我們按步驟來測試:
第一、首先到把下載好的memcached 1.2.6解壓到C:\memcached目錄,分別複製到兩台伺服器中。
第二、安裝memcached服務,在命令提示字元輸入CD c:\memcached進入到memcached目錄,如:
之後輸入memcached -h 斷行符號,看協助說明,接下來輸入memcached -d install 斷行符號即可自動安裝
memcached服務了,如:
安裝好安裝memcached服務後,輸入memcached -d start 斷行符號啟動memcached服務,如:
在兩台電腦都按以上操作來安裝啟動memcached。
第三、下載.NET版memcached用戶端API組件來寫測試程式。
使用memcacheddotnet,如下:
http://sourceforge.net/projects/memcacheddotnet/
下載好之後把這些檔案Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,
Memcached.ClientLibrary.dll放到bin目錄(少一個都不行),之後再到測試專案開發環境引用
Memcached.ClientLibrary.dll,如
第四、測試程式:
Memcached.cs
--------------------------------------
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Memcached.ClientLibrary;
6 using System.Collections;
7
8 namespace Test {
9 public partial class Memcached {
10 private static Memcached _instance = new Memcached();
11 public static Memcached _ {
12 get {
13 return _instance;
14 }
15 }
16
17 string[] servers = { "192.168.1.10:11211","192.168.1.11:11211" };
18 SockIOPool pool;
19 MemcachedClient mc;
20
21 public Memcached() {
22 //初始化池
23 pool = SockIOPool.GetInstance();
24 pool.SetServers(servers);
25
26 pool.InitConnections = 3;
27 pool.MinConnections = 3;
28 pool.MaxConnections = 1000;
29
30 pool.SocketConnectTimeout = 1000;
31 pool.SocketTimeout = 3000;
32
33 pool.MaintenanceSleep = 30;
34 pool.Failover = true;
35
36 pool.Nagle = false;
37 pool.Initialize();
38
39 mc = new MemcachedClient();
40 mc.EnableCompression = false;
41 }
42
43 public void Remove(string key) {
44 mc.Delete(key);
45 }
46
47 public bool Set(string key, object value) {
48 return mc.Set(key, value);
49 }
50
51 public bool Set(string key, object value, int minute) {
52 return mc.Set(key, value, DateTime.Now.AddMinutes(minute));
53 }
54
55 public Hashtable Stats() {
56 return mc.Stats();
57 }
58
59 public object Get(string key) {
60 return mc.Get(key);
61 }
62
63 public bool ContainsKey(string key) {
64 return mc.KeyExists(key);
65 }
66 }
67 public class MemcachedTest {
68 /// <summary>
69 /// 測試緩衝
70 /// </summary>
71 public void test() {
72 //寫入緩衝
73 Console.WriteLine("寫入緩衝測試:");
74 Console.WriteLine("_______________________________________");
75 if (Memcached._.ContainsKey("cache")) {
76 Console.WriteLine("緩衝cache已存在");
77 }
78 else {
79 Memcached._.Set("cache", "寫入緩衝時間:" + DateTime.Now.ToString());
80 Console.WriteLine("緩衝已成功寫入到cache");
81 }
82 Console.WriteLine("_______________________________________");
83 Console.WriteLine("讀取緩衝內容如下:");
84 Console.WriteLine(Memcached._.Get("cache").ToString());
85
86 //測試緩衝到期
87 Console.WriteLine("_______________________________________");
88 if (Memcached._.ContainsKey("endCache")) {
89 Console.WriteLine("緩衝endCache已存在,到期時間為:" + Memcached._.Get
90
91 ("endCache").ToString());
92 }
93 else {
94 Memcached._.Set("endCache", DateTime.Now.AddMinutes(1).ToString(), 1);
95 Console.WriteLine("緩衝已更新寫入到endCache");
96 Console.WriteLine("寫入時間:" + DateTime.Now.ToString());
97 Console.WriteLine("到期時間:" + DateTime.Now.AddMinutes(1).ToString()); 98 }
99
100 //分析緩衝狀態
101 Hashtable ht = Memcached._.Stats();
102 Console.WriteLine("_______________________________________");
103
104 Console.WriteLine("Memcached Stats:");
105 Console.WriteLine("_______________________________________");
106 foreach (DictionaryEntry de in ht) {
107 Hashtable info = (Hashtable)de.Value;
108 foreach (DictionaryEntry de2 in info) {
109 Console.WriteLine(de2.Key.ToString() + ":" + de2.Value.ToString() + " ");
110 }
111 }
112 }
113 }
114 }
115
Program.cs
--------------------------------------
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6
7 namespace Test {
8 class Program {
9 static void Main(string[] args) {
10 MemcachedTest mt = new MemcachedTest();
11 mt.test();
12 Console.ReadLine();
13 }
14 }
15 }
首次執行:
第二次便從緩衝中讀取: