標籤:style blog http io color ar 使用 for sp
本文來自:http://www.cnblogs.com/yukaizhao/archive/2008/11/10/memcached_client_usage.html
一. 背景:
在大訪問量的web程式開發中,資料庫常常會稱為效能的瓶頸。為了緩解資料庫的壓力,我們頻繁的使用緩衝,而asp.net內建的Cache很強大,但是有先天的不足,它是進程內的緩衝,當網站由多台伺服器負載平衡時,當緩衝在有資料更新時,我們不能同時將更新後的資料同步到兩台或多台web server上。所幸的是老外的大牛開發了memcached分布式緩衝,它的效能非凡,memcached常用的.net的client類庫有兩個分別是:http://code.google.com/p/beitmemcached/,http://sourceforge.net/projects/memcacheddotnet/,我個人推薦使用後一個。
有關memcached的介紹和在控制台中的簡單調用請參考:分布式緩衝系統Memcached簡介與實踐
二. 如何使用
- 安裝memcached server端,下載dot net版本的用戶端,請參考分布式緩衝系統Memcached簡介與實踐
- 在asp.net項目中使用memcached用戶端訪問服務端
思路:
1) 我們在網站啟動時即啟動memcached的client端,在網站停止時停掉client端,釋放佔用的連接埠資源;
這一點只要把啟動和關閉放到Global的Application_Start和Application_End中執行,即可。由於整個網站的運行期間,我們都希望不需要再重建MemcachedClient類的執行個體,所以我們在Global中聲明了一個靜態執行個體來存放此Client。並用一個public的屬性RemoteCache和IsRemoteCacheAvailable來暴露MemcachedClient的引用和其是否可用。
2) 為了方便使用我們需要建一個System.Web.UI.Page類的基類PageBase,在此基類中引用Memcached client的用戶端訪問類執行個體,然後建立的所有aspx頁面的基類繼承PageBase就可以了。
這兒也很容已做到,我們只需找到 Global appInstance = HttpContext.Current.ApplicationInstance as Global;然後引用Global中暴露的兩個屬性即可。需要注意的是在程式中引用Global是通過HttpContext.Current.ApplicationInstance而不是HttpContext.Current.Applicatioin,後者只是一個索引值對的集合,而前者是網站的HttpApplication的執行個體
請參考Global和PageBase的代碼:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Memcached.ClientLibrary;
using log4net;
namespace Forum.UI
{
public class Global : System.Web.HttpApplication
{
protected static ILog AppLog = LogManager.GetLogger(typeof(Global));
public override void Init()
{
this.Error += new EventHandler(Application_Error);
base.Init();
}
protected void Application_Error(object sender, EventArgs e)
{
#if !DEBUG
Exception ex = Server.GetLastError();
AppLog.Error(ex.Message, ex);
Server.ClearError();
Response.Redirect("~/error/500.htm", true);
#endif
}
protected void Application_Start(object sender, EventArgs e)
{
SetupMemcachedClient();
}
protected void Application_End(object sender, EventArgs e)
{
ShutdownMemecachedClient();
}
#region RemoteCache
private static MemcachedClient mc = null;
private const string MEMCACHED_INSTANCE_NAME = "Memcached-Forum";
/// <summary>
/// 返回MemcachedClient是否可用
/// </summary>
public bool IsRemoteCacheAvailable
{
get
{
return mc != null;
}
}
/// <summary>
/// 外部存取入口
/// </summary>
public MemcachedClient RemoteCache
{
get
{
return mc;
}
}
/// <summary>
/// 關閉佔用的tcp連接埠資源
/// </summary>
private void ShutdownMemecachedClient()
{
SockIOPool pool = SockIOPool.GetInstance(MEMCACHED_INSTANCE_NAME);
if (pool != null) pool.Shutdown();
}
/// <summary>
/// 啟動memcachedClient,給client賦予指定參數
/// </summary>
private void SetupMemcachedClient()
{
string memcachedServers = ConfigurationManager.AppSettings["memcachedServers"];
string[] servers = memcachedServers.Split(‘;‘);
SockIOPool pool = SockIOPool.GetInstance(MEMCACHED_INSTANCE_NAME);
pool.SetServers(servers);
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();
mc = new MemcachedClient();
mc.PoolName = MEMCACHED_INSTANCE_NAME;
mc.EnableCompression = false;
}
#endregion
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
using Memcached.ClientLibrary;
using System.Collections.Generic;
using log4net;
using Forum.Models;
using Forum.UI.Proxy;
namespace Forum.UI
{
public class PageBase : Page
{
#region RemoteCache & IsRemoteCacheAvailable
protected MemcachedClient RemoteCache
{
get
{
if (HttpContext.Current.ApplicationInstance == null) return null;
Global appInstance = HttpContext.Current.ApplicationInstance as Global;
if (appInstance == null) return null;
return appInstance.RemoteCache;
}
}
protected bool IsRemoteCacheAvailable
{
get
{
return RemoteCache != null;
}
}
#endregion
}
}
在web應用程式中使用MemcachedClient