0

My web-site returns information for items which it takes from disk (involving some logic located in the controller, so it is not just static assets). I tried to optimize it by returning 304 for items which are not changed, by getting file write time for the corresponding item. Now, after I update the code, my application still thinks that an item is not updated and returns 304 - it does not realize that application code is changed so the result would be different. Because of that users do not see the update immediately, only after they get rid of their cache. I would like to solve that problem by checking not only 'item update time' but also 'application update time'. Is there a way to get something like time when application was updated? By this time I would like to see kind of maximum of update times of all application files.

UPD: As asked for example code, here is slightly simplified version:

    public static DateTime? LastKnownDate(this HttpRequestBase request)
    {
        if (!string.IsNullOrEmpty(request.Headers["If-Modified-Since"]))
        {
            var provider = CultureInfo.InvariantCulture;
            DateTime date;
            if (DateTime.TryParse(
                request.Headers["If-Modified-Since"],
                provider,
                DateTimeStyles.RoundtripKind,
                out date)) return date;
        }
        return null;
    }

    public ActionResult Test(int id)
    {
        var path = @"C:\data\" + id;
        var file = new FileInfo(path);
        if (!file.Exists) return HttpNotFound();
        var date = Request.LastKnownDate();
        if (date != null && date >= file.LastWriteTimeUtc)
        {
            return Response.NotModified();
        }

        Response.AddHeader("Last-Modified", file.LastWriteTimeUtc.ToString("o"));
        return File(path, "application/octet-stream");
    }
ironic
  • 8,368
  • 7
  • 35
  • 44
  • Are the user agents getting a real 304 from the server, or are faking one because they think the local cached copy is still valid and return that one without querying the server? – Albireo Oct 24 '14 at 12:01
  • @Albireo, interesting... do you know if chrome would normally fake 304? I am testing using online version of server, nothing special... – ironic Oct 24 '14 at 12:07
  • @Albireo, anyway Fiddler shows request to server... so I think the requests are real :) – ironic Oct 24 '14 at 12:11
  • Can you add a [minimal example](http://stackoverflow.com/help/mcve) regarding how you're setting and returning the 304? – Albireo Oct 24 '14 at 12:13
  • @Albireo, added. For now I used the linux way: added a 'reset' file which is checked on every 'cacheable' request. when I update the data I 'touch' this file. – ironic Oct 24 '14 at 13:02

1 Answers1

0

I think you need something like HTTP conditional GET. More details in the spec.

This is how you can do that in ASP.NET : http://optimizeasp.net/conditional-get

Also take a loot at: Improving Performance with Output Caching

vtortola
  • 34,709
  • 29
  • 161
  • 263
  • 1
    Thank you for trying to help. Conditional get is exactly what I am using. I think I did not describe my problem well, so I updated the question... – ironic Oct 24 '14 at 12:01