C#/.net分布式緩衝系統Memcached 執行個體

來源:互聯網
上載者:User
.net/c#分布式緩衝系統Memcached簡介與實踐--非常好

緣起: 在資料驅動的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 代碼

  後記: 是個不錯的東西 ,使用起來也很方便,php ,ruby 的項目中用這個的很多,但是.net項目中用的較少(恕俺孤陋寡聞) 。希望有興趣的朋友們 多多交流 。 看到頁首了麼各位兄弟? 不用我多說了吧,

namespace Memcached.MemcachedBench
 {
     using System;
     using System.Collections;
 
     using Memcached.ClientLibrary;
 
     public class MemcachedBench 
      {
          [STAThread]
         public static void Main(String[] args) 
          {
             string[] serverlist = { "10.0.0.131:11211", "10.0.0.132:11211" };
 
             //初始化池
              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();
 
             // 獲得用戶端執行個體
              MemcachedClient mc = new MemcachedClient();
              mc.EnableCompression = false;
 
              Console.WriteLine("------------測   試-----------");
              mc.Set("test", "my value");  //儲存資料到快取服務器,這裡將字串"my value"緩衝,key 是"test"
 
             if (mc.KeyExists("test"))   //測試緩衝存在key為test的項目
              {
                  Console.WriteLine("test is Exists");
                  Console.WriteLine(mc.Get("test").ToString());  //在緩衝中擷取key為test的項目
              }
             else
              {
                  Console.WriteLine("test not Exists");
              }
 
              Console.ReadLine();
 
              mc.Delete("test");  //移除緩衝中key為test的項目
 
             if (mc.KeyExists("test"))
              {
                  Console.WriteLine("test is Exists");
                  Console.WriteLine(mc.Get("test").ToString());
              }
             else
              {
                  Console.WriteLine("test not Exists");
              }
              Console.ReadLine();
             
              SockIOPool.GetInstance().Shutdown();  //關閉池, 關閉sockets
          }
      }
 }

聯繫我們

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