HttpContext.Current.Cache 与HttpRuntime.Cache的区别
2016-07-24 16:11
405 查看
HttpContext.Current.Cache:为当前HTTP 请求获取Cache对象。
HttpRuntime.Cache:获取当前应用程序的Cache。
用NET Reflector工具看看HttpContext.Cache和HttpRuntime.Cache的实现:
HttpContext.Cache和HttpRuntime.Cache实现
//System.Web.HttpContext.Cache属性实现
public sealed class
HttpContext
{
public Cache Cache
{
get
{ return HttpRuntime.Cache;
}
}
}
//System.Web.HttpRuntime.Cache属性实现
public sealed class HttpRuntime
{
public static Cache Cache
{
get
{
if (AspInstallDirectoryInternal == null)
{ throw new HttpException(SR.GetString("Aspnet_not_installed", new
object[] { VersionInfo.SystemWebVersion }));
}
Cache cache = _theRuntime._cachePublic;
if (cache == null)
{
CacheInternalcacheInternal = CacheInternal;
CacheSectioncacheSection = RuntimeConfig.GetAppConfig().Cache;
cacheInternal.ReadCacheInternalConfig(cacheSection);
_theRuntime._cachePublic = cacheInternal.CachePublic;
cache = _theRuntime._cachePublic;
}
return cache;
}
}
}
通过上面的代码我们可以看出:
HttpContext.Current.Cache是调用HttpRuntime.Cache实现的,两者指向同一Cache对象。那么两者到底有没有区别的?既然两个指向的是同一Cache对象,两者的差别只能出现在HttpContext和HttpRuntime上了。我们再来看看MSDN中HttpContext和HttpRuntime的定
义。
(1)HttpContext:封装有关个别HTTP请求的所有HTTP特定的信息,HttpContext.Current为当前的HTTP请求获取HttpContext对象。
(2)HttpRuntime:为当前应用程序提供一组ASP.NET运行时服务。
由上面的定义可以看出:
(1)、HttpRuntime.Cache相当于就是一个缓存具体实现类,这个类虽然被放在了System.Web命名空间下,但是非Web应用下也是可以使用。
(2)、HttpContext.Current.Cache是对上述缓存类的封装,由于封装到了HttpContext类中,局限于只能在知道HttpContext下使用,即只能用于Web应用。下面的例子可以很好的说明这一点:
HttpContext.Cache和HttpRuntime.Cache的示例
class CacheTest
{
static void Main(string[] args)
{
System.Web.Caching.CachehttpRuntimeCache =
System.Web.HttpRuntime.Cache;
httpRuntimeCache.Insert("httpRuntimeCache", "I am stored in
HttpRuntime.Cache");
if (httpRuntimeCache != null)
{
Console.WriteLine("httpRuntimeCache:" +
httpRuntimeCache["httpRuntimeCache"]);
}
System.Web.HttpContexthttpContext = System.Web.HttpContext.Current;
if (httpContext == null)
{
Console.WriteLine("HttpContext object is null in Console Project");
}
HttpRuntime.Cache:获取当前应用程序的Cache。
用NET Reflector工具看看HttpContext.Cache和HttpRuntime.Cache的实现:
HttpContext.Cache和HttpRuntime.Cache实现
//System.Web.HttpContext.Cache属性实现
public sealed class
HttpContext
{
public Cache Cache
{
get
{ return HttpRuntime.Cache;
}
}
}
//System.Web.HttpRuntime.Cache属性实现
public sealed class HttpRuntime
{
public static Cache Cache
{
get
{
if (AspInstallDirectoryInternal == null)
{ throw new HttpException(SR.GetString("Aspnet_not_installed", new
object[] { VersionInfo.SystemWebVersion }));
}
Cache cache = _theRuntime._cachePublic;
if (cache == null)
{
CacheInternalcacheInternal = CacheInternal;
CacheSectioncacheSection = RuntimeConfig.GetAppConfig().Cache;
cacheInternal.ReadCacheInternalConfig(cacheSection);
_theRuntime._cachePublic = cacheInternal.CachePublic;
cache = _theRuntime._cachePublic;
}
return cache;
}
}
}
通过上面的代码我们可以看出:
HttpContext.Current.Cache是调用HttpRuntime.Cache实现的,两者指向同一Cache对象。那么两者到底有没有区别的?既然两个指向的是同一Cache对象,两者的差别只能出现在HttpContext和HttpRuntime上了。我们再来看看MSDN中HttpContext和HttpRuntime的定
义。
(1)HttpContext:封装有关个别HTTP请求的所有HTTP特定的信息,HttpContext.Current为当前的HTTP请求获取HttpContext对象。
(2)HttpRuntime:为当前应用程序提供一组ASP.NET运行时服务。
由上面的定义可以看出:
(1)、HttpRuntime.Cache相当于就是一个缓存具体实现类,这个类虽然被放在了System.Web命名空间下,但是非Web应用下也是可以使用。
(2)、HttpContext.Current.Cache是对上述缓存类的封装,由于封装到了HttpContext类中,局限于只能在知道HttpContext下使用,即只能用于Web应用。下面的例子可以很好的说明这一点:
HttpContext.Cache和HttpRuntime.Cache的示例
class CacheTest
{
static void Main(string[] args)
{
System.Web.Caching.CachehttpRuntimeCache =
System.Web.HttpRuntime.Cache;
httpRuntimeCache.Insert("httpRuntimeCache", "I am stored in
HttpRuntime.Cache");
if (httpRuntimeCache != null)
{
Console.WriteLine("httpRuntimeCache:" +
httpRuntimeCache["httpRuntimeCache"]);
}
System.Web.HttpContexthttpContext = System.Web.HttpContext.Current;
if (httpContext == null)
{
Console.WriteLine("HttpContext object is null in Console Project");
}
相关文章推荐
- HTTP学习笔记2
- https分析
- 详解OkHttp
- Python学习笔记17:网络客户端编程
- Python学习笔记16:网络编程
- Android studio使用HttpClient出错
- WCF入门讲解
- 网络虚拟化之FlowVisor:网络虚拟层(下)
- HTTP报文
- HTTP深入浅出 http请求
- Android中okhttp的基本用法1的基础上进行封装
- http状态返回代码
- HTTP学习笔记
- openstack 网络
- Android 网络编程 Socket Http
- 浏览器HTTP请求分析
- TCP的三次握手和四次挥手
- HDU 1532 Drainage Ditches (最大网络流)
- golang的垃圾回收与Finalizer——tcp连接是如何被自动关闭的
- Error fetching https://ruby.taobao.org/:RubySass淘宝镜源无效解决