Last post Sep 18, 2014 01:51 PM by AidyF
Sep 18, 2014 11:42 AM|amithashenoy|LINK
Can anyone explain in which situation/condition one can use Cache/session/application objects .Please provide some example as well..
Thanks in advance
Sep 18, 2014 11:48 AM|PatriceSc|LINK
In short Application is for values shared accross the whole application, Session is scoped to each user session and a Cache allows to have a shorter lifetime (you can define the duration for which you'll want to keep those values).
Sep 18, 2014 01:09 PM|Siva Krishna Macha|LINK
Session - holds the which needs to be shared across one client. Here, client can be a browser (an user who generally logs into the website). You might want to store what is his first name, last name, his user id, last visited datetime, etc. Some also uses
this to send the data from one page to the next page though there are other techniques to achieve this particular one like cross page postback, etc.
Application - The data which needs to be shared across within one application. For example, one wants to display the number of current logged in users. This data is common for all users. These variables generally be modified inside global.asax file - where
you have Application_Start, Application_End, Session_Start, Session_End and other events inside this file.
Cache - a temporary storage for a given key per application - can be with in the process or can use external apps to do it for you. i.e., Distributed cache can be used. When you want to avoid the costly operations (making calls to external services or DB
- which gives the data that doesn't generally change over a period of time) - for example, books "catalog" in an online purchasing website. Remember it is catalog (which may change hardly once in a day) - not the stock (which gets changed everytime once you
Sep 18, 2014 01:51 PM|AidyF|LINK
You wouldn't use the Application these days, its functionality has been superseded by the Cache object.
As already said, Session allows you to store information per-user for the user's current browsing session
Cache - this isn't tied to a user so everything in here is seen by all, but there are tricks you can use to allow it to work per-user, eg you could attach the session id to the names of your keys so that each user can have their own "Blah" data, but storing
it with the key "Blah_<sessionid>". You can use the cache for what you would use the Application object for...storing data you want everyone to have access to. The cache is also useful as it has different expiry options. You can set the data to only be
cached for 5 minutes, and you can specify the timeout is fixed or sliding....so 5 minutes sliding means every time the data is accessed it will live for another 5 minutes. This makes it good for performance issues. Let's say your site sells products and
someone accesses Product 100...after getting it from the database you can store the Product object in the cache with a sliding expiration of a few minutes. So as long as Product 100 is popular, it stays in the cache. But if no-one wants to see product 100
for 5 minutes then it is deleted from the cache, so your cache only contains active items rather than being bloated with non-active ones.
Other tricks you can do is attach dependencies to items in the cache. So you might cache a few things that are dependent to some piece of master data, and when master data in the cache exchanges, your dependency code runs so you can also expire or update
the related items.