2

I've come across hints about output caching a server control, but have yet to find a definitive answer to: Can the output of a server control be cached (in the same way that user controls are cached)?

The suggestions I've come across involve caching the data (not true output caching), or suggest setting the response.cache options. Ideally, I'd like to be able to drag a server control on to the page and set properties with the same names as the outputcache directive:

Example:

<cc1:MyCustomControl ID="ctl1" runat="server" CacheDuration="200" VaryByCustom="user" />

I was going to dig into the framework to see how output caching really works, but was hoping to find some information to get me started. From my limited understanding, the parser decides whether or not a page/usercontrol is cached. Since server controls are not parsed, there would be no way to stop the code from executing. I suppose I could initialize an "IsInCache" boolean and make sure that all methods check that var before executing the code. This would not entirely eliminate the server control from being executed, but it might improve performance enough by avoiding calls to the database and binding data to controls.

I'm still using .NET 3.5, and haven't had much time to explore the OutputCache provider model in .NET 4.0. Maybe there's a solution in the latest version of the framework?

Any thoughts?

1 Answers1

2

I was looking for this as well and I figured I'd add the solution to this page since it came up in my web search. There is an attribute called PartialCaching that is applied to the server-side/custom control to achieve the same effect as the OutputCache directive used in ascx and aspx templates. It takes the same parameters (VaryByParameter, etc) as well. For example:

[PartialCaching(1000, "foo", null, null)]
public class ClientScriptVariableBlock : Control {
  ...
}

The ASP plumbing treats this the same as the template attribute, so the end result is the same. This is valuable (in my case) because I get to create common server controls outside of the web project itself that can be cached. ASCX templates don't work very well in external assemblies :)

kprobst
  • 16,165
  • 5
  • 32
  • 53
  • Thanks! I will definitely checked it out. My current solution involves sub-classing the .NET controls and overriding every "On..." sub so that the events are not raised for any control that I've specified with cache params. I programmatically add the control to cache with a key based on the "VaryBy..." options, CacheDuration, etc. It works like a charm, but I don't like that I've had to stomp on the normal control life cycle to achieve it. – ByteCrunchr Nov 22 '10 at 10:15
  • At first glance it looks like there's no way to change the values of defined in the PartialCachingAttribute. If it works the same as the user control OutputCache directive, the PageParser reads the attributes so there's no way to inject any variable data into the process. – ByteCrunchr Nov 22 '10 at 10:25
  • No, I don't think there is a way to do that. – kprobst Nov 23 '10 at 17:50