varnishvarnish-vclesi

How to cache dynamic content in varnish?


I am working on an RoR app. I want to set a ttl for sold out items. The sold_out_status function returns the productIds of of sold-out products and is defined as:

def sold_out_status
 query_str = params[:pids]
 pids = query_str.split(",")
 sold_outs = cache(request.fullpath.to_s, :expires_in => 3600.seconds.to_i) do 
  pids.select {|x| (Product.find(x).on_hand <= 0)}.join(",")
 end

render :text => sold_outs
end

Is there a way I can cache the products in varnish and set a ttl for the sold-out products. Some research indicated that ESI could be a good idea. But, I could not find a detailed documentation which I could follow to implement this. Also, how can I access the output of sold_out_status in varnish?

EDIT: The sold out products on an page can be found by following a Request URL, the page corresponding to this URL contains ids of the sold-out products rendered as text.


Solution

  • Add the following in vcl_fetch:

    sub vcl_fetch{
      if (req.url ~ "^/taxons/sold_out_status.") {
        std.syslog(180, "setting TTL for sold out products");
        set beresp.do_esi = true;
        set beresp.ttl = 1s;
        return(deliver);
    }