HttpContext.Current.Cache 和 HttpRuntime.Cache
2015-04-06 22:33
295 查看
.NET中Cache有两种调用方式:HttpContext.Current.Cache 和 HttpRuntime.Cache,这两种方式有什么区别呢?我们先看MSDN上的解释:
HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象。
HttpRuntime.Cache:获取当前应用程序的Cache。
我们再用.NET Reflector工具看看HttpContext.Cache和HttpRuntime.Cache的实现:
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//System.Web.HttpContext.Cache属性实现
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public sealed class HttpContext
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Cache Cache
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
get
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return HttpRuntime.Cache;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//System.Web.HttpRuntime.Cache属性实现
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public sealed class HttpRuntime
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Cache Cache
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
get
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (AspInstallDirectoryInternal == null)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throw new HttpException(SR.GetString("Aspnet_not_installed", new object[] { VersionInfo.SystemWebVersion }));
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Cache cache = _theRuntime._cachePublic;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (cache == null)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CacheInternal cacheInternal = CacheInternal;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CacheSection cacheSection = RuntimeConfig.GetAppConfig().Cache;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cacheInternal.ReadCacheInternalConfig(cacheSection);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_theRuntime._cachePublic = cacheInternal.CachePublic;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cache = _theRuntime._cachePublic;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return cache;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
通过上面的代码我们可以看出:HttpContext.Current.Cache是调用HttpRuntime.Cache实现的,两者指向同一Cache对象。那么两者到底有没有区别的?既然两个指向的是同一Cache对象,两者的差别只能出现在HttpContext和HttpRuntime上了。
我们再来看看MSDN中HttpContext和HttpRuntime的定义。
HttpContext:封装有关个别HTTP请求的所有HTTP特定的信息,HttpContext.Current为当前的HTTP请求获取HttpContext对象。
HttpRuntime:为当前应用程序提供一组ASP.NET运行时服务。
由上面的定义可以看出:HttpRuntime.Cache相当于就是一个缓存具体实现类,这个类虽然被放在了System.Web命名空间下,但是非Web应用下也是可以使用;HttpContext.Current.Cache是对上述缓存类的封装,由于封装到了HttpContext类中,局限于只能在知道HttpContext下使用,即只能用于Web应用。
下面的例子可以很好的说明这一点:
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class CacheTest
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static void Main(string[] args)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Web.Caching.Cache httpRuntimeCache = System.Web.HttpRuntime.Cache;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
httpRuntimeCache.Insert("httpRuntimeCache", "I am stored in HttpRuntime.Cache");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (httpRuntimeCache != null)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("httpRuntimeCache:" + httpRuntimeCache["httpRuntimeCache"]);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Web.HttpContext httpContext = System.Web.HttpContext.Current;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (httpContext == null)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("HttpContext object is null in Console Project");
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
else
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Web.Caching.Cache httpContextCache = httpContext.Cache;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
httpContextCache.Insert("httpContextCache", "I am stored in HttpRuntime.Cache");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (httpContextCache == null)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("httpContextCache is null");
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.ReadLine();
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
输出结果:httpRuntimeCache:I am stored in HttpRuntime.Cache
HttpContext object is null in Console Project
综上:我们在使用Cache时,尽量使用HttpRuntime.Cache,既能减少出错,也减少了一次函数调用。
参考资料:HttpRuntime.Cache 与HttpContext.Current.Cache的疑问,HttpRuntime.Cache
vs. HttpContext.Current.Cache
HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象。
HttpRuntime.Cache:获取当前应用程序的Cache。
我们再用.NET Reflector工具看看HttpContext.Cache和HttpRuntime.Cache的实现:
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//System.Web.HttpContext.Cache属性实现
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public sealed class HttpContext
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Cache Cache
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
get
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return HttpRuntime.Cache;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//System.Web.HttpRuntime.Cache属性实现
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public sealed class HttpRuntime
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static Cache Cache
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
get
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (AspInstallDirectoryInternal == null)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throw new HttpException(SR.GetString("Aspnet_not_installed", new object[] { VersionInfo.SystemWebVersion }));
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Cache cache = _theRuntime._cachePublic;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (cache == null)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CacheInternal cacheInternal = CacheInternal;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CacheSection cacheSection = RuntimeConfig.GetAppConfig().Cache;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cacheInternal.ReadCacheInternalConfig(cacheSection);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
_theRuntime._cachePublic = cacheInternal.CachePublic;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cache = _theRuntime._cachePublic;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return cache;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
通过上面的代码我们可以看出:HttpContext.Current.Cache是调用HttpRuntime.Cache实现的,两者指向同一Cache对象。那么两者到底有没有区别的?既然两个指向的是同一Cache对象,两者的差别只能出现在HttpContext和HttpRuntime上了。
我们再来看看MSDN中HttpContext和HttpRuntime的定义。
HttpContext:封装有关个别HTTP请求的所有HTTP特定的信息,HttpContext.Current为当前的HTTP请求获取HttpContext对象。
HttpRuntime:为当前应用程序提供一组ASP.NET运行时服务。
由上面的定义可以看出:HttpRuntime.Cache相当于就是一个缓存具体实现类,这个类虽然被放在了System.Web命名空间下,但是非Web应用下也是可以使用;HttpContext.Current.Cache是对上述缓存类的封装,由于封装到了HttpContext类中,局限于只能在知道HttpContext下使用,即只能用于Web应用。
下面的例子可以很好的说明这一点:
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
class CacheTest
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
static void Main(string[] args)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Web.Caching.Cache httpRuntimeCache = System.Web.HttpRuntime.Cache;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
httpRuntimeCache.Insert("httpRuntimeCache", "I am stored in HttpRuntime.Cache");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (httpRuntimeCache != null)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("httpRuntimeCache:" + httpRuntimeCache["httpRuntimeCache"]);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Web.HttpContext httpContext = System.Web.HttpContext.Current;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (httpContext == null)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("HttpContext object is null in Console Project");
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
else
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
System.Web.Caching.Cache httpContextCache = httpContext.Cache;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
httpContextCache.Insert("httpContextCache", "I am stored in HttpRuntime.Cache");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (httpContextCache == null)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.WriteLine("httpContextCache is null");
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Console.ReadLine();
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
输出结果:httpRuntimeCache:I am stored in HttpRuntime.Cache
HttpContext object is null in Console Project
综上:我们在使用Cache时,尽量使用HttpRuntime.Cache,既能减少出错,也减少了一次函数调用。
参考资料:HttpRuntime.Cache 与HttpContext.Current.Cache的疑问,HttpRuntime.Cache
vs. HttpContext.Current.Cache
相关文章推荐
- HttpContext.Current.Cache 和 HttpRuntime.Cache 区别
- System.Web.HttpContext.Current.Cache和System.Web.HttpRuntime.Cache有什么区别?
- [转]HttpContext.Current.Cache 和 HttpRuntime.Cache
- System.Web.HttpContext.Current.Cache和System.Web.HttpRuntime.Cache有什么区别?
- HttpContext.Current.Cache和HttpRuntime.Cache区别[转]
- HttpContext.Current.Cache vs. HttpRuntime.Cache
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
- HttpContext.Current.Cache vs. HttpRuntime.Cache
- Cache及(HttpRuntime.Cache与HttpContext.Current.Cache)
- System.Web.HttpContext.Current.Cache与System.Web.HttpRuntime.Cache指向同一个引用
- System.Web.HttpContext.Current.Cache 与System.Web.HttpRuntime.Cache
- HttpContext.Current.Cache vs. HttpRuntime.Cache
- HttpContext.Current.Cache 与HttpRuntime.Cache的区别
- HttpContext.Current.Cache和HttpRuntime.Cache的区别
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
- HttpContext.Current.Cache 和 HttpRuntime.Cache 对象区别
- HttpRuntime.Cache vs. HttpContext.Current.Cache
- HttpContext.Current.Cache 和 HttpRuntime.Cache
- HttpContext.Current.Cache vs. HttpRuntime.Cache