Skip to content

Commit 85fb433

Browse files
committed
Smaller lambda closure capture. See: meerikss@aaaad8e
1 parent b0d3753 commit 85fb433

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

src/WebAPI.OutputCache/CacheOutputAttribute.cs

Lines changed: 9 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, Guid.NewGuid().ToString());
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
}

src/WebApi.OutputCache.V2/CacheOutputAttribute.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,15 +193,17 @@ private async Task OnActionExecuted(HttpActionExecutedContext actionExecutedCont
193193
{
194194
SetEtag(actionExecutedContext.Response, Guid.NewGuid().ToString());
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);

0 commit comments

Comments
 (0)