@@ -70,6 +70,8 @@ public CachingHandler(ICacheStore cacheStore, IVaryHeaderStore varyHeaderStore)
7070 if ( ! response . StatusCode . IsIn ( _cacheableStatuses ) )
7171 return ResponseValidationResult . NotCacheable ;
7272
73+ // Technically any response is cacheable unless we are told so or some rules
74+ // but we DO NOT deem cacheable a response which does not bother to put CacheControl header
7375 if ( ! response . IsSuccessStatusCode || response . Headers . CacheControl == null ||
7476 response . Headers . CacheControl . NoStore ) // || response.Headers.CacheControl.NoCache was removed. See issue
7577 return ResponseValidationResult . NotCacheable ;
@@ -79,6 +81,9 @@ public CachingHandler(ICacheStore cacheStore, IVaryHeaderStore varyHeaderStore)
7981
8082 response . Headers . Date = response . Headers . Date ?? DateTimeOffset . UtcNow ; // this also helps in cache creation
8183 var dateTimeOffset = response . Headers . Date ;
84+ var age = TimeSpan . Zero ;
85+ if ( response . Headers . Age . HasValue )
86+ age = response . Headers . Age . Value ;
8287
8388 TraceWriter . WriteLine (
8489 String . Format ( "CachedResponse date was => {0} - compared to UTC.Now => {1}" , dateTimeOffset , DateTimeOffset . UtcNow ) , TraceLevel . Verbose ) ;
@@ -94,19 +99,19 @@ public CachingHandler(ICacheStore cacheStore, IVaryHeaderStore varyHeaderStore)
9499 if ( response . Headers . CacheControl . NoCache )
95100 return ResponseValidationResult . MustRevalidate ;
96101
97- // here we use
98- if ( response . Content . Headers . Expires != null &&
99- response . Content . Headers . Expires < DateTimeOffset . UtcNow )
102+ if ( response . Headers . CacheControl . MaxAge != null &&
103+ DateTimeOffset . UtcNow > response . Headers . Date . Value . Add ( response . Headers . CacheControl . MaxAge . Value . Subtract ( age ) ) )
100104 return response . Headers . CacheControl . ShouldRevalidate ( MustRevalidateByDefault )
101105 ? ResponseValidationResult . MustRevalidate : ResponseValidationResult . Stale ;
102106
103- if ( response . Headers . CacheControl . MaxAge != null &&
104- DateTimeOffset . UtcNow > response . Headers . Date . Value . Add ( response . Headers . CacheControl . MaxAge . Value ) )
107+ if ( response . Headers . CacheControl . SharedMaxAge != null &&
108+ DateTimeOffset . UtcNow > response . Headers . Date . Value . Add ( response . Headers . CacheControl . SharedMaxAge . Value . Subtract ( age ) ) )
105109 return response . Headers . CacheControl . ShouldRevalidate ( MustRevalidateByDefault )
106110 ? ResponseValidationResult . MustRevalidate : ResponseValidationResult . Stale ;
107111
108- if ( response . Headers . CacheControl . SharedMaxAge != null &&
109- DateTimeOffset . UtcNow > response . Headers . Date . Value . Add ( response . Headers . CacheControl . SharedMaxAge . Value ) )
112+ // moved this down since Expires is < MaxAge
113+ if ( response . Content . Headers . Expires != null &&
114+ response . Content . Headers . Expires < DateTimeOffset . UtcNow )
110115 return response . Headers . CacheControl . ShouldRevalidate ( MustRevalidateByDefault )
111116 ? ResponseValidationResult . MustRevalidate : ResponseValidationResult . Stale ;
112117
0 commit comments