Skip to content

Commit a5dd409

Browse files
Allow browser cache by minutes and seconds
1 parent 41b167d commit a5dd409

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

src/ImageSharp.Web/Middleware/ImageContext.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void ComprehendRequestHeaders(DateTimeOffset lastModified, long length)
127127
/// <returns>The <see cref="Task"/>.</returns>
128128
public Task SendStatusAsync(int statusCode, in ImageCacheMetadata metaData)
129129
{
130-
this.ApplyResponseHeaders(statusCode, metaData.ContentType, this.ComputeMaxAge(metaData));
130+
this.ApplyResponseHeaders(statusCode, metaData.ContentType, metaData.CacheControlMaxAge);
131131

132132
// this.logger.LogHandled(statusCode, SubPath);
133133
return ResponseConstants.CompletedTask;
@@ -141,7 +141,7 @@ public Task SendStatusAsync(int statusCode, in ImageCacheMetadata metaData)
141141
/// <returns>The <see cref="Task"/>.</returns>
142142
public async Task SendAsync(Stream stream, ImageCacheMetadata metaData)
143143
{
144-
this.ApplyResponseHeaders(ResponseConstants.Status200Ok, metaData.ContentType, this.ComputeMaxAge(metaData));
144+
this.ApplyResponseHeaders(ResponseConstants.Status200Ok, metaData.ContentType, metaData.CacheControlMaxAge);
145145

146146
if (stream.CanSeek)
147147
{
@@ -267,17 +267,5 @@ private void ComputeIfModifiedSince()
267267
this.ifUnmodifiedSinceState = unmodified ? PreconditionState.ShouldProcess : PreconditionState.PreconditionFailed;
268268
}
269269
}
270-
271-
private TimeSpan ComputeMaxAge(in ImageCacheMetadata metaData)
272-
{
273-
// 14.9.3 CacheControl Max-Age
274-
var maxAge = TimeSpan.FromDays(this.options.MaxBrowserCacheDays);
275-
if (!metaData.CacheControlMaxAge.Equals(TimeSpan.MinValue))
276-
{
277-
maxAge = metaData.CacheControlMaxAge;
278-
}
279-
280-
return maxAge;
281-
}
282270
}
283271
}

src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.ComponentModel;
67
using System.Diagnostics;
78
using System.IO;
89
using System.Linq;
@@ -77,6 +78,11 @@ public class ImageSharpMiddleware
7778
/// </summary>
7879
private readonly DateTimeOffset minCacheLastWriteTimeUtc;
7980

81+
/// <summary>
82+
/// The maximum time to store the response in a browser cache.
83+
/// </summary>
84+
private readonly TimeSpan maxBrowserCacheDuration;
85+
8086
/// <summary>
8187
/// The collection of known commands gathered from the processors.
8288
/// </summary>
@@ -128,6 +134,7 @@ public ImageSharpMiddleware(
128134
this.cache = cache;
129135
this.cacheHash = cacheHash;
130136
this.minCacheLastWriteTimeUtc = this.GetMaxCacheDateTimeOffset();
137+
this.maxBrowserCacheDuration = this.GetMaxBrowserCacheDuration();
131138

132139
var commands = new HashSet<string>();
133140
foreach (IImageWebProcessor processor in this.processors)
@@ -256,9 +263,10 @@ private async Task ProcessRequestAsync(
256263
format = image.Format;
257264
}
258265

259-
// Check to see if the source metadata has a cachecontrol max-age value and use it to
260-
// override the default max age from our options.
261-
var maxAge = TimeSpan.FromDays(this.options.MaxBrowserCacheDays);
266+
// 14.9.3 CacheControl Max-Age
267+
// Check to see if the source metadata has a CacheControl Max-Age value
268+
// and use it to override the default max age from our options.
269+
TimeSpan maxAge = this.maxBrowserCacheDuration;
262270
if (!sourceImageMetadata.CacheControlMaxAge.Equals(TimeSpan.MinValue))
263271
{
264272
maxAge = sourceImageMetadata.CacheControlMaxAge;
@@ -416,5 +424,12 @@ private DateTimeOffset GetMaxCacheDateTimeOffset()
416424
.AddMinutes(-this.options.MaxCacheMinutes)
417425
.AddSeconds(-this.options.MaxCacheSeconds);
418426
}
427+
428+
private TimeSpan GetMaxBrowserCacheDuration()
429+
{
430+
return TimeSpan.FromDays(this.options.MaxBrowserCacheDays)
431+
.Add(TimeSpan.FromMinutes(this.options.MaxBrowserCacheMinutes))
432+
.Add(TimeSpan.FromSeconds(this.options.MaxBrowserCacheSeconds));
433+
}
419434
}
420435
}

src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,23 @@ public class ImageSharpMiddlewareOptions
2828

2929
/// <summary>
3030
/// Gets or sets the number of days to store images in the browser cache.
31+
/// Defaults to 7.
3132
/// </summary>
3233
public int MaxBrowserCacheDays { get; set; } = 7;
3334

35+
/// <summary>
36+
/// Gets or sets the number of minutes to store images in the browser cache.
37+
/// </summary>
38+
public int MaxBrowserCacheMinutes { get; set; }
39+
40+
/// <summary>
41+
/// Gets or sets the number of seconds to store images in the browser cache.
42+
/// </summary>
43+
public int MaxBrowserCacheSeconds { get; set; }
44+
3445
/// <summary>
3546
/// Gets or sets the number of days to store images in the image cache.
47+
/// Defaults to 365.
3648
/// </summary>
3749
public int MaxCacheDays { get; set; } = 365;
3850

@@ -47,7 +59,8 @@ public class ImageSharpMiddlewareOptions
4759
public int MaxCacheSeconds { get; set; }
4860

4961
/// <summary>
50-
/// Gets or sets the length of the filename to use (minus the extension) when storing images in the image cache.
62+
/// Gets or sets the length of the filename to use (minus the extension) when storing
63+
/// images in the image cache. Defaults to 12.
5164
/// </summary>
5265
public uint CachedNameLength { get; set; } = 12;
5366

0 commit comments

Comments
 (0)