Skip to content

Commit eb76db3

Browse files
author
dmalanij
committed
Included LastModified in response headers
Implemented logic to keep record of the timestamp when a response content was generated and use it as the Last-Modified response header
1 parent 37bc932 commit eb76db3

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/WebApi.OutputCache.Core/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ public sealed class Constants
44
{
55
public const string ContentTypeKey = ":response-ct";
66
public const string EtagKey = ":response-etag";
7+
public const string GenerationTimestampKey = ":response-generationtimestamp";
78
}
89
}

src/WebApi.OutputCache.V2/CacheOutputAttribute.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ public override void OnActionExecuting(HttpActionContext actionContext)
195195
if (val == null) return;
196196

197197
var contenttype = _webApiCache.Get<MediaTypeHeaderValue>(cachekey + Constants.ContentTypeKey) ?? responseMediaType;
198+
var contentGenerationTimestamp = DateTimeOffset.Parse(_webApiCache.Get<string>(cachekey + Constants.GenerationTimestampKey));
198199

199200
actionContext.Response = actionContext.Request.CreateResponse();
200201
actionContext.Response.Content = new ByteArrayContent(val);
@@ -204,7 +205,7 @@ public override void OnActionExecuting(HttpActionContext actionContext)
204205
if (responseEtag != null) SetEtag(actionContext.Response, responseEtag);
205206

206207
var cacheTime = CacheTimeQuery.Execute(DateTime.Now);
207-
ApplyCacheHeaders(actionContext.Response, cacheTime);
208+
ApplyCacheHeaders(actionContext.Response, cacheTime, contentGenerationTimestamp);
208209
}
209210

210211
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
@@ -213,8 +214,9 @@ public override async Task OnActionExecutedAsync(HttpActionExecutedContext actio
213214

214215
if (!IsCachingAllowed(actionExecutedContext.ActionContext, AnonymousOnly)) return;
215216

216-
var cacheTime = CacheTimeQuery.Execute(DateTime.Now);
217-
if (cacheTime.AbsoluteExpiration > DateTime.Now)
217+
var actionExecutionTimestamp = DateTimeOffset.Now;
218+
var cacheTime = CacheTimeQuery.Execute(actionExecutionTimestamp.DateTime);
219+
if (cacheTime.AbsoluteExpiration > actionExecutionTimestamp)
218220
{
219221
var httpConfig = actionExecutedContext.Request.GetConfiguration();
220222
var config = httpConfig.CacheOutputConfiguration();
@@ -251,14 +253,19 @@ public override async Task OnActionExecutedAsync(HttpActionExecutedContext actio
251253
_webApiCache.Add(cachekey + Constants.EtagKey,
252254
etag,
253255
cacheTime.AbsoluteExpiration, baseKey);
256+
257+
258+
_webApiCache.Add(cachekey + Constants.GenerationTimestampKey,
259+
actionExecutionTimestamp.ToString(),
260+
cacheTime.AbsoluteExpiration, baseKey);
254261
}
255262
}
256263
}
257264

258-
ApplyCacheHeaders(actionExecutedContext.ActionContext.Response, cacheTime);
265+
ApplyCacheHeaders(actionExecutedContext.ActionContext.Response, cacheTime, actionExecutionTimestamp);
259266
}
260267

261-
protected virtual void ApplyCacheHeaders(HttpResponseMessage response, CacheTime cacheTime)
268+
protected virtual void ApplyCacheHeaders(HttpResponseMessage response, CacheTime cacheTime, DateTimeOffset? contentGenerationTimestamp = null)
262269
{
263270
if (cacheTime.ClientTimeSpan > TimeSpan.Zero || MustRevalidate || Private)
264271
{
@@ -277,6 +284,10 @@ protected virtual void ApplyCacheHeaders(HttpResponseMessage response, CacheTime
277284
response.Headers.CacheControl = new CacheControlHeaderValue { NoCache = true };
278285
response.Headers.Add("Pragma", "no-cache");
279286
}
287+
if ((response.Content != null) && contentGenerationTimestamp.HasValue)
288+
{
289+
response.Content.Headers.LastModified = contentGenerationTimestamp.Value;
290+
}
280291
}
281292

282293
protected virtual string CreateEtag(HttpActionExecutedContext actionExecutedContext, string cachekey, CacheTime cacheTime)

0 commit comments

Comments
 (0)