Tutorial :Hold global data for an ASP.net webpage


I am currently working on a large-scale website, that is very dynamic, and so needs to store a large volume of information in memory on a near-permanent basis (things like configuration settings for the checkout, or the tree used to implement the menu structure).

This information is not session-specific, it is consistent for every thread using the website.

What is the best way to hold this data globally within ASP, so it can be accessed when needed, instead of re-loaded on each use?


Any AppSettings in web.config are automatically cached (i.e., they aren't read from the XML every time you need to use them).

You could also manually manipulate the cache yourself.

Edit: Better links...


It's not precisely clear whether your information is session specific or not...if it is, then use the ASP Session object. Given your description of the scale, you probably want to look at storing the state in Sql Server:


That's the 101 approach. If you're looking for something a little beefier, then check out memcached (that's pronounced Mem-Cache-Dee):


That's the system that apps like Facebook and Twitter use.

Good luck!


Using ASP.NET caching feature is a good option I think. In addition to John's answer, you can use Microsoft's Patterns & Practices team's Caching Application Block.


This is a good video exploring the different ways to can retain application state. http://www.asp.net/learn/3.5-videos/video-11.aspx It brushes on the Application object which is global for the whole application, for all users and shows you how to create a hit counter (obviously instead of storing an integer you could store objects). If you need to make changes, you do need to use a lock for concurrency, and I'm not sure how it handles LARGE amounts of data because I've never had to keep that much there.


I usually keep things like that in the Application object.


If the pages are dependent upon one another and they post to one another, you could use the page's request object. Probably not the answer you're looking for, but definitely one of the smallest in memory to use.


I have run into the same situation in the past and found an interface to be the most scalable solution. Application cache may be the answer today, but will it scale to meet your needs?

If you need to scale up, you may find cookies, or some type of temp database storage to be the trick. Simply add a new method to your interface, and set the interface to choose the "mode" from web.config.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »