I am implementing how data caching work in asp.net. For that i have kept three button
1)cache date button,
2)currentdate button and
3)Cache Remove button.
If I click on cache date button date will be cached for 2 minitues. if click on current date button current datetime will be displayed, and finally If I click on Remove cache button cache will be removed and fresh cache date and fresh current date will be displayed.
For this I have done the coding but datetime is not caching always it refreshing I mean if datetime is cached for 2 minits and if i click the cached button datetime should not change but in my case its always changing.... Pls somebody modify my code...
protected void BtnCacheTime_Click(object sender, EventArgs e)
{
Cache.Insert("date",DateTime.Now,null,
DateTime.Now.AddMinutes(2), TimeSpan.Zero);
lblCacheDateTime.Text = Cache["date"].ToString();
}
protected void BtnCurrentDate_Click(object sender, EventArgs e)
{
lblCurrentDate.Text = DateTime.Now.ToString();
}
protected void BtnRemoveCache_Click(object sender, EventArgs e)
{
Cache.Remove("date");
lblFreshCa开发者_StackOverflowcheDate.Text = DateTime.Now.ToString();
lblFreshCrntDate.Text = DateTime.Now.ToString();
}
Well, for once, you insert DateTime.Now
to the cache, and then immediately read its value. What else would you expect?
Next, you shouldn't use TimeSpan.Zero
- when setting absolute expiration, you should use Cache.NoSlidingExpiration
.
精彩评论