开发者

Caching a server side user control in asp.net

My asp.net app is heavily using开发者_如何转开发 user controls and is high load; my concern is that loading user controls for every request (Page.LoadControl(controlPath)) is sub-optimal at best. I was thinking of caching an instance of loaded user control in HttpRuntime, but I don't know how's it going to behave when multiple connections (threads) will try to fetch it from the cache and modify properties - I guess trouble is waiting there. Any good way to optimize user control loading?

Again, I am talking about caching user control, not it's html output (which it makes no sense caching as it's [html output] going to be different every time you use it)

Thanks!


Do not cache a user control, or anything that inherits from WebControl, ever.

The reason is that it contains a reference to the Page instance on which it is instantiated. By caching the control, you are effectively caching the Page and the entire control subtree with it. You're right when you say that there is trouble waiting there!

Far, far better to cache the data that your usercontrol needs, create a new usercontrol and initialize it with the data.

Some other suggestions for optimization:

  • If you're only using one or two controls per page, rather than loading them dynamically, just declare them as part of the page and set their visibility to false initially, and then to true when you need them.

  • If usercontrols are really just too slow for you, you could try rewriting it as a WebControl and set up the layout in code. I'm not sure if the extra work would be worth any optimizations that might be gained there.

I wouldn't pre-optimize anything until you know it's an issue. Are you actually running into performance problems with LoadControl()?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜