|
Cache files using ASP.NET Cache Dependency
|
|
Rating: 7 user(s) have rated this article
Posted by: Malcolm Sheridan,
on 6/12/2009,
in category "ASP.NET 2.0 & 3.5"
Views: this article has been read 13963 times
Abstract: The following article demonstrates how to use ASP.NET cache dependency with files.
Cache files using ASP.NET Cache Dependency
When it comes to getting the most performance out of your ASP.NET application, caching is one avenue to look into. Earlier this year I demonstrated how to use SQL Cache Dependency using SQL Server 2005 and 2008, but this time I’ll demonstrate how to cache files using ASP.NET Cache Dependency.
Caching files or directories for changes is made possible thanks to the System.Web.Caching.CacheDependency class. This class can monitor a single file and folder or an array of files and folders for changes. In this example I am using a single file in the webs ites root directory to monitor.
Open Visual Studio 2008 and create a new ASP.NET Web Application. Add a new text file to the root directory and call it SampleFile.txt. Open the text file and add the following text:
Donec fermentum condimentum nisi. Proin iaculis mauris id lorem viverra molestie. Duis vel orci. Et pulvinar pede ligula a sapien. Donec fermentum condimentum nisi. Proin iaculis mauris id lorem viverra molestie. Duis vel orci. Nam consequat. Morbi nec lacus
This is just some placeholder text. Next add a TextBox to the Default.aspx page and set the TextMode to MultiLine:
<asp:TextBox ID="TextBox1" runat="server" Width="312px" Height="188px"
TextMode="MultiLine"></asp:TextBox>
In the page load event lets add the code that monitors the file for changes:
C#
protected void Page_Load(object sender, EventArgs e)
{
string fileContent = Cache["SampleFile"] as string;
if (string.IsNullOrEmpty(fileContent))
{
using (StreamReader sr = File.OpenText(Server.MapPath("~/SampleFile.txt")))
{
fileContent = sr.ReadToEnd();
Cache.Insert("SampleFile", fileContent, new System.Web.Caching.CacheDependency(Server.MapPath("~/SampleFile.txt")));
}
}
TextBox1.Text = fileContent;
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim fileContent As String = TryCast(Cache("SampleFile"), String)
If String.IsNullOrEmpty(fileContent) Then
Using sr As StreamReader = File.OpenText(Server.MapPath("~/SampleFile.txt"))
fileContent = sr.ReadToEnd()
Cache.Insert("SampleFile", fileContent, New System.Web.Caching.CacheDependency(Server.MapPath("~/SampleFile.txt")))
End Using
End If
TextBox1.Text = fileContent
End Sub
As you can see in the example, the application checks the cache for an item in the cache named SampleFile and assigns the value to the fileContent variable. When you’re checking for values in the cache, it is important to create a reference to the cached object. This is important because the data in the cache can be removed at any time by the web server. Assigning it to a variable eliminates this problem.
If the cache is null, the file is opened and added to the cache by the Cache.Insert method. The code shows an item named SampleFile added to the cache and having a file dependency set on the file named SampleFile.xml file. If you reload the page, the contents are read from the cache. If you update the contents of the SampleFile.txt file and reload the page, you’ll see that the object has been removed from the cache.
This is yet another way of using the cache to make your ASP.NET application perform that little better. The entire source code of this article can be downloaded from here