Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Troubleshooting ASP.Net caching - CacheDuration property seems to have no effect

I'm trying to get ASP.Net to cache the response of a web service request by setting the CacheDuration property of the WebMethod attribute:

[WebMethod(CacheDuration = 60)]
[ScriptMethod(UseHttpGet = true)]
public static List<string> GetNames()
{
    return InnerGetNames();
}

The above is a method on an ASP.Net page (I've also tried moving it to its own class, but it didn't seem to make any difference) - I've set UseHttpGet to true because POST requests aren't cached, however despite my best efforts it still doesn't seem to be making any difference (a breakpoint placed at the start of the method is always hit).

This is the code I'm using to call the method:

%.ajax({
    url: "MyPage.aspx/GetNames",
    contentType: "application/json; charset=utf-8",
    success: function() {
        alert("success");
    }

Is there anything that I've missed which might be preventing ASP.Net from caching this method?

Failing that, are there any diagnostic mechanisms I can use to more clearly understand what's going on with the ASP.Net caching?

like image 686
Justin Avatar asked Oct 20 '10 05:10

Justin


People also ask

How can set cache-control no-store in asp net?

AppendHeader("Pragma", "no-cache"); Response. AppendHeader("Expires", "0"); The first line sets Cache-control to no-cache , and the second line adds the other attributes no-store, must-revalidate . This may not be the only way, but does provide an alternative method if the more straightforward Response.

What does the location property of the ResponseCache attribute control?

ResponseCache Attribute Similarly, the Location property will set the location in the cache-control header. Since we set the location as Any , both the client and server will be able to cache the response, which is equivalent to the public directive of the cache-control header.

How will you create the caching in asp net?

To manually cache application data, you can use the MemoryCache class in ASP.NET. ASP.NET also supports output caching, which stores the generated output of pages, controls, and HTTP responses in memory. You can configure output caching declaratively in an ASP.NET Web page or by using settings in the Web. config file.

How can get data from cache in asp net?

From an ASP.NET page s code-behind class, the data cache can be accessed using the Page class s Cache property, and allows for syntax like Cache["key"] = value , as discussed in Step 2. From a class within the architecture, the data cache can be accessed using either HttpRuntime. Cache or HttpContext. Current.


1 Answers

According to this MSDN How to article, the CacheDuration property of the WebMethod attribute is for XML WebMethods. Since the ScriptMethod attribute specifies to return JSON, we're forced to use object level caching instead:

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static List<string> GetNames()
{
    var result = GetCache<List<string>>("GetNames");
    if(result == null)
    {
        result = InnerGetNames();
        SetCache("GetNames", result, 60);
    }
    return result;
}

protected static void SetCache<T>(string key, T obj, double duration)
{
    HttpContext.Current.Cache.Insert(key, obj, null, DateTime.Now.AddSeconds(duration), System.Web.Caching.Cache.NoSlidingExpiration);
}

protected static T GetCache<T>(string key) where T : class
{
    return HttpContext.Current.Cache.Get(key) as T;
}
like image 68
ericdagenais Avatar answered Oct 16 '22 21:10

ericdagenais