您的位置:首页 > 理论基础 > 计算机网络

HttpRuntime.Cache vs. HttpContext.Current.Cache

2008-10-08 21:02 549 查看
 
已经有人说过这个话题,相关链接:

HttpRuntime.Cache vs. HttpContext.Current.Cache
http://weblogs.asp.net/pjohnson/archive/2006/02/06/437559.aspx

HttpContext.Cache和HttpRuntime.Cache
http://blog.joycode.com/dotey/archive/2005/01/15/43091.aspx

我这里要说的是从另外一个角度来说:

一,两个实现代码的差异:
我们用 .NET Reflector  看 HttpContext 类的 Cache 属性 ,会看到如下代码:



public Cache Cache




...{


get




...{


return HttpRuntime.Cache;


}


}
所以,两者在代码上是完全一致的。

二、两者的差异其实在于 HttpContext.Current

用 .NET Reflector  看 HttpContext.Current 如下:

public static HttpContext Current




...{


get




...{


return (ContextBase.Current as HttpContext);


}


}
ContextBase 类的静态属性  Current 如下:


internal static object Current




...{


get




...{


return CallContext.HostContext;


}


}

CallContext 类的静态属性 HostContext 如下:


public static object HostContext




...{


get




...{


IllogicalCallContext context1 = Thread.CurrentThread.GetIllogicalCallContext();


object obj1 = context1.HostContext;


if (obj1 == null)




...{


LogicalCallContext context2 = CallContext.GetLogicalCallContext();


obj1 = context2.HostContext;


}


return obj1;


}


}
显然,非 Web 应用 HttpContext.Current 返回 null 是因为
ContextBase.Current as HttpContext 这么一句,返回的 null 。

ContextBase.Current 是有值的,但是由于非 Web 应用,返回的 Object 无法转换为HttpContext,而返回 null 的。

所以, HttpContext.Current.Cache 只可能用于 Web 应用的缓存。 而且是跟 HttpContext 紧密联系的。

三、HttpRuntime.Cache 可用于非 Web 应用的缓存。

比如我如下的一个控制台程序,是可以正常读写缓存的。而这里当然是不可以使用  HttpContext.Cache 的。

static void Main(string[] args)




...{


System.Web.Caching.Cache c = System.Web.HttpRuntime.Cache;


if (c != null)




...{


c.Insert("1", "123141432432");




object o = c.Get("1");


Console.WriteLine(o);




}


Console.ReadLine();


}



总结,
1、HttpRuntime.Cache 相当于就是一个缓存具体实现类,这个类虽然被放在了 System.Web 命名空间下了。但是非 Web 应用也是可以拿来用的。
2、HttpContext.Cache 是对上述缓存类的封装,由于封装到了 HttpContext ,局限于只能在知道 HttpContext 下使用,即只能用于 Web 应用。

综上所属,在可以的条件,尽量用  HttpRuntime.Cache ,而不是用  HttpContext.Cache 。




# re: HttpRuntime.Cache vs. HttpContext.Current.Cache
www.suanshu.net/blog转载了此文
2006/9/4 13:25 | rthy




# 回复: HttpRuntime.Cache vs. HttpContext.Current.Cache
分析得太好了!
以下是msdn中的一段代码
using System;
using System.Web;
using System.Web.Caching;
public static class ReportManager
{
private static bool _reportRemovedFromCache = false;
static ReportManager()
{ }

public static String GetReport()
{
lock (typeof(ReportManager))
{
if (HttpContext.Current.Cache["MyReport"] != null)
return (string)HttpRuntime.Cache["MyReport"];
else
{
CacheReport();
return (string)HttpRuntime.Cache["MyReport"];
}
}
}

public static void CacheReport()
{
lock (typeof(ReportManager))
{
HttpContext.Current.Cache.Add("MyReport",
CreateReport(), null, DateTime.MaxValue,
new TimeSpan(0, 1, 0),
System.Web.Caching.CacheItemPriority.Default,
ReportRemovedCallback);
}
}

private static string CreateReport()
{
System.Text.StringBuilder myReport =
new System.Text.StringBuilder();
myReport.Append("Sales Report<br />");
myReport.Append("2005 Q2 Figures<br />");
myReport.Append("Sales NE Region - $2 million<br />");
myReport.Append("Sales NW Region - $4.5 million<br />");
myReport.Append("Report Generated: " + DateTime.Now.ToString()
+ "<br />");
myReport.Append("Report Removed From Cache: " +
_reportRemovedFromCache.ToString());
return myReport.ToString();
}

public static void (String key, object value,
CacheItemRemovedReason removedReason)
{
_reportRemovedFromCache = true;
CacheReport();
}
}

每次从ReportRemovedCallback方法中调用CasheReport方法,运行到HttpContext.Current.Cache.Add都会发现Current为NULL,导致异常。看楼主一分析,明白了!
谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息