public override object OnActionExecuting(IWebApi api) { if (api.RequestValues["nocache"] != null) { api.SetHeader("Cache-Control", "no-cache"); return(null); } var _key = GetIdempotentKey(api); var value = api.Cache.AddOrGetExisting(_key, "toomanyrequest", DateTimeOffset.Now.AddSeconds(2)); var now = DateTime.UtcNow; if (value != null) { if (value.ToString().Equals("toomanyrequest")) { api.SetHeader("Cache-Control", "no-cache"); return(new ApiException(ExceptionCode.TooManyRequests)); } DateTime ifModifiedSince; DateTime.TryParse(api.RequestValues.Headers["If-Modified-Since"], out ifModifiedSince); var expire = ifModifiedSince.AddSeconds(_seconds); var sec = _seconds; if (expire > now) { sec = (int)(expire - now).TotalSeconds; } api.SetHeader("Cache-Control", "max-age=" + sec); api.SetHeader("Expires", "-1"); api.SetHeader("ETag", _key); return(value); } api.OnError += Api_OnError; api.SetHeader("Cache-Control", "max-age=" + _seconds); api.SetHeader("Expires", now.AddSeconds(_seconds).ToString("r")); api.SetHeader("ETag", _key); return(null); }
public override object OnActionExecuting(IWebApi api) { api.SetHeader("Cache-Control", "no-cache"); return(null); }