I’ve been working on updating our services to improve caching but also to deal with stale data updates. I’ve implemented Last-Modified/ETag on GET requests, but I’m a little bit stuck on POST.
We would have data fetched with a GET, and that Last-Modified in the response is what’s required as the date to pass in the If-Unmodified-Since header to specific POST request. I don’t see any cache extensions for this header.
I’m also wondering if there are best practices for this in SS or any direction on best approach.
ServiceStack’s HTTP Caching is only applied to GET requests, there is no explicit support for If-Unmodified-Since Header which is primarily used for optimistic concurrency control not caching.
So you could access it like any other HTTP Header, e.g:
var checkUnmodifiedSince = base.Request.GetHeader("If-Unmodified-Since");
Right, so optimistic concurrency control is what I was referring to when I said stale data update. Regardless, correct, it’s not caching and probably shouldn’t have been labeled as such. I just added some simple extensions to check it exactly that way, and one for creating an exception with 412 response.
public static class HttpAdditionalExtensions
{
public static bool UnModifiedSince(this IRequest req, DateTime? lastModified)
{
if (lastModified.HasValue)
{
string header = req.Headers[HttpHeaders.IfUnmodifiedSince];
DateTime result;
if (header == null)
return false;
if (header != null && DateTime.TryParse(header, new DateTimeFormatInfo(), DateTimeStyles.RoundtripKind, out result))
{
DateTime universalTime = lastModified.Value.Truncate(TimeSpan.FromSeconds(1.0)).ToUniversalTime();
return result >= universalTime;
}
}
return false;
}
public static Exception NotModified(string description = null)
{
return (Exception)new HttpError(HttpStatusCode.PreconditionFailed, description);
}
}
Sometimes just writing a post and asking a question makes it more clear. Added my code above if anyone is doing the same.