Search code examples
c#asp.netsessioncachingviewstate

Advantages of Cache vs Session


What is the difference between storing a datatable in Session vs Cache? What are the advantages and disadvantages?

So, if it is a simple search page which returns result in a datatable and binds it to a gridview. If user 'a' searches and user 'b' searches, is it better to store it in Session since each user would most likely have different results or can I still store each of their searches in Cache or does that not make sense since there is only one cache. I guess basically what I am trying to say is that would the Cache be overwritten.


Solution

  • One important difference is, that items in the cache can expire (will be removed from cache) after a specified amount of time. Items put into a session will stay there, until the session ends.

    ASP.NET can also remove items from cache when the amount of available memory gets small.

    Another difference: the session state can be kept external (state server, SQL server) and shared between several instances of your web app (for load balancing). This is not the case with the cache.

    Besides of these differences (as others have noted): session is per user/session while cache is per application.