星期日, 11月 22, 2009

asp.net using memcache

安裝Memcache
Step1:下載Memcache (memcached for Win32)
Step2:安裝與啟動Memcache
sc create memcached binPath= "C:\memcached-1.2.1-win32\memcached.exe -p 8888 -l 127.0.0.1 -m 512 -d runservice" DisplayName= "memcached server" start= auto depend= TCPIP
相關參數:

Asp.net設定
1.將取得的MemCache用戶端(download)解壓縮內的三個dll加入參考Enyim.Caching.dll, log4net.dll, MemcachedProviders.dll
2.設定web.config內的configsection加入以下tags
在web.config或app.config裡的<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"/>


3.然後在configuration區塊裡加入下面的詳細設定
<!--Memcatch 詳細設定-->
  <cacheProvider defaultProvider="MemcachedCacheProvider">
    <providers>
      <add name="MemcachedCacheProvider" type="MemcachedProviders.Cache.MemcachedCacheProvider, MemcachedProviders"
      keySuffix="_MySuffix_" defaultExpireTime="2000"/>
    </providers>
  </cacheProvider>

  <enyim.com>
    <memcached>
      <servers>
        <!--Memcatch default port 11211-->
        <add address="127.0.0.1" port="8888" />
      </servers>
      <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" />
    </memcached>
  </enyim.com>

  <log4net>
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}]- %message%newline" />
      </layout>
    </appender>
    <root>
      <priority value="WARN"/>
      <appender-ref ref="ConsoleAppender">
        <filter type="log4net.Filter.LevelRangeFilter">
          <levelMin value="WARN"/>
          <levelMax value="FATAL"/>
        </filter>
      </appender-ref>
    </root>
  </log4net>


*.aspx 範例
        DataTable dt = (DataTable)MemcachedProviders.Cache.DistCache.Get("SearchKey3");
        if (dt == null)
        {
            Response.Write("without cache");
            Module_Command.GetMenuList obj = new Module_Command.GetMenuList(3);
            dt = (System.Data.DataTable)obj.Execute();
            MemcachedProviders.Cache.DistCache.Add("SearchKey3", dt, TimeSpan.FromSeconds(5));
        }
        this.GridView1.DataSource = dt;
        this.GridView1.DataBind();



檢查物件是否有存在memcache

Telnet 到你的伺服器(ex:telnet 127.0.0.1 ),輸入指令'stats',就可以看到目前memcached的狀態。
curr_items 是目前memcached裡暫存物件的數量。

Reference:
在.NET應用程式裡使用memcached暫存Linq資料
MSDNDataContractSerializer Class
如何用 C# 撰寫序列化(Serialize)與反序列化(Deserialize)
用 C# 撰寫序列化(Serialize)與反序列化(Deserialize) - Part 2
.NET 筆記:序列化

沒有留言:

張貼留言

留個話吧:)