SQL Server 2000Visual C++ 7.1Windows VistaDBAVisual Studio .NET 2003Windows 2003.NET 1.1Windows 2000Windows XPSQL Server 2005中级开发Visual StudioSQL ServerSQLWindowsC++.NETASP.NETC#
使用缓存通过会话提高性能






3.64/5 (7投票s)
2005 年 7 月 20 日
2分钟阅读

92155

516
我写这篇简短的文章是为了改进您的 ASP.NET 应用程序会话,通过使用缓存。
引言
这是一篇很短的文章,旨在帮助那些使用 SQL 会话的人。众所周知,将会话存储在您的 SQL Server 上是个好主意,但也是一个性能噩梦。如果您像我一样经常使用会话,那么您的 SQL Server 会非常忙碌地在这里和那里检索会话。所以让我向您展示我是如何避免往返 SQL Server 并更多地使用内存缓存的。
会话 InProc vs. In Cache
我更喜欢尽可能多地使用内存缓存,因为它比每次需要信息时都访问 SQL Server 要快。现在我确信您在想,只需使用会话 InProc。嗯,我正在尽量避免这种情况。为什么?如果您使用过 InProc,您可以用一句话回答这个问题。如果您想将会话存储在您的 SQL Server 上,那是有原因的(Web 场或 Web 园)。
缓存不适用于 Web 场
我知道!但是,如果与 SQL Server 一起使用,它会变得更有趣和更快。要检索会话,我们将首先检查本地缓存中是否有一个副本,如果没有,我们将不得不访问 SQL Server。
让我们编写代码!
using System;
//Normally this is how we store and retrieve sessions
Session["Test"] = "Storing a string";
//Retrieving a string
string sTemp = Session["Test"];
所以现在让我们用缓存来实现它
AlsUtils.BetterSession pSession = new BetterSession();
// Store 2 keys
pSession["test"] = "hello";
pSession["bye"] = "Good Bye";
// Retrive them
string sTemp = pSession["test"];
string sTemp2 = pSession["bye"];
//Clear the cache at logout
pSession.ClearSession();
使一切成为可能的简单类
using System;
using System.Collections;
namespace AlsUtils
{
/// <SUMMARY>
/// Summary description for BetterSession.
/// </SUMMARY>
public class BetterSession : DictionaryBase
{
public object this[string key]
{
get
{
if (System.Web.HttpContext.Current.Cache[
System.Web.HttpContext.Current.Session.SessionID +
"~" + key] != null )
return(System.Web.HttpContext.Current.Cache[
System.Web.HttpContext.Current.Session.SessionID
+ "~" + key]);
else if (System.Web.HttpContext.Current.Session[key] != null )
{
System.Web.HttpContext.Current.Cache[
System.Web.HttpContext.Current.Session.SessionID
+ "~" + key] =
System.Web.HttpContext.Current.Session[key];
Add(System.Web.HttpContext.Current.Session.SessionID
+ "~" + key, System.Web.HttpContext.Current.Cache[
System.Web.HttpContext.Current.Session.SessionID
+ "~" + key]);
return(System.Web.HttpContext.Current.Session[key]);
}
return (null);
}
set
{
System.Web.HttpContext.Current.Cache[
System.Web.HttpContext.Current.Session.SessionID
+ "~" + key] = value;
System.Web.HttpContext.Current.Session[key] = value;
Add(System.Web.HttpContext.Current.Session.SessionID
+ "~" + key, value);
}
}
public void ClearSession()
{
string sSessionID =
System.Web.HttpContext.Current.Session.SessionID;
ArrayList pArray = new ArrayList(Keys);
for ( int i=0; i<pArray.Count; i++)
{
if ( pArray[i].ToString().IndexOf(sSessionID+"~") == 0)
System.Web.HttpContext.Current.Cache.Remove(
pArray[i].ToString());
}
}
//add a string based on key
protected void Add(string key, object oValue)
{
this.Dictionary.Add(key, oValue);
}
//see if collection contains an entry corresponding to key
protected bool Contains(string key)
{
return this.Dictionary.Contains(key);
}
//we will get to this later
protected ICollection Keys
{
get {return this.Dictionary.Keys;}
}
}
}
结论
嗯,这就是想法。有一件非常重要的事情要记住,缓存没有时间限制,所以如果您希望缓存在 30 分钟后自行销毁,您必须更改将其添加到以下内容的行:
System.Web.HttpContext.Current.Cache.Insert(sSessionID+"~"+key,
value, null, System.Web.Caching.Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes(30));
如果您的应用程序在一开始设置所有会话,然后您使用会话来读取该用户的配置,那么这个简单的类将为您节省大量往返 SQL 会话数据库的次数。
希望这对某些人有帮助。