How do I cache a composite server control?
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("adxdependenc开发者_如何学JAVAy: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.
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
精彩评论