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


