private void Page_Load(object sender, System.EventArgs e)
{
System.Web.Caching.Cache cache = new System.Web.Caching.Cache();
cache.Insert( "t","test");
if( cache[ "t" ] != null )
{
Response.Write( cache[ "t" ].ToString() );
}
}编译通过,但执行时提示
cache.Insert( "t","test");这句代码有错误。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
还想请教个问题,cache可以永不过期么?

解决方案 »

  1.   

    在页面上使用Cache不需要实例化。
    直接可以使用this.Cache.Insert("t","test");
    和this.Cache读取。Cache可以设置过期时间为DateTime.MaxValue就会永远不过期了!
      

  2.   

    Cache和Session、Cookies一样,不用实例化。
      

  3.   

    to cuike519()我是在一个单独的类文件中使用Cache,必须实例化的。不然出错,提示要求对象引用。
      

  4.   

    你可以在cache.Insert( "t","test");这个语句前面加个提示语句看下cache里面有内容不,由于你新建了实例,里面的内容肯定是空的了
    cache可一设置不过气
      

  5.   

    在一个单独的类文件中使用System.Web.HttpRuntime.Cache.Insert("t","test");