缓存 HttpContext.Current.Cache和HttpRuntime.Cache的区别
先看MSDN上的解释:
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)
{
CacheInternal cacheInternal = CacheInternal;
CacheSection cacheSection = 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的定义。
HttpContext:封装有关个别HTTP请求的所有HTTP特定的信息,HttpContext.Current为当前的HTTP请求获取HttpContext对象。
HttpRuntime:为当前应用程序提供一组ASP.NET运行时服务。 由上面的定义可以看出:HttpRuntime.Cache相当于就是一个缓存具体实现类,这个类虽然被放在了System.Web命名空间下,但是非Web应用下也是可以使用;HttpContext.Current.Cache是对上述缓存类的封装,由于封装到了HttpContext类中,局限于只能在知道HttpContext下使用,即只能用于Web应用。 下面的例子可以很好的说明这一点: //HttpContext.Cache和HttpRuntime.Cache的示例
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,既能减少出错,也减少了一次函数调用。 参考资料:HttpRuntime.Cache 与HttpContext.Current.Cache的疑问,HttpRuntime.Cache vs. HttpContext.Current.Cache
缓存 HttpContext.Current.Cache和HttpRuntime.Cache的区别的更多相关文章
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cac ...
- HttpContext.Current.Cache 和 HttpRuntime.Cache 区别
原文地址:http://blog.csdn.net/avon520/article/details/4872704 .NET中Cache有两种调用方式:HttpContext.Current.Cach ...
- HttpContext.Current.Cache 和 HttpRuntime.Cache
HttpRuntime.Cache:用于winfrom 和 web HttpContext.Current.Cache 用于web .NET中Cache有两种调用方式:HttpContext.Curr ...
- Cache及(HttpRuntime.Cache与HttpContext.Current.Cache)
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/avon520/archive/2009/11/25/4872704.aspx .NET中Cache有两种调用方式:Ht ...
- HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching
先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cac ...
- Asp.Net framework 类库 自带的缓存 HttpRuntime.Cache HttpContext.Cache
两个Cache 在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序 ...
- ASP.NET HttpRuntime.Cache缓存类使用总结
1.高性能文件缓存key-value存储—Redis 2.高性能文件缓存key-value存储—Memcached 备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出的博文地 ...
- HttpRuntime.Cache .Net自带的缓存类
.Net自带的缓存有两个,一个是Asp.Net的缓存 HttpContext.Cache,一个是.Net应用程序级别的缓存,HttpRuntime.Cache. MSDN上有解释说: HttpCont ...
- Asp.net中的Cache--HttpRuntim.Cache 和 HttpContext.Current.Cache
在ASP.NET中有两个类都提供缓存支持, 一个是HttpRuntime类的Cache属性, 另一个是HttpContext类的Cache属性. 通过查看这两个属性的类型可以发现其实这两个属性都是Sy ...
随机推荐
- wamp服务下部署禅道或其它项目时访问缓慢的解决办法
原因其实很简单: WAMP服务默认是不支持外网访问的,如果公司内外网在一起就会引起缓慢甚至超时的问题,直接修改WAPM的配置文件让它可以访问外网即可解决问题. 解决的方法/步骤 1.解决办法: ...
- 解决不能访问远程mysql的问题
一般是没有给用户访问权限 给用户test_user授权,让他可以从外部登陆和本地登陆注意:@左边是用户名,右边是域名.IP和%,表示可以访问mysql的域名和IP,%表示外部任何地址都能访问. m ...
- Linux 传输文件
不同的Linux之间copy文件通常有4种方法: ftp samba服务 sftp scp 我使用了scp在同网段的内网 ip之间传输,非常快.所以先介绍下scp的方法: scp 是 ssh管道下的c ...
- springMVC数据验证出现404错误解决办法
今天使用springMVC的数据验证的时候,看似很简单的东西,却有一个很大的陷阱:提交空表单的时候总是出现404错误,但是后台却不给你报任何错.遇到这个错误这个很苦恼,搞了几小时,今天记录并分享一下解 ...
- 【bzoj1499】 NOI2005—瑰丽华尔兹
http://www.lydsy.com/JudgeOnline/problem.php?id=1499 (题目链接) 题意 给出一个n*m的地图,刚开始人在(x,y),每次给出一段区间(l,r,t) ...
- CCTray配置如何添加远程服务器
前提: Windows防火墙必须开通的TCP端口 或者直接把防火墙关闭(不建议) 或者直接在防火墙规则增加CCNET的服务进去 总者,只要确保能telnet ip 21234能通即可 建议全部软件都装 ...
- Ubuntu各文件夹功能说明
通常情况下,根文件系统所占空间一般应该比较小,因为其中的绝大部分文件都不需要经常改动,而且包括严格的文件和一个小的不经常改变的文件系统不容易损坏.除了可能的一个叫/vmlinuz标准的系统引导映像之外 ...
- Linux UserSpace Back-Door、Rootkit SSH/PAM Backdoor Attack And Defensive Tchnology
catalog . 引言 . Pam后门 . SSH后门 . Hijacking SSH . Hijacking SSH By Setup A Tunnel Which Allows Multiple ...
- JSP表单提交中文乱码解决方案
分2种提交方式,解决方案不同: 1.form表单提交方式为get 乱码: 解决方案: 因为get方法是参数在URL中显示,因为tomcat的URL编码默认是:IOS-8859-1所以要么改tomcat ...
- hdu 2031 杨辉三角
题意:显示杨辉三角形. 解法: 组合数学公式:combi(n,m)=combi(n-1,m-1)+combi(n-1,m); 至于为什么有这个公式呢?那就是高中数学的内容啦 1: #include&l ...