惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
Spread Privacy
Spread Privacy
I
InfoQ
V
V2EX
S
Schneier on Security
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Attack and Defense Labs
Attack and Defense Labs
云风的 BLOG
云风的 BLOG
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
C
Cybersecurity and Infrastructure Security Agency CISA
NISL@THU
NISL@THU
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
Latest news
Latest news
S
Secure Thoughts
Project Zero
Project Zero
MongoDB | Blog
MongoDB | Blog
I
Intezer
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
N
Netflix TechBlog - Medium
V2EX - 技术
V2EX - 技术
量子位
T
Threatpost
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
T
Tor Project blog
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
博客园 - Franky
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题

博客园 - e旋风

异常码汇总 搬家公司管理系统,能完成车辆管理、订单管理等工作,并给出报表统计数据 Win7系统上华为无线上网卡与VMware冲突导致驱动无法正常安装的解决办法 VMware 中禁用虚拟内存加速虚拟机速度 Thread & ThreadPool 的一些背景知识 - e旋风 [转]WinForm数据绑定--BindingContext C#中字符数与字节数的区别 - e旋风 - 博客园 世联地产软件工程师笔试试题 配置节处理程序声明 - e旋风 - 博客园 判断是否存在在父结果集中有而在子结果集中没有的记录的最佳方法 动态执行Sql语句与临时表的问题(对象名无效) 定义局部变量时,字符串不能超过8000的方法 我搜集的关于工作流方面的技术文章 注意Transact-SQL中Case函数的两种用法导致不同的结果集 只有设置为InProc,Session失效时才会触发Session_End asp.net用户控件中使用相对路径问题 a href=#与 a href=javascript:void(0) 的区别 打开新窗口链接的几种办法 只能输入汉字,数字,英文大小写,符号只允许,。!的正则表达式 HTML及XML语言的转义字符
HttpRuntime.Cache 与HttpContext.Current.Cache的疑问
e旋风 · 2006-12-31 · via 博客园 - e旋风

已经有人说过这个话题,相关链接:

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 。