-
Notifications
You must be signed in to change notification settings - Fork 39
Conversation
return false; | ||
} | ||
|
||
// Request allows stale values | ||
if (age < context.RequestCacheControlHeaderValue.MaxStaleLimit) | ||
var maxStaleLimit = context.RequestCacheControlHeaderValue.MaxStaleLimit; | ||
if (maxStaleLimit.HasValue && age - lowestMaxAge < maxStaleLimit) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Misread the RFC previously, the stale limit is the difference between current age and the specified max-age.
return true; | ||
} | ||
|
||
if (cachedResponseHeaders.ETag != null) | ||
{ | ||
foreach (var tag in ifNoneMatchHeader) | ||
{ | ||
if (cachedResponseHeaders.ETag.Compare(tag, useStrongComparison: true)) | ||
if (cachedResponseHeaders.ETag.Compare(tag, useStrongComparison: false)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Misread the RFC previously, the comparison should be a weak comparison.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to have this change singled out in a separate commit.
formatString: "A valid cached response was retrieved for this request. The cached response will be served."); | ||
_log504GatewayTimeoutServed = LoggerMessage.Define( | ||
logLevel: LogLevel.Information, | ||
eventId: 23, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops
TestUtils.AssertLoggedMessageContains( | ||
sink.Writes[0], | ||
LogLevel.Debug, | ||
"Retrieving cache entry for base key"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it's probably better to use eventIDs instead of string matches. More specific and less verbose
eventId: 26, | ||
formatString: "No valid response was retrieved for this request."); | ||
_logVaryByRulesStored = LoggerMessage.Define<string>( | ||
logLevel: LogLevel.Information, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an internal detail. Will set to Debug level
_log304NotModifiedServed = LoggerMessage.Define( | ||
logLevel: LogLevel.Information, | ||
eventId: 23, | ||
formatString: $"The content requested is not modified. A {StatusCodes.Status304NotModified} {ReasonPhrases.GetReasonPhrase(StatusCodes.Status304NotModified)} response will be served."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"The content requested has not been modified."
The 304 will be logged by hosting.
_logCachedResponseServed = LoggerMessage.Define( | ||
logLevel: LogLevel.Information, | ||
eventId: 24, | ||
formatString: "A valid cached response was retrieved for this request. The cached response will be served."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Serving response from cache."
_log504GatewayTimeoutServed = LoggerMessage.Define( | ||
logLevel: LogLevel.Information, | ||
eventId: 25, | ||
formatString: $"No valid response was retrieved for this request but a only-if-cached cache directive was specified. A {StatusCodes.Status504GatewayTimeout} {ReasonPhrases.GetReasonPhrase(StatusCodes.Status504GatewayTimeout)} response will be served."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"The request could not be satisfied from cache and only-if-cached was specified."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"No cached response available for this request and the only-if-cached cache directive was specified."?
_logNoResponseServed = LoggerMessage.Define( | ||
logLevel: LogLevel.Information, | ||
eventId: 26, | ||
formatString: "No valid response was retrieved for this request."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"No cached response available."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"No cached response available for this request."?
_logVaryByRulesStored = LoggerMessage.Define<string>( | ||
logLevel: LogLevel.Information, | ||
eventId: 27, | ||
formatString: "Vary by rules were updated at key {Key}."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't give the key, give the components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I purge the keys from the messages logged during retrieval as well? Those are debug levels so maybe it doesn't hurt to keep them?
_logResponseStored = LoggerMessage.Define<string>( | ||
logLevel: LogLevel.Information, | ||
eventId: 28, | ||
formatString: "The response for this request was stored at key {Key}."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"The response has been stored in the cache."
_logResponseNotStored = LoggerMessage.Define( | ||
logLevel: LogLevel.Information, | ||
eventId: 29, | ||
formatString: "The response could not be stored for this request."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"The response could not be cached." Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lots of reasons, response was not deemed cacheable, buffering was disabled due to invalid operations or exceeding the maximum body length. This is a generic message but I might add more debug messages that can indicate why the response was not stored.
_logResponseContentLengthMismatchNotStored = LoggerMessage.Define( | ||
logLevel: LogLevel.Information, | ||
eventId: 30, | ||
formatString: $"The response could not be stored for this request. The content length specified in the {HeaderNames.ContentLength} header did not match the length of the response."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Warning? Error? This is an app error, though not a cache error. "The response could not be cached because the Content-Length did not match the body length."
_logRequestMethodNotCacheable = LoggerMessage.Define<string>( | ||
logLevel: LogLevel.Debug, | ||
eventId: 1, | ||
formatString: "Request is not cacheable since it is a {Method} HTTP Method."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
due to the HTTP method: {Method}
_logRequestWithAuthorizationNotCacheable = LoggerMessage.Define( | ||
logLevel: LogLevel.Debug, | ||
eventId: 2, | ||
formatString: $"Request is not cacheable since it contains an {HeaderNames.Authorization} header."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because it contains an...
_logRequestWithNoCacheNotCacheable = LoggerMessage.Define( | ||
logLevel: LogLevel.Debug, | ||
eventId: 3, | ||
formatString: "Request is not cacheable since it contains a no-cache cache directive."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because
_logRequestWithPragmaNoCacheNotCacheable = LoggerMessage.Define( | ||
logLevel: LogLevel.Debug, | ||
eventId: 4, | ||
formatString: "Request is not cacheable since it contains a no-cache pragma directive."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because
_logExpirationMinFreshAdded = LoggerMessage.Define<TimeSpan>( | ||
logLevel: LogLevel.Debug, | ||
eventId: 5, | ||
formatString: "A requirement of minimum freshness of {Duration} specified by min-fresh is added."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a min-fresh requirement of {Duration}
🆙📅 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | ||
|
||
public HttpContext HttpContext { get; } | ||
|
||
public ILogger Logger { get; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public?
_logRequestMethodNotCacheable = LoggerMessage.Define<string>( | ||
logLevel: LogLevel.Debug, | ||
eventId: 1, | ||
formatString: "Request is not cacheable due to the HTTP method: {Method}."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'Request is not cacheable'... is the request ever cacheable? 'The request cannot be served from cache...'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea that might be better. I just went with the current name of the method that generates these logs, which is also a little misleading.
_logResponseNotStored = LoggerMessage.Define( | ||
logLevel: LogLevel.Information, | ||
eventId: 27, | ||
formatString: "The response could not be stored for this request."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'cached'
_logResponseContentLengthMismatchNotStored = LoggerMessage.Define( | ||
logLevel: LogLevel.Warning, | ||
eventId: 28, | ||
formatString: $"The response could not be stored for this request because the {HeaderNames.ContentLength} did not match the body length."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'cached'
@@ -24,9 +25,11 @@ public class ResponseCacheContext | |||
private bool _parsedResponseExpires; | |||
|
|||
internal ResponseCacheContext( | |||
HttpContext httpContext) | |||
HttpContext httpContext, | |||
ILogger logger) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is short enough to be kept on same line.
@@ -37,6 +40,8 @@ public class ResponseCacheContext | |||
|
|||
public CachedVaryByRules CachedVaryByRules { get; internal set; } | |||
|
|||
internal ILogger Logger { get; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not public
? after all the context itself is public which means it is open to application consumption.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
talked with @JunTaoLuo and @Tratcher, I bought the explanation. keep it internal then.
return true; | ||
} | ||
|
||
if (cachedResponseHeaders.ETag != null) | ||
{ | ||
foreach (var tag in ifNoneMatchHeader) | ||
{ | ||
if (cachedResponseHeaders.ETag.Compare(tag, useStrongComparison: true)) | ||
if (cachedResponseHeaders.ETag.Compare(tag, useStrongComparison: false)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to have this change singled out in a separate commit.
42ef871
to
eb21f74
Compare
_logExpirationMustRevalidate = LoggerMessage.Define<TimeSpan, TimeSpan>( | ||
logLevel: LogLevel.Debug, | ||
eventId: 7, | ||
formatString: "The age of the entry is {Age} and has exceeded the maximum age of {MaxAge} specified by max-age. It must be revalidated becauses must-revalidate is specified."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
becauses
_logResponseWithoutPublicNotCacheable = LoggerMessage.Define( | ||
logLevel: LogLevel.Debug, | ||
eventId: 11, | ||
formatString: "Response is not cacheable because it does not contain the public cache directive."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be "... the 'public' cache directive"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was wondering how we format all the header names and cache directives. In a few middlewares I didn't see any formatting. Guess I'll use single quotes here.
|
_logResponseCached = LoggerMessage.Define( | ||
logLevel: LogLevel.Information, | ||
eventId: 26, | ||
formatString: "The response has been cached in the cache."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"The response has been cached."
46f021d
to
39d380c
Compare
7141041
to
3b2a2eb
Compare
#42 Adding logging to response caching
cc @BrennanConroy @Tratcher