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.
Session data is stored at the user level but caching data is stored at the application level and shared by all the users. Sessions may not improve performance whereas Cache will improve site performance. Items in cache can expire after given time to cache while items in session will stay till session expires.
The main difference between Cache and Cookie is that, Cache is used to store online page resources during a browser for the long run purpose or to decrease the loading time. On the other hand, cookies are employed to store user choices such as browsing session to trace the user preferences.
The main advantage, and also the goal, of caching is speeding up loading and minimizing system resources needed to load a page. The main disadvantage is how it's implemented by the developers, and then maintaining proper caching system for the website, making it properly manageable by the Admin.
A session cache allows a server to store session information from multiple clients. WebSEAL uses two types of session caches to accommodate both HTTPS and HTTP session state information between clients and WebSEAL: WebSEAL session cache.
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.
AFAIK, The key difference is session is per user, while cache will be for application scoped items.
As noted in the other answers you can store per user info in the cache, providing you provide a key (either by session or cookie). Then you'd have more control to expire items in the cache and also set dependencies on them. So if the DataTable in question is going to change on a regular basis, then caching is probably an appropriate option. Otherwise, if it is static session might be more appropriate. Steven Smith has an excellent video on caching at dnrtv that is worth checking out.
It really depends on what you're trying to achieve, how much time you've got. There are some other alternatives to consider with respect to how you store state in an application. Depending on how large the table is, you could consider storing the state in a cookie (encrypted if it is sensitive information). Alternatively, if it's application scoped data you cold use a static field on a page or class. There is the Application object as well.
Update: I think the key question you have to ask yourself, is who should see this data.
Are they going to access the data frequently?
(No, don't bother).
Is it going to change?
(No, use a static field or Application).
Is it acceptable for user a and user b to see the same results?
(No, use the cache with a key comprising of the username and the search term.).
(Yes, use the cache using a key of the search term).
Honestly though, if you're not far along in your development, I would consider parking the caching/state issue to a later date - you might not even need it.
The first three rules of performance tuning are: 1. Measure, 2. Measure some more. 3. Measure again...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With