Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/WebApi.OutputCache.Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public sealed class Constants
{
public const string ContentTypeKey = ":response-ct";
public const string EtagKey = ":response-etag";
public const string Headers = ":headers";
}
}
39 changes: 37 additions & 2 deletions src/WebApi.OutputCache.V2/CacheOutputAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -61,7 +63,12 @@ public class CacheOutputAttribute : FilterAttribute, IActionFilter
/// Class used to generate caching keys
/// </summary>
public Type CacheKeyGenerator { get; set; }


/// <summary>
/// Comma seperated list of HTTP headers to cache
/// </summary>
public string HeadersToInclude { get; set; }

private MediaTypeHeaderValue _responseMediaType;

// cache repository
Expand Down Expand Up @@ -143,6 +150,8 @@ private void OnActionExecuting(HttpActionContext actionContext)

if (!_webApiCache.Contains(cachekey)) return;

var responseHeaders = _webApiCache.Get(cachekey + Constants.Headers) as string;

if (actionContext.Request.Headers.IfNoneMatch != null)
{
var etag = _webApiCache.Get(cachekey + Constants.EtagKey) as string;
Expand All @@ -152,6 +161,7 @@ private void OnActionExecuting(HttpActionContext actionContext)
{
var time = CacheTimeQuery.Execute(DateTime.Now);
var quickResponse = actionContext.Request.CreateResponse(HttpStatusCode.NotModified);
if (responseHeaders != null) AddCachedHeaders(quickResponse, responseHeaders);
ApplyCacheHeaders(quickResponse, time);
actionContext.Response = quickResponse;
return;
Expand All @@ -171,6 +181,8 @@ private void OnActionExecuting(HttpActionContext actionContext)
var responseEtag = _webApiCache.Get(cachekey + Constants.EtagKey) as string;
if (responseEtag != null) SetEtag(actionContext.Response, responseEtag);

if (responseHeaders != null) AddCachedHeaders(actionContext.Response, responseHeaders);

var cacheTime = CacheTimeQuery.Execute(DateTime.Now);
ApplyCacheHeaders(actionContext.Response, cacheTime);
}
Expand Down Expand Up @@ -215,6 +227,16 @@ private async Task OnActionExecuted(HttpActionExecutedContext actionExecutedCont
_webApiCache.Add(cachekey + Constants.EtagKey,
etag,
cacheTime.AbsoluteExpiration, baseKey);


if (!String.IsNullOrEmpty(HeadersToInclude))
{
string headersSerialized = JsonConvert.SerializeObject(actionExecutedContext.Response.Headers.Where(h => HeadersToInclude.Contains(h.Key)));

_webApiCache.Add(cachekey + Constants.Headers,
headersSerialized,
cacheTime.AbsoluteExpiration, baseKey);
}
}
}
}
Expand Down Expand Up @@ -251,6 +273,19 @@ private static void SetEtag(HttpResponseMessage message, string etag)
}
}

protected virtual void AddCachedHeaders(HttpResponseMessage response, string headers)
{
var headersDeserialized = JsonConvert.DeserializeObject<IEnumerable<KeyValuePair<string, IEnumerable<string>>>>(headers);

foreach (var header in headersDeserialized)
{
foreach (var headerValue in header.Value)
{
response.Headers.Add(header.Key, headerValue);
}
}
}

Task<HttpResponseMessage> IActionFilter.ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
{
if (actionContext == null)
Expand Down