Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
ASP.NET: Data Caching
From Wiki
Summary: An example of how you can cache data within your application
ASP.NET provides an OutputCache method that allows you to cache a page on the server so that future requests don't have to recreate the page. However, there may be times when you want to cache something programmatically so that you can use that object across multiple page. Luckily, this is very easy to do and here's a simple example that shows how to cache a DataTable for 1 hour:
- Public Function GetData() As DataTable
- ' Create a new DataTable and set it to the cached version
- Dim dt As New DataTable
- dt = HttpContext.Current.Cache.Get("MyData")
- ' Check if the DataTable exists in the cache
- If dt Is Nothing Then
- ' Place your code to get your data here
- MyDataTable = ...
- ' Add the DataTable to the cache
- HttpContext.Current.Cache.Insert("MyData", MyDataTable, Nothing, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration)
- 'Return the DataTable
- Return MyDataTable
- Else
- ' Return the cached version
- Return dt
- End If
- End Function
This Hack is part of the ASP.NET Hacks collection



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.