Is there a way to access a cache or session from a static method? Is there a way to access a cache or session from a static method? asp.net asp.net

Is there a way to access a cache or session from a static method?


System.Web.HttpContext.Current.Cache

Cache is shared per app domain - not per Page. Page just has a convenience property of Page.Cache to get the current Cache, which means you can just do Cache["key"] from a method in a page.

As you've noticed, if you're in a static method - then you have no Page instance, and you have no Page.Cache property. So, you need to use HttpContext.Cache. But, wait - you have no HttpContext instance either! That's ok, the currently executing instance is stored at the static property of HttpContext.Current.

So - to answer your question - in a static method, use HttpContext.Current.Cache. BTW, you can also access the Request and Response properties from there.


I think calling a PageMethod may be the best you can really do, if you really want to do this:

http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/


Javascript is client side, the Cache is on the Server Side, so you need to do a callback to a method in your asp.net application, that returns the content of the cache.

The ASP.NET Cache API is really good, you can use Cache["Key"] to get the cached content that you like. Read more here : http://msdn.microsoft.com/en-us/library/ms972379.aspx