@@ -71,6 +71,12 @@ public class ImageSharpMiddleware
7171 /// </summary>
7272 private readonly ICacheHash cacheHash ;
7373
74+ /// <summary>
75+ /// The minimum allowable last write time for source files.
76+ /// Used to determine whether a file has expired its cache duration.
77+ /// </summary>
78+ private readonly DateTimeOffset minCacheLastWriteTimeUtc ;
79+
7480 /// <summary>
7581 /// The collection of known commands gathered from the processors.
7682 /// </summary>
@@ -121,6 +127,7 @@ public ImageSharpMiddleware(
121127 this . processors = processors as IImageWebProcessor [ ] ?? processors . ToArray ( ) ;
122128 this . cache = cache ;
123129 this . cacheHash = cacheHash ;
130+ this . minCacheLastWriteTimeUtc = this . GetMaxCacheDateTimeOffset ( ) ;
124131
125132 var commands = new HashSet < string > ( ) ;
126133 foreach ( IImageWebProcessor processor in this . processors )
@@ -329,7 +336,7 @@ private async Task<ValueTuple<bool, ImageMetadata>> IsNewOrUpdatedAsync(
329336 {
330337 // Has the cached image expired or has the source image been updated?
331338 if ( cachedImageMetadata . SourceLastWriteTimeUtc == sourceImageMetadata . LastWriteTimeUtc
332- && cachedImageMetadata . CacheLastWriteTimeUtc > DateTimeOffset . UtcNow . AddDays ( - this . options . MaxCacheDays ) )
339+ && cachedImageMetadata . CacheLastWriteTimeUtc > this . minCacheLastWriteTimeUtc )
333340 {
334341 // We're pulling the image from the cache.
335342 using Stream cachedBuffer = await cachedImageResolver . OpenReadAsync ( ) ;
@@ -401,5 +408,13 @@ private static string GetUri(HttpContext context, IDictionary<string, string> co
401408
402409 return sb . ToString ( ) . ToLowerInvariant ( ) ;
403410 }
411+
412+ private DateTimeOffset GetMaxCacheDateTimeOffset ( )
413+ {
414+ return DateTimeOffset . UtcNow
415+ . AddDays ( - this . options . MaxCacheDays )
416+ . AddMinutes ( - this . options . MaxCacheMinutes )
417+ . AddSeconds ( - this . options . MaxCacheSeconds ) ;
418+ }
404419 }
405420}
0 commit comments