ASP.NET Podcast Show #129 - Caching with .NET 3.5 SP1
Original Url: http://aspnetpodcast.com/CS11/blogs/asp.net_podcast/archive/2008/12/04/asp-net-podcast-show-129-caching-with-net-3-5-sp1.aspx
Show Notes:
- New Caching Support in .NET 3.5 SP1.
Source Code:
protected void
Page_Load(object sender, EventArgs e)
{
string Output = String.Empty;
if (!Page.IsPostBack)
{
lblOutput.Text =
GetCacheItem();
}
}
private string
GetCacheItem()
{
string OutValue = String.Empty;
if (Cache["Test"]
!= null)
{
OutValue = Convert.ToString(Cache["Test"]);
}
else
{
OutValue = DateTime.Now.ToString();
Cache.Insert("Test", OutValue, null,
DateTime.Now.AddSeconds(10),
TimeSpan.Zero,
OnUpdateCallback);
}
return (OutValue);
}
private void
OnUpdateCallback(String key, CacheItemUpdateReason r,
out Object
ObjectToCreate, out CacheDependency CacheDependencies,
out DateTime
DateTimeExpire, out TimeSpan TimeSpanExpire)
{
ObjectToCreate =
DateTime.Now;
CacheDependencies =
null;
DateTimeExpire =
DateTime.Now.AddSeconds(10);
TimeSpanExpire =
TimeSpan.Zero;
}