【问题标题】:Asp.Net OutputCache and ExpirationAsp.Net OutputCache 和过期
【发布时间】:2009-01-26 22:05:57
【问题描述】:

我在包含用户控件的页面上使用 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)

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

【问题讨论】:

    标签: c# asp.net vb.net caching outputcache


    【解决方案1】:

    你可以试试这个:

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

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

    谢谢。

    【讨论】:

    • 是的,这似乎是唯一的例子,但它没有考虑 VaryByParams,所以如果我使该页面过期,我认为缓存中仍有数据用于同一页面不同的查询字符串。
    【解决方案2】:

    您的解决方案对我不起作用。但是......经过一些测试,我得到了这个工作正常。此代码将位于需要缓存的 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;
    

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

    在我的例子中,我通过用户 ID 缓存我的控件,因此每个用户将拥有具有不同数据的该控件的不同版本,我使用 VaryByCustom 单独缓存它。

    【讨论】:

      【解决方案3】:

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

      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)
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-22
        • 1970-01-01
        • 2011-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多