How to do donut caching with c# mvc and Varnish?

974 Views Asked by At

I added to varnish config

sub vcl_fetch {

   set beresp.do_esi = true; 

}

}

In my mvc application I've a childaction

<div>@* this should not be cached, I change the returned value in my DB *@
        1 @Html.Action("GetHour", "Index", new { id = 5 })
    </div>

    <div>
        2
        <esi:include>@* this should be cached *@
            @Html.Action("GetHour", "Index", new { id = 5 })
        </esi:include>
    </div>

And added a Request header

Request.Headers.Add("X-Esi", "1");

But Varnish keeps caching entire page.

What do I miss? I've notice in my browser the request header X-Esi doesn't exist. Also Varnish remove properly the tag <esi:include

The code in action GetHour is pretty simple, just retrieve a decimal from SQL Server.

1

There are 1 best solutions below

0
ramires.cabral On

Change this:

<esi:include>@* this should be cached *@
        @Html.Action("GetHour", "Index", new { id = 5 })
    </esi:include>

for this:

<esi:include src="/Index/GetHour/5">
          </esi:include>

And add to Varnish default.vcl:

sub vcl_fetch {
   set beresp.do_esi = true;

  if(bereq.url ~ "/Index/GetHour"){
    set beresp.ttl = 0s;
  }
}

This was partially answered by @ronald in comments above. Had to remove the [ChildActionOnly] annotation also.