Search code examples
c#asp.netweb-servicescachingpagemethods

Caching with Sliding Expiration in ASP.NET Page Methods


I know that when declaring a page method in ASP.NET, I can specify a CacheDuration like so:

[WebMethod(CacheDuration=60)]
public static void Foo()
{
    //TODO Bar
}  

But from what I understand, CacheDuration only supports absolute expiration. I want to have sliding expiration. So that leads me to believe that I need to access the System.Web.Caching.Cache object somehow. But, since page methods are static, and this is essentially a standalone web service, I'm not sure how to access it statically. The only ways I have seen on Google rely on getting it from the HttpContext. But, there is no HttpContext available to me here, right?

Or, do I need to use the System.Runtime.Caching.MemoryCache to do my own caching?

Much thanks.


Solution

  • you can access

    System.Web.HttpContext.Current.Cache
    

    from your page method.