开发者

Should I cache instances of frequently accessed classes

New to .net and was wondering if there is a performance gain to keeping an instance of, for example a DAL object in scope?

Coming from the Coldfusion world I would instanciate a c开发者_JS百科omponent and store it in the application scope so that every time my code needed to use that component it would not have to be instanciated over and over again effecting performance.

Is there any benefit to doing this in ASP.Net apps?


Unless you are actually experiencing a performance problem, than you need not worry yourself with optimizations like this.

Solve the business problems first, and use good design. As long as you have a decent abstraction layer for your data access code, then you can always implement a caching solution later down the road if it becomes a problem.

Remember that any caching solution increases complexity dramatically.


NO. In the multi-tier world of .asp this would be considered a case of "premature optimization". Once a sites suite of stubs, scripts and programs has scaled up and been running for a few months then you can look at logs and traces to see what might be cached, spawned or rewritten to improve performance. And as the infamous Jeff Atwood says "Most code optimizations for web servers will benifit from money being spent on new and improved hardware rather than tweaking code for hours and hours"


Yes indeed you can and probably should. Oftentimes the storage for this is in the Session; you store data that you want for the user.

If it's a global thing, you may load it in the Application_Start event and place it somewhere, possibly the HttpCache.

And just a note, some people use "Premature Optimisation" to avoid optimising at all; this is nonsense. It is reasonable to cache in this case.


It is very important to do the cost benefit analysis before caching any object, one must consider all the factors like

  1. Performance advantage
  2. Frequency of use
  3. Hardware
  4. Scalability
  5. Maintainability
  6. Time available for delivery (one of the most important factor)

Finally, it is always useful to cache object which are very costly to create or you are using very frequently i.e. Tables's Data (From DB) or xml data


Does the class you are considering this for have state? If not, (and DAL classes often do not have state, or do not need state), then you should make it's methods static, and then you don't need to instantiate it at all. If the only state it holds is a connection string, you can also make that property field a static property field, and avoid the requirement of instantiating it that way.

Otherwise, take a look at the design pattern called Flyweight

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜