分享

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

 estorm2008 2015-03-20

           今天终于看了一下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的时候要使用同步关健字.



 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    }


    
       

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多