Search code examples
asp.net-mvcazureazure-web-rolesazure-cdn

Azure CDN not caching controller response


I put code from end of this article to my MVC controller method: http://msdn.microsoft.com/en-us/library/windowsazure/gg680299.aspx

I configured cname for cdn and all working fine except I feel that cdn not caching :)

There is CDN url http://cdn.services.idemkvrachu.ru/services/BranchLogo/82f204fe-bb1d-4204-b817-d424e1284b17/E0F4F2AE-B6C2-4516-BE7C-59B649E2C5AC?lastUpdated=635169430040919922&width=499

And this is original url http://prm.idemkvrachu.ru/cdn/services/BranchLogo/82f204fe-bb1d-4204-b817-d424e1284b17/E0F4F2AE-B6C2-4516-BE7C-59B649E2C5AC?lastUpdated=635169430040919922&width=499

This is my code:

Response.Cache.SetExpires(DateTime.Now.AddDays(14));        
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetLastModified(blob.ChangDateOfs.DateTime);
return File(bytes, format);

When I checked timings receiving picture from original link and cdn - I found that timings higher on cdn.

Also I was trying change blob.ChangDateOfs and comparing Last-Modified header from cdn response: it immediately changes.

What's wrong with my code? Maybe this header breaks cdn cache Cache-Control public, no-cache="Set-Cookie" ?


Solution

  • To troubleshoot caching issues the first thing you want to do is validate if your content is actually getting cached or not.o

    To do this you can add the X-LDebug header with a value of 2. An example of doing this against your endpoint with the relevant portions of output included:

    C:\Azure\Tools\wget\bin>wget -S --header "X-LDebug:2" http://cdn.services.idemkvrachu.ru/services/BranchLogo/82f204fe-bb1d-4204-b817-d424e1284b17/E0F4F2AE-B6C2-4516-BE7C-59B649E2C5AC?lastUpdated=635169430040919922&width=499
    
      Cache-Control: public, no-cache="Set-Cookie"
    
      Set-Cookie: ASP.NET_SessionId=nnxb3xqdqetj0uhlffdmtf03; path=/; HttpOnly
    
      Set-Cookie: idCity=31ed5892-d3cb-45eb-bd4f-526cd65f5302; domain=idemkvrachu.cloudapp.net; 
    
      X-Cache: MISS from cds173.sat9.msecn.net
    

    As you can see, you are setting the Cache-Control header to no-cache="Set-Cookie", and then are setting a cookie. This is telling the CDN to not cache the content. Since your code is only setting the cache control to Public I assume that you have a setting in your web.config or aspx page that is modifying the cache control header to add the no-cache="Set-Cookie".