This tutorial shows how to use basic caching to improve the performance of a website. C# version.We can add the following line of code to the top of our ASPX
pages to set a duration that the page is valid for.
Optimization - Output Cache in ASP.NET & C#
This tutorial shows how to use basic caching to improve the performance of a website. C# version.
We can add the following line of code to the top of our ASPX
pages to set a duration that the page is valid for. With the below
example, the page will not be reloaded during the 15 second timeframe.
This means that if the user was to refresh the page within those 15
seconds, the cache version would be loaded.
| <%@ OutputCache Duration="15" VaryByParam="none" %> |
Example: If we had a label that displayed the time (as below), the time
would only be updated if the page was reloaded after the 15 second
timeframe.
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = System.DateTime.Now.ToString();
}
|
We migrated our web sites to Server Intellect
over one weekend and the setup was so smooth that we were up and
running right away. They assisted us with everything we needed to do
for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
The above example of caching is page-specific. If we wanted to cache
pages on a larger scale on our website, we could use Application-Level
Caching.
This is done by modifying the Web.config file:
And then modifying the ASPX directive:
| <%@ OutputCache CacheProfile="AppCache1" VaryByParam="none" %> |
This directive refers to the Cache Profile specified in the Web.config file.
However,
there are yet more ways we can use caching. We can cache pages using
parameters (VaryByParam). This will enable ASP.NET to store different
variations of the page depending on certain parameters such as query
strings, post values, request headers, etc.
We can create an example
of this by changing the color of the label that holds the current time.
We create a textbox and a button, and allow the user to type a color to
change the label. Every time the color changes, the time should change.
However, if the color remains the same, and the page is still within
its duration specified in the directive (60 seconds), then the time
will stay the same. This is because it is the cached version of the
page which is being loaded.
So we change the directive once again to add the parameters:
| <%@ OutputCache Location="Server" Duration="60" VaryByParam="textBoxColor" %> |
And our ASPX page will look something like this:
Type the name of a color:
runat="server" OnClick="Button1_Click" Text="Change Color" />
The time displayed in the label should only update if a new color is entered, or
the page cache duration has expired (60 secs)
|
The code-behind will be something like this:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = System.DateTime.Now.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.BackColor = System.Drawing.Color.FromName(Server.HtmlEncode(textBoxColor.Text));
if (Label1.BackColor == System.Drawing.Color.Black)
{
Label1.ForeColor = System.Drawing.Color.White;
}
else
{
Label1.ForeColor = System.Drawing.Color.Black;
}
}
} |
|