- 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:
What you saw is session and caching working the same but the difference is as I explained, session is user level but caching is application level.
So now I am again running Session.aspx and showing you how it stores the data at the user level.
Now Difference
Run Session.aspx and provide username and password:
After clicking on "Submit":
Now after copying the URL you will get the output:
Now
Run Session.aspx and provide username and password:
After clicking on "Submit":
Now after copying the URL you will get the output:
You can see both outputs.
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
Absolute expiration means in caching the cache memory will be deleted after a fixed amount of time, the same as for cookies
Sliding expiration means in caching the cache memory will be deleted after a fixed amount of time if that memory is not in use, if it is used in that specific time then it can be relatively increased. the same as for a session.
1. How to provide Absolute Expiration in caching
- Cache.Insert("Uname", txtUser.Text, null, DateTime.Now.AddSeconds(20), TimeSpan.Zero);
Here:
Uname: Key
txtUser.Text: Object Type value (that we want to save into the cache)
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.
DateTime.Now.AddSeconds(20): Absolute time is given for expiration.
TimeSpan.Zero: This property disable the sliding expiration, in other words if it's set to zero then the relative time will never expand for the cache.
2. How to provide Sliding Expiration in caching
- Cache.Insert("Uname", txtUser.Text, null, DateTime.MaxValue, TimeSpan.FromSeconds(20));
Uname: Key
txtUser.Text: Object Type value (that we want to save into the cache)
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.
TimeSpan.FromSeconds(20)): It will set a sliding expiration time to 20 second and it will enable the property for a sliding expiration.