we often use session, cache and profile in asp.net webform project. we often store data in session, cache and profile in asp.net webform project but i like to know when we should store data in session or when we should store in cache and profile. what is the scope of cache or profile. this two also session specific life time or application specific.
suppose if i store any data in cache or profile from session 1 then can i access that data from session2 or not. just guide me with scenario & example when we should store data in session, cache and profile. thanks
You use cache
typically when you want to improve site performance: reduce database calls, accessing files on filesystem, calling external services, etc.
Scenario 1
: Assume there is an application which has a page that lists all products stored in database. Users have need in product list filtering feature (like on ebay or amazon for example). Also it is known that list of products and product features are changing rarely comparing to frequency of product list page usage.
Solution 1
: Here you can use cache
to reduce database calls. You put product list and product features to cache
to perform filtering in memory and avoid redundant database calls. Cache
should be invalidated whenever product list or feature changes. This solution should be OK until memory consumption handling is cheaper than gain in performance.
As a good resource I suggest reading the ASP.NET Caching: Techniques and Best Practices MSDN article.
In profile
you typically save data which is specific to concrete user and should be available at every time user logs into system.
Scenario 2
: Consider you have the same application as in Scenario 1
but now you need to provide ability for users to save their filter preferences (kind of list of favourite filters).
Solution 2
: You can create a kind of filter-state class and store it in user profile
. As a result users will not be bothered in specifying filter parameters again and again and these preferences would be available every time user logs in.
Session
is used to store user-specific information that could be accessible from all web pages and will not be needed on next time user logs in.
Scenario 3
: Assume there is some e-commerce application and marketing people want to see what is happening on site in close real time manner, i.e. see what pages were visited by certain users while they were logged in. Consider this scenario as a selected users activity snapshot.
Solution 3:
Here you can use session
to log what URLs were visited by users and then generate report for marketing people.
As for state management you can also reffer to another MSDN article ASP.NET State Management Overview.
Another good answer which highlights the difference between objects that are managing asp.net application state.
Hope this helped you a bit.
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