3

I'm not sure what caused this, but our controller action use to cache its output but it no longer does:

[HttpGet]
[OutputCache(VaryByParam = "slug", Duration = 3600)]
public ContentResult Index(string slug)
{
    // each call to the page returns a new tick value
    var viewData = DateTime.Now.Ticks.ToString();
    return View(viewData);
}

Before, I could debug the page, put a break point inside the Index method and the first call to the page would trigger the breakpoint, and the subsequent calls would not. Now, every call to the page triggers the break point.

Even when I am running the site (not debugging), I can trace the SQL calls in the action, which should not be called after the first call.

I don't know when it stopped working, but we somewhat recently upgraded to .Net 4.0 and MVC 3.

Thanks so much.

EDIT

Here are the headers of the page response:

Cache-Control   public, no-cache="Set-Cookie", max-age=1296000
Content-Encoding    gzip
Content-Length  5414
Content-Type    text/html; charset=utf-8
Date    Wed, 18 Sep 2013 00:33:23 GMT
Expires Thu, 03 Oct 2013 00:33:20 GMT
Last-Modified   Wed, 18 Sep 2013 00:33:20 GMT
Server  Microsoft-IIS/7.5
Set-Cookie  tag=tech; expires=Thu, 19-Sep-2013 00:33:23 GMT; path=/
Vary    *
X-AspNet-Version    4.0.30319
X-AspNetMvc-Version 3.0
X-Powered-By    ASP.NET
Swisher Sweet
  • 769
  • 11
  • 33
  • Are you passing different `slug` parameters? – Justin Helgerson Sep 17 '13 at 22:08
  • Yes, slug can be "car-forum" or "bike-forum" as examples. – Swisher Sweet Sep 17 '13 at 22:11
  • Right, but, I presume you aren't passing different parameters each time? The database would be hit every 3600 seconds for each parameter. – Justin Helgerson Sep 17 '13 at 22:59
  • I agree. The problem is that the database is getting hit every time regardless. For example, if I pass slug=car-forum, it hits the database the first time. Then I do the same thing a few seconds later, it hits the database again. It seems to be ignoring the OutpuCacheAttribute altogether. – Swisher Sweet Sep 17 '13 at 23:05
  • 1
    Interesting. Can you show the code inside of your method? – Justin Helgerson Sep 18 '13 at 00:03
  • 1
    Also, can you post the HTTP headers of the response you get from the server? – Justin Helgerson Sep 18 '13 at 00:07
  • The code inside the method is nothing special. It just calls a service, which returns data that needs to be displayed on Index. The service, in turn, calls the database. I've simplified the code and the method implementation just returns the value of DateTime.Now.Ticks.ToString()`. I can refresh every few seconds and get a new value. – Swisher Sweet Sep 18 '13 at 00:16
  • Ek0nomik, thanks so much for your help... without it I wouldn't have found the issue. – Swisher Sweet Sep 18 '13 at 00:46

1 Answers1

3

The problem is that I was setting a cookie value in the controller action:

HttpContext.Response.Cookies.Add(myCookie);

Which changed the header from this:

Cache-Control: public, max-age=1295931

To this:

Cache-Control: public, no-cache="Set-Cookie", max-age=1296000

By commenting the line that was added the cookie, the page cached again.

Swisher Sweet
  • 769
  • 11
  • 33