.net下使用memcached分佈式緩存數據

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

本文主要是在vs2010下,使用Memcached Providers 1.2 .NET 3.5操作memcached緩存數據。按照如下步驟進行:

1.添加引用

2.web.config配置文件

configuration下首個節點(必須是首個節點不然會報錯,配置過configSections節點的人應該知道)

<configSections>
    <section name="cacheProvider" type="MemcachedProviders.Cache.CacheProviderSection, MemcachedProviders"
        allowDefinition="MachineToApplication" restartOnExternalChanges="true"/>
    <sectionGroup name="enyim.com">
      <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
    </sectionGroup>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>
 <enyim.com>
    <memcached>
      <servers>
        <!-- 服務器地址及端口號-->
        <add address="127.0.0.1" port="11211" />
      </servers>
      <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" />
    </memcached>
  </enyim.com>
  <cacheProvider defaultProvider="MemcachedCacheProvider">
    <providers>
      <add name="MemcachedCacheProvider"
           type="MemcachedProviders.Cache.MemcachedCacheProvider, MemcachedProviders"
           keySuffix="_MySuffix_" defaultExpireTime="2000"/>
    </providers>
  </cacheProvider>
2.添加引用

using MemcachedProviders.Cache;
using Enyim.Caching;
3.簡單操作
             MemcachedClient client = new MemcachedClient("enyim.com/memcached");
            client.Store(Enyim.Caching.Memcached.StoreMode.Set,"test1","hello你好");//存儲方式1
            var a= client.Get("test1");//獲取值方式1
            string aa = null;
           bool isOk= DistCache.Add("aa", null);//爲null不能存儲,存儲會失敗
            aa = "bb";
            aa = DistCache.Get<string>("aa");//獲取存儲值方式2
            bool storeBool = false;
            isOk = DistCache.Add("aa",storeBool);//存儲值方式2
            storeBool = DistCache.Get<bool>("aa");//獲取值方式2
            string[] arry = {"aa","bb" };
            isOk = DistCache.Add("arry",arry);//存儲數組
            string[] arry2 = DistCache.Get<string[]>("arry");//獲取數組值

4.針對對象操作

新建一個類

    [Serializable()]//c#序列化,必須要的,不然會存儲失敗
    public class Product
    {

        public int ProductId { get; set; }

        public string ProductName { get; set; }

        public string ProductCompany { get; set; }

        public DateTime SignDate { get; set; }

        public DateTime UpdateData { get; set; }

    }
往memcached添加Product對象

 public IList<Product> GetAllPersonByEntity()
        {
            MemcachedClient client = new MemcachedClient("enyim.com/memcached"); 
            IList<Product> list = DistCache.Get<IList<Product>>("productPersonList");
            if (list==null)
            {
                list = new IList<Product>();
                 Product pr1 = new Product();
                pr1.ProductCompany = "A公司";
                pr1.ProductName = "A產品";
                pr1.SignDate = DateTime.Now;
                pr1.UpdateData = DateTime.Now;
                list.Add(pr1);//可以添加多個值
 bool isOk = DistCache.Add("productPersonList", list);//往緩存中添加product對象
            }
            return list;
        }

以上就是在.net下對memcached進行的簡單操作。



發佈了32 篇原創文章 · 獲贊 4 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章