Skip to content

Commit 5a055b2

Browse files
authored
Merge pull request filipw#202 from dmalanij/LastModifiedImplementation
Included LastModified in response headers
2 parents 19c4789 + eb76db3 commit 5a055b2

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
@@ -205,6 +205,7 @@ public override void OnActionExecuting(HttpActionContext actionContext)
205205
if (val == null) return;
206206

207207
var contenttype = _webApiCache.Get<MediaTypeHeaderValue>(cachekey + Constants.ContentTypeKey) ?? responseMediaType;
208+
var contentGenerationTimestamp = DateTimeOffset.Parse(_webApiCache.Get<string>(cachekey + Constants.GenerationTimestampKey));
208209

209210
actionContext.Response = actionContext.Request.CreateResponse();
210211
actionContext.Response.Content = new ByteArrayContent(val);
@@ -214,7 +215,7 @@ public override void OnActionExecuting(HttpActionContext actionContext)
214215
if (responseEtag != null) SetEtag(actionContext.Response, responseEtag);
215216

216217
var cacheTime = CacheTimeQuery.Execute(DateTime.Now);
217-
ApplyCacheHeaders(actionContext.Response, cacheTime);
218+
ApplyCacheHeaders(actionContext.Response, cacheTime, contentGenerationTimestamp);
218219
}
219220

220221
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
@@ -223,8 +224,9 @@ public override async Task OnActionExecutedAsync(HttpActionExecutedContext actio
223224

224225
if (!IsCachingAllowed(actionExecutedContext.ActionContext, AnonymousOnly)) return;
225226

226-
var cacheTime = CacheTimeQuery.Execute(DateTime.Now);
227-
if (cacheTime.AbsoluteExpiration > DateTime.Now)
227+
var actionExecutionTimestamp = DateTimeOffset.Now;
228+
var cacheTime = CacheTimeQuery.Execute(actionExecutionTimestamp.DateTime);
229+
if (cacheTime.AbsoluteExpiration > actionExecutionTimestamp)
228230
{
229231
var httpConfig = actionExecutedContext.Request.GetConfiguration();
230232
var config = httpConfig.CacheOutputConfiguration();
@@ -261,14 +263,19 @@ public override async Task OnActionExecutedAsync(HttpActionExecutedContext actio
261263
_webApiCache.Add(cachekey + Constants.EtagKey,
262264
etag,
263265
cacheTime.AbsoluteExpiration, baseKey);
266+
267+
268+
_webApiCache.Add(cachekey + Constants.GenerationTimestampKey,
269+
actionExecutionTimestamp.ToString(),
270+
cacheTime.AbsoluteExpiration, baseKey);
264271
}
265272
}
266273
}
267274

268-
ApplyCacheHeaders(actionExecutedContext.ActionContext.Response, cacheTime);
275+
ApplyCacheHeaders(actionExecutedContext.ActionContext.Response, cacheTime, actionExecutionTimestamp);
269276
}
270277

271-
protected virtual void ApplyCacheHeaders(HttpResponseMessage response, CacheTime cacheTime)
278+
protected virtual void ApplyCacheHeaders(HttpResponseMessage response, CacheTime cacheTime, DateTimeOffset? contentGenerationTimestamp = null)
272279
{
273280
if (cacheTime.ClientTimeSpan > TimeSpan.Zero || MustRevalidate || Private)
274281
{
@@ -287,6 +294,10 @@ protected virtual void ApplyCacheHeaders(HttpResponseMessage response, CacheTime
287294
response.Headers.CacheControl = new CacheControlHeaderValue { NoCache = true };
288295
response.Headers.Add("Pragma", "no-cache");
289296
}
297+
if ((response.Content != null) && contentGenerationTimestamp.HasValue)
298+
{
299+
response.Content.Headers.LastModified = contentGenerationTimestamp.Value;
300+
}
290301
}
291302

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

0 commit comments

Comments
 (0)