public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext) { if (actionContext.Request.Method == HttpMethod.Get && this.CacheKey != CacheKey.NO_CACHE) { Models.CacheContainer c = CacheManager.Instance.Retreive(this.CacheKey); if (c != null && c.ETag != null) { if (actionContext.Request.Headers.IfNoneMatch != null) { if (actionContext.Request.Headers.IfNoneMatch.Any(x => x.Tag.Contains(c.ETag))) { // user has already received data response so send back as 304 and client will use cache... actionContext.Response = actionContext.Request.CreateResponse(System.Net.HttpStatusCode.NotModified); SetEtag(actionContext.Response, c.ETag); return; } } // user did not send ETag... maybe new user so set the content and ETag and send back... actionContext.Response = actionContext.Request.CreateResponse( System.Net.HttpStatusCode.OK, c.Data, actionContext.ControllerContext.Configuration.Formatters.JsonFormatter ); SetEtag(actionContext.Response, c.ETag); // since this is users first time receiving this response... cache it in browser ApplyCacheHeaders(actionContext.Response, TimeSpan.FromSeconds(this.ClientDuration)); return; } } }
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { if (actionExecutedContext.Request.Method == HttpMethod.Get && actionExecutedContext.Response.IsSuccessStatusCode) { if (this.CacheKey != CacheKey.NO_CACHE) { string eTag = CreateEtag(actionExecutedContext, this.CacheKey.ToString()); SetEtag(actionExecutedContext.Response, eTag); Models.CacheContainer c = CacheManager.Instance.Retreive(this.CacheKey); if (null == c) { c = new Models.CacheContainer(); } c.ETag = eTag; c.Data = GetResponseContent(actionExecutedContext); CacheManager.Instance.Store(this.CacheKey, c); } ApplyCacheHeaders(actionExecutedContext.Response, TimeSpan.FromSeconds(this.ClientDuration)); } }