敬的世界

常用链接

统计

最新评论

Page.Cache. HttpContext.Cache. HttpRuntime.Cache. Hsahtable有什么区别,怎样用

转自:http://zwjy.gjjblog.com/archives/3683/

   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的时候要使用同步关健字.

 1public 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    }

posted on 2009-01-13 20:20 picture talk 阅读(629) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: