如何缓存复合服务器控件?

时间:2011-02-10 22:25:51

标签: .net caching servercontrols composite-controls cache-dependency

如何使用系统缓存对象HttpContext.Current.Cache在缓存中存储自定义服务器控件?

目前我的代码如下所示:

            string adxkey = string.Format("adxdependency:document:{0}", this.SearchRootDocumentKey);
        System.Web.Caching.CacheDependency cd = new System.Web.Caching.CacheDependency(null, new string[] { adxkey });
        HttpContext.Current.Cache.Insert(adxkey, this, cd);   

这位于OnInit方法中。我的问题是:

  • 有没有更好的方法来放置缓存代码?
  • 您可以通过如上所述传递对它的引用来缓存服务器控件吗?或者我是否需要缓存渲染的html?

如何以编程方式缓存服务器控件的示例将不胜感激!感谢。

1 个答案:

答案 0 :(得分:0)

对此的简短回答是,您需要将缓存属性添加到页面或用户控件,而不是服务器控件。我写了一篇关于如何做到这一点的博客文章,你可以在这里找到它:

http://www.dirigodev.com/blog/post/AspNET-Caching-101-Part-2-Cache-Key-Dependencies.aspx