Friday, 27 June 2014

Difference between Session and Cache

Both session and cache can be used to store the data at the server level (also "Caching" can also be done at client side for static files). Main defference is that Session is created for each user/browser request. It is user specific. and the data stored in session expires when the complete session is expired.

Cache data is not user specific (but if you want to make use of user specific cache data then you can create keys which is very unique to the user, lets say using a very unique "userid" value etc), it can be shared among all the users and we can specify the expiry time for the cache object.
Types of session:
InProc: Data is stored in server memory.
StateServerwhich stores session state in a separate process (ASP.NET state service). this data can be shared amoung multuple web servers in web farm.
SqlServer: session is saved in the sqlserver database. even if  webapplication is restarted, session state is preserved in the database.
Custom: We can specify custom storage of session.
Off: we can disable storing session.
Types of Cache:
Output Cache: Server side caching where the page can be cached at the server memory, and the data of the cache can be retrieved when the request is made from client and the data is served from the cache.
Fragment Cache: Server side cache for the part of the page (using usercontrol).  
- Data Cache: We can store any data in Cache object, and retrieve the data from the memory.
304 Response >> for unchanged content and tell browser to take the local copy of the file. this check is done at the server level and if the file is not changed then return 304 response.
Client side caching: For the images, js, css files which dont change very often. we can set approprite header for these  

One more interesting thing to read about cache is SqlCacheDependency. It is used to check if the data in the database is modified/altered which will automatically invalidate the cached object. we can use this for output cache and application cache.

No comments:

Post a Comment