Friday, July 25, 2008

Understanding the differences between Cache, Session and ViewState

URL: http://www.progtalk.com/ViewArticle.aspx?ArticleID=62

My colleague sent this article to us recently. After I've read it, I found it quite useful and easy to understand, just wanna share it out.

In the article, it introduces these 3 types of storage methods: Cache, Session, ViewState.

To be honest, I only used session before. :p
It's time for me to learn how to use the storage methods probably, to make the web application better.

These are the notes I've taken:
----------------------------------------

Cache
- memory.
- allow you to store difficult and complex constructed data which will can be reused.
- is available to be accessed from global/application level where one reference to memory is updated. Each request will use the same cache for different users.

Session
- a period of time that is shared between the web application and the user.
- Each user that is using the web application has their own session.
- The Session variables will be cleared by the application which can clear it, as well as through the timeout property in the web config file.
- Session variables will use different session variables for each different user.

ViewState
- hidden data that is kept by ASP.NET pages.
- track the changes to a web site during post backs.
- All server controls contain a view state.
[EnableViewState property - enable/disable if the control properties will be held in hidden fields.]
- Having a large ViewState will cause a page to download slowly from the users side.
- When a user clicks on a button and a post back occurs, all the view state information has to be posted back to the server which causes a slower request.
- ViewState should be limited to what is needed.
- Data can also be written into the ViewState.
- The ViewState only persists for the life cycle of the page.
- If the page redirects to another page, or even to itself, the ViewState will be reset.
- ViewState should be used to hold small amonts of data, which will be only used on the current page.