public static void Add(CacheHolder value, string key, DateTime expiration) { lock (Cache) { Cache.Add(key, value, expiration); } }
public async override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken) { //if current request is GET, there is no errors, and expiration date > now if (actionExecutedContext.Request.Method.Method.ToUpper() == "GET" && actionExecutedContext.Response.IsSuccessStatusCode && _expirationDate > DateTime.Now) { var key = GenerateCacheKey(actionExecutedContext.ActionContext); var contentType = actionExecutedContext.Response.Content.Headers.ContentType; var value = await(actionExecutedContext.Response.Content as ObjectContent).ReadAsByteArrayAsync().ConfigureAwait(false); //create cache holder and add it to memory list var cacheHolder = new CacheHolder(_expirationDate, value, key, contentType); GalileoMemoryStorage.Add(cacheHolder, key, _expirationDate); //set cache control header for clients to know that content is cached var secondsLeft = Math.Round((GalileoMemoryStorage.Get(key).ExpirationDate - DateTime.Now).TotalSeconds); actionExecutedContext.Response.Headers.Add("Cache-Control", string.Format("max-age={0}", secondsLeft)); actionExecutedContext.Response.Headers.Add("ETag", string.Format("\"{0}\"", cacheHolder.ETag)); } await base.OnActionExecutedAsync(actionExecutedContext, cancellationToken); }