Page.Cache,HttpContext.Cache,HttpRuntime.Cache,Hashtable有什么区别,怎么用.
今天终于看了一下CACHE ,一点学习心得与大家一起分享.也请大家指教.
Page.Cache,HttpContext.Cache,HttpRuntime.Cache,Hashtable有什么区别,怎么用.
HttpContext.Cache,HttpRuntime.Cache,用法上没有区别,就是使用范围不同.
HttpContext.Cache是基于上下文,对同一个用户起做用,如果换了一个访问,那么这个CACHE就不起做用了.
HttpRuntime.Cache是全局的,对任务人都有做,只要有内容,任务用户都可以访问.
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实际上是访问其它CACHE的接口.可以通过Page.Cache来访问HttpContext.Cache,HttpRuntime.Cache里面的东西.
还有另外一种保存Cache方式是,使用Hashtable等来保存,值的一说的是这种Cache也是全局 ,而且是不可过期的 ,我想这是与 HttpRuntime.Cache的一个区别吧.至于安全性能方面我还没有去验证.Hashtable保存方式比较特别,就是NEW的时候要使用同步关健字.
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
}
public class hashcache2
{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
}

浙公网安备 33010602011771号