如何缓存复合服务器控件?
如何使用系统缓存对象 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?
如何以编程方式缓存服务器控件的示例将不胜感激!谢谢。
How would I use the system caching object, HttpContext.Current.Cache to store a custom server control in the cache?
Currently I have code that looks like this:
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);
This is located int he OnInit method. My questions are:
- Is there a better method to place the caching code in?
- Can you cache a server control by passing a reference to it as in above? Or do I need to cache the rendered html?
An example of how to programmatically cache a server control would be greatly appreciated! Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简而言之,您需要将缓存属性添加到页面或用户控件,而不是服务器控件。我写了一篇关于如何执行此操作的博客文章,您可以在这里找到它:
http://www.dirigodev.com/blog/post/AspNET-Caching-101-Part-2-Cache-Key-Dependency.aspx
The short answer for this is that you need to add your caching attriutes to a page or user control, not a server control. I have written a blog post about how to do this, you can find it here:
http://www.dirigodev.com/blog/post/AspNET-Caching-101-Part-2-Cache-Key-Dependencies.aspx