分布式緩衝系統Memcached簡介與實踐

來源:互聯網
上載者:User

緣起: 在資料驅動的web開發中,經常要重複從資料庫中取出相同的資料,這種重複極大的增加了資料庫負載。緩衝是解決這個問題的好辦法。但是ASP.NET中的雖然已經可以實現對頁面局部進行緩衝,但還是不夠靈活。此時Memcached或許是你想要的。

Memcached是什麼?
Memcached是由Danga Interactive開發的,高效能的,分布式的記憶體對象緩衝系統,用於在Live App中減少資料庫負載,提升訪問速度。

Memcached能緩衝什麼。
通過在記憶體裡維護一個統一的巨大的hash表,Memcached能夠用來儲存各種格式的資料,包括映像、視頻、檔案以及資料庫檢索的結果等。

Memcached快麼。

非常快。Memcached使用了libevent(如果可以的話,在linux下使用epoll)來均衡任何數量的開啟連結,使用非阻塞的網路I/O,對內部對象實現引用計數(因此,針對多樣的用戶端,對象可以處在多樣的狀態), 使用自己的頁塊分配器和雜湊表, 因此虛擬記憶體不會產生片段並且虛擬記憶體分配的時間複雜度可以保證為O(1).。

Danga Interactive為提升Danga Interactive的速度研發了Memcached。目前,LiveJournal.com每天已經在向一百萬使用者提供多達兩千萬次的頁面訪問。而這些,是由一個由web伺服器和資料庫伺服器組成的叢集完成的。Memcached幾乎完全放棄了任何資料都從資料庫讀取的方式,同時,它還縮短了使用者查看頁面的速度、更好的資源分派方式,以及Memcache失效時對資料庫的訪問速度。

Memcached的特點
Memcached的緩衝是一種分布式的,可以讓不同主機上的多個使用者同時訪問, 因此解決了共用記憶體只能單機應用的局限,更不會出現使用資料庫做類似事情的時候,磁碟開銷和阻塞的發生。

Memcached的使用
一
Memcached伺服器端的安裝 (此處將其作為系統服務安裝)
  下載檔案:memcached 1.2.1 for Win32 binaries (Dec 23, 2006)
   1 解壓縮檔案到c:/memcached
   2 命令列輸入 'c:/memcached/memcached.exe -d install' 
   3 命令列輸入 'c:/memcached/memcached.exe -d start' ,該命令啟動 Memcached ,預設監聽連接埠為 11211
  通過 memcached.exe -h 可以查看其協助
二   .NET memcached client library
   下載檔案:https://sourceforge.net/projects/memcacheddotnet/

   裡面有.net1.1  和 .net2.0的兩種版本  還有一個不錯的例子。

三  應用

  1 將Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll 等放到bin目錄
  2 引用Memcached.ClientLibrary.dll
  3 代碼

 

   
   1  namespace  Memcached.MemcachedBench
 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               //  獲得用戶端執行個體
33              MemcachedClient mc  =   new  MemcachedClient();
34              mc.EnableCompression  =   false ;
35 
36              Console.WriteLine( " ------------測  試----------- " );
37              mc.Set( " test " ,  " my value " );   // 儲存資料到快取服務器,這裡將字串"my value"緩衝,key 是"test"
38 
39               if  (mc.KeyExists( " test " ))    // 測試緩衝存在key為test的項目
40              {
41                  Console.WriteLine( " test is Exists " );
42                  Console.WriteLine(mc.Get( " test " ).ToString());   // 在緩衝中擷取key為test的項目
43              }
44               else
45              {
46                  Console.WriteLine( " test not Exists " );
47              }
48 
49              Console.ReadLine();
50 
51              mc.Delete( " test " );   // 移除緩衝中key為test的項目
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();   // 關閉池, 關閉sockets
65          }
66      }
67  }   4 運行結果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.