session vs Caching in asp.net
- Output Caching
- Fragmentation Caching
- Data Caching
- First open the Visual Studio and create a new ASP.NET project with the C# language.
- Now add one ASP.NET page I named “Session.aspx”.
- Now I am creating here a login page by using 2 Text Boxes and 1 Button control inside the table as you can see in the following:
The code of this login page is as in the following (inside the body tag):
- <form id=“form1” runat=“server”>
- <div>
- <h2 style=“margin-left: 40px”><strong> Login Form</strong></h2>
- <table>
- <tr>
- <td>
- UserName: </td>
- <td>
- <asp:TextBox ID=“txtUser” runat=“server”></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- Password:
- </td>
- <td>
- <asp:TextBox ID=“txtPwd” runat=“server”></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td colspan=“2”>
- <center><asp:Button ID=“btnSubmit” runat=“server” Text=“Submit” OnClick=“btnSubmit_Click” /></center>
- </td>
- </tr>
- </table>
- </div>
- </form>
- Now on the code behind of this page (“Session.aspx”) write the following code in the click event of the submit button.
The code above shows that we have 2 session keys UName and Pwd and I am providing the user name textbox’s (txtUser’s) value to the session which the key is UName and TextBox of the password’s (txtPwd’s) value to session of which the key is Pwd.
Look at the code above after creating both of the session keys I am redirecting to another page called “AfterSessionRedirect.aspx”.
- So now I am creating another page named “AfterSessionRedirect.aspx” and taking two lables where I will show the saved value on the session. I have created “AfterSessionRedirect.aspx” as follows:
The code of this aspx page is as folows:
- <div>
- User Name:
- <asp:Label ID=“lblUsr” runat=“server” Font-Bold=“True” ForeColor=“Red”></asp:Label>
- <br />
- Password:
- <asp:Label ID=“lblPwd” runat=“server” Font-Bold=“True” ForeColor=“Red”></asp:Label>
- </div>
- Now on the code behind of that page I am writing code that will display the user name and password in the label. So on the load time of the page I am writing the following code:
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Session[“UName”] != null && Session[“Pwd”] != null)
- {
- lblUsr.Text = Session[“UName”].ToString();
- lblPwd.Text = Session[“Pwd”].ToString();
- }
- else
- {
- lblUsr.Text = “Session of UName key is not set”;
- lblPwd.Text = “Session of Pwd key is not set”;
- }
- }
- Now run the Session.aspx page; you will see output such as the following:
When Session.aspx page loads:
After entering username and password as in the following:
After clicking “Submit” the session variable will be stored on the server and displayed on the next page as in the following:
- Create two more pages one named as Caching.aspx and another one is AfterCachingRedirect.aspx.
- On Caching.aspx:
For creating this I have used same code that I have written in Session.aspx as in the following:
- <form id=“form1” runat=“server”>
- <div>
- <div>
- <h2 style=“margin-left: 40px”><strong> Login Form</strong></h2>
- <table>
- <tr>
- <td>
- UserName: </td>
- <td>
- <asp:TextBox ID=“txtUser” runat=“server”></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td>
- Password:
- </td>
- <td>
- <asp:TextBox ID=“txtPwd” runat=“server”></asp:TextBox>
- </td>
- </tr>
- <tr>
- <td colspan=“2”>
- <center><asp:Button ID=“btnSubmit” runat=“server” Text=“Submit” OnClick=“btnSubmit_Click” /></center>
- </td>
- </tr>
- </table>
- </div>
- </div>
- </form>
- For the code behind of Caching.aspx write the following code in the click event of the submit button:
You can see I have created UName and Pwd two key of the caching and storing information on the server’s cache memory.
Code above is describes that I am saving username and password on the server’s cache memory, and after storing the username and password inside the server cache page will be redirect to the AfterCahchingRedirect.aspx - For the same as AfterSessionRedirect.aspx I have created AfterCachingRedirect.aspx and on this page use the following code:
And the following code is for creating this page(AfterCachingRedirect.aspx).
- <div>
- User Name:
- <asp:Label ID=“lblUsr” runat=“server” Font-Bold=“True” ForeColor=“Red”></asp:Label>
- <br />
- Password:
- <asp:Label ID=“lblPwd” runat=“server” Font-Bold=“True” ForeColor=“Red”></asp:Label>
- </div>
- On the code behind of the AfterCachingRedirect.aspx I have written the following code in the page load.
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Cache[“UName”] != null && Cache[“Pwd”] != null)
- {
- lblUsr.Text = Cache[“UName”].ToString();
- lblPwd.Text = Cache[“Pwd”].ToString();
- }
- else
- {
- lblUsr.Text = “Caching of UName key is not set”;
- lblPwd.Text = “Caching of Pwd key is not set”;
- }
- }
- After writing the code above run Caching.aspx as in the following:
On load:
After providing username and password:
On click of the “Submit” button:
So now I am again running Session.aspx and showing you how it stores the data at the user level.
Run Session.aspx and provide username and password:
After clicking on “Submit”:
1. In Session after storing the session, when you copy the URL and paste it into another browser then you can’t access the URL but when you do that for caching, in other words after storing into the cache variable, it can be accessed by a different user also.
So now we can say that a session has user-level storage on the server but caching has application-level storage on the server.
2. A session has a relative or sliding expiration, in other words by default a session has an expiration time of 20 minutes, but an application that is not being used will be relatively increased, in other words if the user is not using that application for more than 20 minutes then the session will be automatically expired and if the user is not using it then the time will be relatively increased, well we can also provide a custom time of session expiration. But caching has 2 types of expiration as in the following:
- Absolute Expiration
- Relative Expiration or Sliding Expiration
- Cache.Insert(“Uname”, txtUser.Text, null, DateTime.Now.AddSeconds(20), TimeSpan.Zero);
Here:
null: Dependencies; in caching there are the following 3 types of dependencies:
- Created cache that depends on another cache
- Created cache that depends on a file
- Created cache that depends upon a database. I have given it null because I don’t want my cache to expire by changing a file or a cache or a database.
- Cache.Insert(“Uname”, txtUser.Text, null, DateTime.MaxValue, TimeSpan.FromSeconds(20));
null: Dependencies (in caching there are the following 3 types of dependencies as in the following:
- Created cache that depends on another cache
- Created cache that depends on a file
- Created cache that depends upon a database. I have given it null because I don’t want my cache to expire by changing a file or a cache or a database.
DateTime.MaxValue: Sets the time to max so it will disable the property of the absolute expiration.
Leave a Reply