HttpContext.Current.Cache 和 HttpRuntime.Cache 对象区别
2012-01-02 20:53
344 查看
HttpContext.Current.Cache 和 HttpRuntime.Cache 对象区别
.NET中Cache有两种调用方式:HttpContext.Current.Cache 和 HttpRuntime.Cache,这两种方式有什么区别呢?我们先看MSDN上的解释:HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象。
HttpRuntime.Cache:获取当前应用程序的Cache。
我们再用.NET Reflector工具看看HttpContext.Cache和HttpRuntime.Cache的实现:
//System.Web.HttpContext.Cache属性实现
public sealed class HttpContext
//System.Web.HttpRuntime.Cache属性实现
public sealed class HttpRuntime
class CacheTest
{
static void Main(string[] args)
{
System.Web.Caching.Cache httpRuntimeCache = System.Web.HttpRuntime.Cache;
httpRuntimeCache.Insert("httpRuntimeCache", "I am stored in HttpRuntime.Cache");
if (httpRuntimeCache != null)
{
Console.WriteLine("httpRuntimeCache:" + httpRuntimeCache["httpRuntimeCache"]);
}
System.Web.HttpContext httpContext = System.Web.HttpContext.Current;
if (httpContext == null)
{
Console.WriteLine("HttpContext object is null in Console Project");
}
else
{
System.Web.Caching.Cache httpContextCache = httpContext.Cache;
httpContextCache.Insert("httpContextCache", "I am stored in HttpRuntime.Cache");
if (httpContextCache == null)
{
Console.WriteLine("httpContextCache is null");
}
}
Console.ReadLine();
}
}
复制代码
输出结果:httpRuntimeCache:I am stored in HttpRuntime.Cache
HttpContext object is null in Console Project
综上:我们在使用Cache时,尽量使用HttpRuntime.Cache,既能减少出错,也减少了一次函数调用。
相关文章推荐
- HttpContext.Current.Cache 和 HttpRuntime.Cache 对象区别
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
- 缓存 HttpContext.Current.Cache和HttpRuntime.Cache的区别
- HttpContext.Current.Cache 和 HttpRuntime.Cache 区别
- HttpContext.Current.Cache 和 HttpRuntime.Cache 区别
- HttpContext.Current.Cache和HttpRuntime.Cache区别[转]
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
- System.Web.HttpContext.Current.Cache和System.Web.HttpRuntime.Cache有什么区别?
- 缓存通用管理类 + 缓存 HttpContext.Current.Cache 和 HttpRuntime.Cache 的区别
- HttpContext.Current.Cache 与 HttpRuntime.Cache有什么区别?
- System.Web.HttpContext.Current.Cache和System.Web.HttpRuntime.Cache有什么区别?
- HttpContext.Current.Cache和HttpRuntime.Cache的区别
- HttpContext.Current.Cache 与HttpRuntime.Cache的区别
- HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching
- HttpContext.Current.Cache vs. HttpRuntime.Cache
- System.Web.HttpContext.Current.Cache 与System.Web.HttpRuntime.Cache
- Cache及(HttpRuntime.Cache与HttpContext.Current.Cache)