Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ASP.NET: how to access cache when no HttpContext.Current is available (is null)?

During Application_End() in Global.aspx, HttpContext.Current is null. I still want to be able to access cache - it's in memory, so want to see if I can reference it somehow to save bits to disk.

Question - is there a way to reference cache in memory somehow when HttpContext.Current is null?

Perhaps I could create a global static variable that would store pointer to cache that I could update on HTTP requests (pseudo: "static <pointer X>" = HttpRequest.Current) and retrieve a reference to cache through that pointer in Application_End()?

Is there a better way to access Cache in memory when there is no Http Request is made?

like image 550
Carl J. Avatar asked Dec 15 '10 08:12

Carl J.


1 Answers

You should be able to access it via HttpRuntime.Cache

http://www.hanselman.com/blog/UsingTheASPNETCacheOutsideOfASPNET.aspx

According to Scott - looking at Reflector HttpContext.Current.Cache just calls HttpRuntime.Cache - so you might as well always access it this way.

like image 63
James Gaunt Avatar answered Nov 15 '22 16:11

James Gaunt