分布式緩存系統(tǒng)Memcached簡介與實(shí)踐緣起: 在數(shù)據(jù)驅(qū)動的web開發(fā)中,經(jīng)常要重復(fù)從數(shù)據(jù)庫中取出相同的數(shù)據(jù),這種重復(fù)極大的增加了數(shù)據(jù)庫負(fù)載。緩存是解決這個問題的好辦法。但是ASP.NET中的雖然已經(jīng)可以實(shí)現(xiàn)對頁面局部進(jìn)行緩存,但還是不夠靈活。此時Memcached或許是你想要的。 里面有.net1.1 和 .net2.0的兩種版本 還有一個不錯的例子。 三 應(yīng)用 1 將Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll 等放到bin目錄 1 namespace Memcached.MemcachedBench
4 運(yùn)行結(jié)果 2 { 3 using System; 4 using System.Collections; 5 6 using Memcached.ClientLibrary; 7 8 public class MemcachedBench 9 { 10 [STAThread] 11 public static void Main(String[] args) 12 { 13 string[] serverlist = { "10.0.0.131:11211", "10.0.0.132:11211" }; 14 15 //初始化池 16 SockIOPool pool = SockIOPool.GetInstance(); 17 pool.SetServers(serverlist); 18 19 pool.InitConnections = 3; 20 pool.MinConnections = 3; 21 pool.MaxConnections = 5; 22 23 pool.SocketConnectTimeout = 1000; 24 pool.SocketTimeout = 3000; 25 26 pool.MaintenanceSleep = 30; 27 pool.Failover = true; 28 29 pool.Nagle = false; 30 pool.Initialize(); 31 32 // 獲得客戶端實(shí)例 33 MemcachedClient mc = new MemcachedClient(); 34 mc.EnableCompression = false; 35 36 Console.WriteLine("------------測 試-----------"); 37 mc.Set("test", "my value"); //存儲數(shù)據(jù)到緩存服務(wù)器,這里將字符串"my value"緩存,key 是"test" 38 39 if (mc.KeyExists("test")) //測試緩存存在key為test的項(xiàng)目 40 { 41 Console.WriteLine("test is Exists"); 42 Console.WriteLine(mc.Get("test").ToString()); //在緩存中獲取key為test的項(xiàng)目 43 } 44 else 45 { 46 Console.WriteLine("test not Exists"); 47 } 48 49 Console.ReadLine(); 50 51 mc.Delete("test"); //移除緩存中key為test的項(xiàng)目 52 53 if (mc.KeyExists("test")) 54 { 55 Console.WriteLine("test is Exists"); 56 Console.WriteLine(mc.Get("test").ToString()); 57 } 58 else 59 { 60 Console.WriteLine("test not Exists"); 61 } 62 Console.ReadLine(); 63 64 SockIOPool.GetInstance().Shutdown(); //關(guān)閉池, 關(guān)閉sockets 65 } 66 } 67 }
后記: 是個不錯的東西 ,使用起來也很方便,php ,ruby 的項(xiàng)目中用這個的很多,但是.net項(xiàng)目中用的較少(恕俺孤陋寡聞) 。希望有興趣的朋友們 多多交流 。 看到頁首了么各位兄弟? 不用我多說了吧,揮動您的鼠標(biāo) 輕輕按下左鍵吧 |
|
|