Skip to content

Commit 41b167d

Browse files
Allow caching by minutes and seconds.
1 parent eec9fe8 commit 41b167d

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
}

src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ public class ImageSharpMiddlewareOptions
3636
/// </summary>
3737
public int MaxCacheDays { get; set; } = 365;
3838

39+
/// <summary>
40+
/// Gets or sets the number of seconds to store images in the image cache.
41+
/// </summary>
42+
public int MaxCacheMinutes { get; set; }
43+
44+
/// <summary>
45+
/// Gets or sets the number of minutes to store images in the image cache.
46+
/// </summary>
47+
public int MaxCacheSeconds { get; set; }
48+
3949
/// <summary>
4050
/// Gets or sets the length of the filename to use (minus the extension) when storing images in the image cache.
4151
/// </summary>

0 commit comments

Comments
 (0)