In the previous discussion, we discussed about few concepts like Reflection, difference between strongly types & loosely type, How to achieve multiple inheritances, Static classes.

Now in this discussion we will learn about more some concepts like sessions, http, etc.


We have seen http word in our browser when we are writing some website. That http is known as Hypertext Transfer Protocol. We can say that it is the main internet protocol behind World Wide Web (www).

But this Http is a stateless protocol. So firstly we understand that what do we mean by stateless?

In general meaning it means that not recognizing or forgetting the situation. But we try to understand its meaning in the computing world where statelessness is a protocol. Here we can say that stateless protocol means that it is a communication protocol which treats each request as an independent transaction which is not related to the previous request.

And therefore we can say that Http is a stateless protocol which means that the connection between the browser and the server is lost once the transaction ends.

It means that for every request a connection is made between the browser and the server, and the connection between them is lost as the transaction completes.


Now here this Session concept is in relation with Http being a stateless protocol.

As we have discussed above that how the connection is lost as soon as the transaction ends and the connection is made when the transaction is started. Therefore we can define Session as keeping the data or the request from the moment the browser is started till the browser is closed. So the timing in between the starting and closing of the browser is termed as Session.

Now we can assume that the Http behaves like a clerk in the bank. If we are going for the first time to meet the clerk for bank work, what does he do is that he asks our details and then provides us a form or something and takes our data and keep the data in the bank’s computer. Now the clerk will not remember our details or the conversation we had. So the next time we go to him we remind him of the first meeting and the form which he gave us. So he will look at the form and then remember our details after which he will resume the work related to us which was kept incomplete. He will not remember us because he has lot of work to do and cannot remember everything; therefore he gave us that form so that next time by looking at the form he will remember our details.

So in the same way we can say that Http is a communication protocol and cannot remember every detail. Therefore whenever we go to any website it will store a cookie inside our browser. Now this cookie will be having a unique id, so the next time we visit the website we have that cookie with us so accordingly the Http will remember our details and then continues the work.


We understand how session works, and along with it we saw how session is managed using cookies.

So these cookies are just unique ids which are provided by the website whenever we visit that site. These cookies are stored on client side in our browser, whereas the sessions are stored on the server side.

So whenever we visit the site from our browser the server sees the cookie and then identifies our details and does the transaction with us. As Http is a stateless protocol and does not remember the details of each of its client, it instead gives the cookie or we can say puts the cookies inside their browser. In this way it can do the transactions with the client easily and at the same time does not need to remember them after the transaction is completed. Here we see for e.g. how does a cookie looks like:


Here we see for website, 4 cookies are set inside the Google Chrome browser. The red underlined part is the unique id of the cookie. It also shows when the cookie is created & when it is going to expire.

So the generation of cookies and session we can say that all this is session management.

As we have seen about the session and cookies, we will now see what does Cache do and what are its various functions.


The general definition of cache is that it stores the values or data temporarily to reuse it whenever we want and also due to this next time to access the same value or data it will take less time and improve the performance. Therefore we can say that Caching means to store something in memory, which can be used frequently to provide better performance.

Now as we have seen what Caching means, we will now look at its different types. There are two types of Caching available, they are:

  • Page Output Caching
  • Application Caching

Page Output Caching:

As the name itself says, this type of caching’s main function is to cache the whole page. It means that the web server caches a certain webpage when a user requests for it. After that if there are further requests for the same webpage by the user then it will check the validity of the cached page and page will be returned to the user from cache

For e.g. We are caching the page for 30 seconds after the first request comes from the user. In those 30 seconds if the user request for the same webpage, that webpage will be provided from the cache. Therefore for the duration of 30 seconds the output of the request will be coming from cache. After 30 seconds are over when user requests for the webpage, it will not be coming from cache but instead a new cache will be created for duration of 30 seconds.

We understand this concept with a simple sample code,

public class TestController : Controller


    [OutputCache(Duration=30, VaryByParam="none")]

publicActionResult Index()


return View();




ViewBag.Title = "This is Output Cache Demo";


Time is @DateTime.Now.ToString()

Now first of all to use the page output caching we need to decorate the controller with [OutputCache] attribute as it is shown in the sample code. Here we are passing two values in the attribute, one is Duration and the other is property “VaryByParam”.

Here in the view we are giving some title and the current date &time. We have mentioned the duration by 30 seconds and VaryByParam is provided with no value means the caching is not depended on any parameters.

So what happens is that when we run this code we get an output showing the title “This is output cache demo” and the current date and time. Now for the duration of 30 seconds this output will not change when further requests are made because this output has been cached and we are getting the output from the cache and not from the data resource. As soon as 30 seconds are finished and then a request is made, it will give us new output and cache that output so that for further requests made within 30 seconds the output will be given from cache.

Also in the above code we have not provided any value for VaryByParam. If we provide the value for the property of ValueByParam, then for every new value the new output will be cached. We see the code for it,

public class TestController : Controller


    [OutputCache(Duration = 30, VaryByParam = "none")]

publicActionResult Index()


return View();


    [OutputCache(Duration = 30, VaryByParam = "id")]

publicActionResult Index1()


return View();



Here we have provided the value of VaryByParam equal to “id”. So now for every id we have new output, i.e. for different id’s different values are cached for the page request having different query string. For e.g. id=1 will cache different value, id=2 will cache different value.

Now if we want to apply this output caching to the whole controller then we have to decorate the [OutputCaching] attribute at the top of the controller.

[OutputCache(Duration = 30, VaryByParam = “none”)]

public class TestController : Controller

If we want to apply this attribute to multiple controllers we have to put the caching values in our web.config file in our project and create a caching profile




<add name="MyCacheProfile"



location="Any" />




To use it in each controller we have to just write,

[OutputCache(CacheProfile = “MyCacheProfile”)]

public class TestController : Controller

Partial Page Caching:

It is also a type of caching same as output page caching only the difference is that it will cache only partial page and not the full page.We first use render partial command to create partial view.For e.g. in a webpage where we want to keep the header & footer same and only cache the page between header & footer we use Partial Page Caching.

Application Caching:

We can define Application Caching as a process or mechanism used for storing the data objects on cache. It not related to page caching as here only data is stored which needs to be cached.

We understand this concept with simple sample code,

publicActionResult Index1()


if (System.Web.HttpContext.Current.Cache["time"] == null)


System.Web.HttpContext.Current.Cache["time"] = DateTime.Now;


ViewBag.Time = ((DateTime)System.Web.HttpContext.Current.Cache["time"]).ToString();

return View();



ViewBag.Title = "Application Caching Demo";


Time is @ViewBag.Time

Here we have the current date & time in our view. And when we execute this code we see the current date and time. But after that if we refresh the page several times the value of the date & time will not change because it is coming from application cache. That is when we run the code the value of the object date & time are cached and refreshing the page again and again will provide us the same value from the cache. Here we are not changing the cache values by using any parameters.

This concept is known as Application Caching.

As we have seen the two types of caching there is another concept of caching which we will try to understand.

Object Caching:

In Object Caching what happens is that when we are sending some request to the database server or any server it gives us the desired output. Now that desired output is cached in our web server. So suppose if the data changes or gets updated in the resource server, the data also get updated in our web server i.e. the cached data gets updated according to the changes in data made in our resource server.

This is termed as object caching. It can be applied by adding dependencies on the object, so that as the dependencies change the object is removed and a new object is added.

For e.g. we take a small code to understand dependency in cache,

string[] dependencies = { “CacheItem1” };

Cache.Insert(“CacheItem2”, “Cached Item 2”,

newSystem.Web.Caching.CacheDependency(null, dependencies));

Here we are inserting a new item or object Cache2 which is dependent on another item or object Cache1. So as Cache1 changes or is updated, Cache2 also changes or gets updated as Cache2 is dependent on Cache1.