Skip to content

Commit 71bb828

Browse files
committed
Merge pull request #127 from Thorium/master
Allow override for custom ETag creation
2 parents b0d3753 + e84b4a1 commit 71bb828

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

src/WebAPI.OutputCache/CacheOutputAttribute.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,23 +178,26 @@ public override void OnActionExecuted(HttpActionExecutedContext actionExecutedCo
178178

179179
if (!string.IsNullOrWhiteSpace(cachekey) && !(WebApiCache.Contains(cachekey)))
180180
{
181-
SetEtag(actionExecutedContext.Response, Guid.NewGuid().ToString());
181+
var response = actionExecutedContext.Response;
182+
var actionContext = actionExecutedContext.ActionContext;
183+
SetEtag(response, CreateEtag(actionExecutedContext, cachekey, cacheTime));
182184

183-
if (actionExecutedContext.Response.Content != null)
185+
186+
if (response.Content != null)
184187
{
185-
actionExecutedContext.Response.Content.ReadAsByteArrayAsync().ContinueWith(t =>
188+
response.Content.ReadAsByteArrayAsync().ContinueWith(t =>
186189
{
187-
var baseKey = config.MakeBaseCachekey(actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerName, actionExecutedContext.ActionContext.ActionDescriptor.ActionName);
190+
var baseKey = config.MakeBaseCachekey(actionContext.ControllerContext.ControllerDescriptor.ControllerName, actionContext.ActionDescriptor.ActionName);
188191

189192
WebApiCache.Add(baseKey, string.Empty, cacheTime.AbsoluteExpiration);
190193
WebApiCache.Add(cachekey, t.Result, cacheTime.AbsoluteExpiration, baseKey);
191194

192195
WebApiCache.Add(cachekey + Constants.ContentTypeKey,
193-
actionExecutedContext.Response.Content.Headers.ContentType.MediaType,
196+
response.Content.Headers.ContentType.MediaType,
194197
cacheTime.AbsoluteExpiration, baseKey);
195198

196199
WebApiCache.Add(cachekey + Constants.EtagKey,
197-
actionExecutedContext.Response.Headers.ETag.Tag,
200+
response.Headers.ETag.Tag,
198201
cacheTime.AbsoluteExpiration, baseKey);
199202
});
200203
}
@@ -204,6 +207,11 @@ public override void OnActionExecuted(HttpActionExecutedContext actionExecutedCo
204207
ApplyCacheHeaders(actionExecutedContext.ActionContext.Response, cacheTime);
205208
}
206209

210+
protected virtual string CreateEtag(HttpActionExecutedContext actionExecutedContext, string cachekey, CacheTime cacheTime)
211+
{
212+
return Guid.NewGuid().ToString();
213+
}
214+
207215
private void ApplyCacheHeaders(HttpResponseMessage response, CacheTime cacheTime)
208216
{
209217
if (cacheTime.ClientTimeSpan > TimeSpan.Zero || MustRevalidate || Private)

src/WebApi.OutputCache.V2/CacheOutputAttribute.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,19 @@ private async Task OnActionExecuted(HttpActionExecutedContext actionExecutedCont
191191

192192
if (!string.IsNullOrWhiteSpace(cachekey) && !(_webApiCache.Contains(cachekey)))
193193
{
194-
SetEtag(actionExecutedContext.Response, Guid.NewGuid().ToString());
194+
SetEtag(actionExecutedContext.Response, CreateEtag(actionExecutedContext, cachekey, cacheTime));
195195

196-
if (actionExecutedContext.Response.Content != null)
196+
var responseContent = actionExecutedContext.Response.Content;
197+
198+
if (responseContent != null)
197199
{
198200
var baseKey = config.MakeBaseCachekey(actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerName, actionExecutedContext.ActionContext.ActionDescriptor.ActionName);
199-
var contentType = actionExecutedContext.Response.Content.Headers.ContentType;
201+
var contentType = responseContent.Headers.ContentType;
200202
string etag = actionExecutedContext.Response.Headers.ETag.Tag;
201203
//ConfigureAwait false to avoid deadlocks
202-
var content = await actionExecutedContext.Response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
203-
204-
actionExecutedContext.Response.Content.Headers.Remove("Content-Length");
204+
var content = await responseContent.ReadAsByteArrayAsync().ConfigureAwait(false);
205+
206+
responseContent.Headers.Remove("Content-Length");
205207

206208
_webApiCache.Add(baseKey, string.Empty, cacheTime.AbsoluteExpiration);
207209
_webApiCache.Add(cachekey, content, cacheTime.AbsoluteExpiration, baseKey);
@@ -242,6 +244,11 @@ protected virtual void ApplyCacheHeaders(HttpResponseMessage response, CacheTime
242244
}
243245
}
244246

247+
protected virtual string CreateEtag(HttpActionExecutedContext actionExecutedContext, string cachekey, CacheTime cacheTime)
248+
{
249+
return Guid.NewGuid().ToString();
250+
}
251+
245252
private static void SetEtag(HttpResponseMessage message, string etag)
246253
{
247254
if (etag != null)

0 commit comments

Comments
 (0)