轉(zhuǎn)自:
http://zwjy.gjjblog.com/archives/3683/ HttpContext.Cache,HttpRuntime.Cache,用法上沒有區(qū)別,就是使用范圍不同.
HttpContext.Cache是基于上下文,對(duì)同一個(gè)用戶起做用,如果換了一個(gè)訪問,那么這個(gè)CACHE就不起做用了.
HttpRuntime.Cache是全局的,對(duì)任務(wù)人都有做,只要有內(nèi)容,任務(wù)用戶都可以訪問.
????public?class?HttpRuntimeCache

????{
????
????????public?static?string?Instance()

????????{
????????????string?vc=(string)HttpRuntime.Cache["Test"];
????????????if(vc==null)

????????????{
????????????????HttpRuntime.Cache.Add("Test","test",?null,System.DateTime.Now.AddDays(10),TimeSpan.Zero,
????????????????CacheItemPriority.Default,null);
????????????}
????????????return?vc;
????????}

????}
Page.Cache實(shí)際上是訪問其它CACHE的接口.可以通過Page.Cache來訪問HttpContext.Cache,HttpRuntime.Cache里面的東西.
還有另外一種保存Cache方式是,使用Hashtable等來保存,值的一說的是這種Cache也是全局 ,而且是不可過期的 ,我想這是與 HttpRuntime.Cache的一個(gè)區(qū)別吧.至于安全性能方面我還沒有去驗(yàn)證.Hashtable保存方式比較特別,就是NEW的時(shí)候要使用同步關(guān)健字.
?1
public?class?hashcache
?2
????{
?3
???????private?static?Hashtable?parmCache?=?Hashtable.Synchronized(new?Hashtable());
?4
?5
????????public?static?void?CacheParameters(string?cacheKey,?string?cmdParms)?
?6
????????{
?7
????????????parmCache[cacheKey]?=?cmdParms;
?8
????????}
?9
10
????????public?static?string?GetCachedParameters(string?cacheKey)?
11
????????{
12
????????????return?(string)parmCache[cacheKey];
13
????????}
14
????}