分布式緩衝系統Memcached在Asp.net下的應用

來源:互聯網
上載者:User

標籤:代碼下載   大型網站   water   shm   mil   守護   blog   ext   table   

Memcached 是一個高效能的分布式記憶體對象緩衝系統,用於動態Web應用以減輕資料庫負載。它通過在記憶體中快取資料和對象來減少讀取資料庫的次數,從而提高動態、資料庫驅動網站的速度。Memcached基於一個儲存鍵/值對的hashmap。其守護進程(daemon )是用C寫的,但是用戶端可以用任何語言來編寫,並通過memcached協議與守護進程通訊。

站下的session效能並不高,所以造成人們一種印象,大型WEB項目使用Java的錯覺,致使很多人吐槽微軟不給力,其實這好比拉不出怪地球引力,本文介紹Memcached在ASP.NET Web項目中的應用,智聯招聘,招商銀行,農業銀行等都是採用解決方案,在效能上是絕對不亞於任何大型網站.同時Memcached還能很方便建立起伺服器叢集,對於大型解決方案,伺服器叢集的重要性不言而喻;

1.準備工作.
要在項目中使用到Memcached,需要準備好如下條件:
伺服器環境:安裝Memcached服務到伺服器上
a.下載Memcached安裝檔案
b.以管理員身份運行CMD 在下載的Memcached服務安裝路徑下安裝Memcached服務(命令列:X:\memcached.exe -d install)
C.檢查服務安裝

d.啟動服務 命令列 memcached.exe –d start  當然可以直接在電腦服務管理來操作
到這裡Memcached服務就搭建完成了,那麼如何運用到.NET項目中區呢?
2.下載.NET Memcached lbr
從檔案..trunk\clientlib\src\clientlib\bin\2.0\Debug下拷貝出4個DLL檔案Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll,添加引用到項目中去
3.初始化Memcached

 

從“服務”中可以看到Memcached Server處於“正在運行”的狀態中,如果是停止的,右鍵“啟動服務”就可以了,當然可以memcached.exe –d start來啟動

 

 

 

主要的緩衝代碼:

using System;using System.Data;using System.Web;using Memcached.ClientLibrary;public class Cache_Info{    private readonly static string CacheKey = "Info_key";    /// <summary>    /// 緩衝是否存在    /// </summary>    /// <param name="pMC"></param>    /// <param name="pKey"></param>    /// <returns></returns>    private static bool IsCache(MemcachedClient pMC,string pKey)     {        if (pMC.KeyExists(pKey))        {            return true;        }        else         {            return false;        }    }    /// <summary>    /// 覆蓋緩衝    /// </summary>    /// <param name="pKey"></param>    /// <param name="pObject"></param>    /// <returns></returns>    private static bool StoreCache(string pKey,object pObject)     {        MemcachedClient mc = new MemcachedClient();        mc.EnableCompression = true;        bool _result = false;        if (IsCache(mc, pKey))        {            if (mc.Get(pKey) == null)            {                mc.Set(pKey, pObject);//緩衝存在,強行覆蓋            }            else             {                mc.Replace(pKey, pObject);//緩衝存在,強行覆蓋            }            _result = true;        }        else         {            mc.Add(pKey, pObject);//第一次載入緩衝            _result = true;        }        return _result;    }    /// <summary>    /// 清除緩衝    /// </summary>    /// <param name="pKey"></param>    /// <returns></returns>    public static bool RemoveCache(string pKey)     {        MemcachedClient mc = new MemcachedClient();        mc.EnableCompression = true;        return mc.Delete(pKey);    }    /// <summary>    /// 擷取資料    /// </summary>    /// <returns></returns>    public static DataTable GetInfo()    {        #region 通過緩衝來擷取DataTable的資料        MemcachedClient mc = new MemcachedClient();        mc.EnableCompression = true;        if (mc.Get(CacheKey) != null)        {            return mc.Get(CacheKey) as DataTable; //直接從緩衝取資料        }        else        {            DataTable dt=DB_Info.GetInfo();  //第一次載入將資料存入緩衝中            if (StoreCache(CacheKey, dt))            {                return mc.Get(CacheKey) as DataTable;            }            else            {                return null;            }        }        #endregion        #region 直接從資料庫擷取DataTable        //return DB_Info.GetInfo();        #endregion    }          }

代碼:

Memcached code

分布式緩衝系統Memcached在Asp.net下的應用

聯繫我們

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