代码之家  ›  专栏  ›  技术社区  ›  John Boker

Asp。Net OutputCache和过期

  •  4
  • John Boker  · 技术社区  · 16 年前

    我在一个包含用户控件的页面上使用Asp.net OutputCache,在某些情况下,当编辑用户控件时,我希望能够使页面缓存过期,并用新数据重新加载页面。

    有什么方法可以在用户控制中做到这一点吗?

    如果没有,还有什么其他缓存页面的方法可以让我以这种方式进行编辑。

    -----------编辑-----------

    经过更多的研究,我发现了一种似乎很有效的方法。

    Dim cachekey As String = String.Format("Calendar-{0}", calendarID)
    HttpContext.Current.Cache.Insert(cachekey, DateTime.Now, Nothing, System.DateTime.MaxValue, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing)
    Response.AddCacheItemDependency(cachekey)
    

    这将为页面缓存对象添加一个依赖关系,然后为了过期,我这样做:

    Dim cachekey as string = String.Format("Calendar-{0}", CalendarID)
    HttpContext.Current.Cache.Insert(cachekey, DateTime.Now, Nothing, System.DateTime.MaxValue, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing)
    

    现在,只要知道依赖cachekey,页面就可以过期。

    3 回复  |  直到 13 年前
        1
  •  1
  •   Andre Gallo    16 年前

    你可以试试这个:

    private void RemoveButton_Click(object sender, System.EventArgs e)
    {
        HttpResponse.RemoveOutputCacheItem("/caching/CacheForever.aspx");
    }
    

    发件人: http://aspalliance.com/668

    谢谢。

        2
  •  1
  •   some_guy    14 年前

    然而,你的解决方案对我不起作用。..经过一些测试,我让它工作得很好。此代码将位于需要缓存的UserControl Page_Load中。

    string key_refresh = "refresh_id_" + YourID;
    Cache[key_refresh] = DateTime.Now.ToString();
    
    CacheDependency dep = new CacheDependency(null, new string[] { key_refresh });
    this.CachePolicy.Dependency = dep;
    

    出于某种原因,使用 Response.AddCacheItemDependency 当我从更新数据时,没有任何效果 Cache[key_refresh] .

    在我的例子中,我按用户ID缓存我的控件,这样每个用户都会有一个不同版本的控件,其中包含不同的数据,我使用VaryByCustom单独缓存它。

        3
  •  0
  •   John Boker    15 年前

    经过更多的研究,我发现了一种似乎很有效的方法。

    Dim cachekey As String = String.Format("Calendar-{0}", calendarID)
    HttpContext.Current.Cache.Insert(cachekey, DateTime.Now, Nothing, System.DateTime.MaxValue, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing)
    Response.AddCacheItemDependency(cachekey)
    

    这将为页面缓存对象添加一个依赖关系,然后为了过期,我这样做:

    Dim cachekey as string = String.Format("Calendar-{0}", CalendarID)
    HttpContext.Current.Cache.Insert(cachekey, DateTime.Now, Nothing, System.DateTime.MaxValue, System.TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, Nothing)
    

    现在,只要知道依赖cachekey,页面就可以过期。