Setting cache expiration with HttpContext.Current.Cache

Last post 06-17-2009 12:00 AM by rami_nassar. 3 replies.

Sort Posts:

  • Setting cache expiration with HttpContext.Current.Cache

    06-16-2009, 5:13 PM
    • Member
      12 point Member
    • mindgmz
    • Member since 03-09-2005, 11:46 AM
    • Posts 37

     How do I set the timelimit for a cache before it expires.

    Currently my cache is handled by using HttpContext.Current.Cache[<keyname>] = object;

    Using this method I cant see to add a time limit of expiration. How do I do this?

    Previously I had used HttpContext.Current.Cache.Insert(<keyname>, object, null, DateTime.Now.AddMinutes(30), TimeSpan.Zero);

    but that never worked, my object never got cached. I am using asp.net 2.0

     

    Thanks

  • Re: Setting cache expiration with HttpContext.Current.Cache

    06-16-2009, 11:43 PM
    Answer

     try using  Cache.Add(<keyname>, object, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 30, 0), System.Web.Caching.CacheItemPriority.Default, null);

     NoAbsoluteExpiration and TimeSpan attributes tell how and when the Cache should expire.

     You can also use SlidingExpiration instead of Absolute.


     

    Please Mark as Answer, if you find this helpful.
  • Re: Setting cache expiration with HttpContext.Current.Cache

    06-16-2009, 11:54 PM
    Answer
    • Star
      10,646 point Star
    • sumitd
    • Member since 07-16-2008, 4:32 PM
    • Bangalore
    • Posts 1,874
    Try this: 
    Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
    Refer below article:
    http://msdn.microsoft.com/en-us/kb/kb00323290.aspx
    
    Please mark it as answer if it resolves the issue.

    visit: http://technicalsol.blogspot.com

    Latest ajax control toolkit (Release: May 13, 2009): http://ajaxcontroltoolkit.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27326
  • Re: Setting cache expiration with HttpContext.Current.Cache

    06-17-2009, 12:00 AM
    Answer
    • Contributor
      3,472 point Contributor
    • rami_nassar
    • Member since 10-07-2008, 3:01 AM
    • U.A.E.
    • Posts 788

    use this

     

    Response.Cache.SetExpires(DateTime.Now.AddMinutes(60));
     
    Regards...
    Nassar, Rami (MCP, MCTS)
    My Blog || E-Mail

    Don't forget to click "Mark as Answer" on the post that helped you.

    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
Page 1 of 1 (4 items)