Skip to content

Commit 054bc81

Browse files
Simplify to use TimeSpan
1 parent 8898466 commit 054bc81

File tree

5 files changed

+30
-75
lines changed

5 files changed

+30
-75
lines changed

samples/ImageSharp.Web.Sample/Startup.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

4+
using System;
45
using Microsoft.AspNetCore.Builder;
56
using Microsoft.AspNetCore.Hosting;
67
using Microsoft.Extensions.Configuration;
@@ -82,8 +83,8 @@ private void ConfigureDefaultServicesAndCustomOptions(IServiceCollection service
8283
options =>
8384
{
8485
options.Configuration = Configuration.Default;
85-
options.MaxBrowserCacheDays = 7;
86-
options.MaxCacheDays = 365;
86+
options.BrowserMaxAge = TimeSpan.FromDays(7);
87+
options.CacheMaxAge = TimeSpan.FromDays(365);
8788
options.CachedNameLength = 8;
8889
options.OnParseCommands = _ => { };
8990
options.OnBeforeSave = _ => { };
@@ -105,8 +106,8 @@ private void ConfigureCustomServicesAndCustomOptions(IServiceCollection services
105106
options =>
106107
{
107108
options.Configuration = Configuration.Default;
108-
options.MaxBrowserCacheDays = 7;
109-
options.MaxCacheDays = 365;
109+
options.BrowserMaxAge = TimeSpan.FromDays(7);
110+
options.CacheMaxAge = TimeSpan.FromDays(365);
110111
options.CachedNameLength = 8;
111112
options.OnParseCommands = _ => { };
112113
options.OnBeforeSave = _ => { };

src/ImageSharp.Web/DependencyInjection/ImageSharpConfiguration.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using Microsoft.Extensions.Options;
@@ -14,10 +14,6 @@ public class ImageSharpConfiguration : IConfigureOptions<ImageSharpMiddlewareOpt
1414
/// <inheritdoc/>
1515
public void Configure(ImageSharpMiddlewareOptions options)
1616
{
17-
options.Configuration = Configuration.Default;
18-
options.MaxCacheDays = 365;
19-
options.MaxBrowserCacheDays = 7;
20-
options.CachedNameLength = 12;
2117
}
2218
}
23-
}
19+
}

src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,6 @@ public class ImageSharpMiddleware
7272
/// </summary>
7373
private readonly ICacheHash cacheHash;
7474

75-
/// <summary>
76-
/// The minimum allowable last write time for source files.
77-
/// Used to determine whether a file has expired its cache duration.
78-
/// </summary>
79-
private readonly DateTimeOffset minCacheLastWriteTimeUtc;
80-
81-
/// <summary>
82-
/// The maximum time to store the response in a browser cache.
83-
/// </summary>
84-
private readonly TimeSpan maxBrowserCacheDuration;
85-
8675
/// <summary>
8776
/// The collection of known commands gathered from the processors.
8877
/// </summary>
@@ -133,8 +122,6 @@ public ImageSharpMiddleware(
133122
this.processors = processors as IImageWebProcessor[] ?? processors.ToArray();
134123
this.cache = cache;
135124
this.cacheHash = cacheHash;
136-
this.minCacheLastWriteTimeUtc = this.GetMaxCacheDateTimeOffset();
137-
this.maxBrowserCacheDuration = this.GetMaxBrowserCacheDuration();
138125

139126
var commands = new HashSet<string>();
140127
foreach (IImageWebProcessor processor in this.processors)
@@ -266,7 +253,7 @@ private async Task ProcessRequestAsync(
266253
// 14.9.3 CacheControl Max-Age
267254
// Check to see if the source metadata has a CacheControl Max-Age value
268255
// and use it to override the default max age from our options.
269-
TimeSpan maxAge = this.maxBrowserCacheDuration;
256+
TimeSpan maxAge = this.options.BrowserMaxAge;
270257
if (!sourceImageMetadata.CacheControlMaxAge.Equals(TimeSpan.MinValue))
271258
{
272259
maxAge = sourceImageMetadata.CacheControlMaxAge;
@@ -344,7 +331,7 @@ private async Task<ValueTuple<bool, ImageMetadata>> IsNewOrUpdatedAsync(
344331
{
345332
// Has the cached image expired or has the source image been updated?
346333
if (cachedImageMetadata.SourceLastWriteTimeUtc == sourceImageMetadata.LastWriteTimeUtc
347-
&& cachedImageMetadata.CacheLastWriteTimeUtc > this.minCacheLastWriteTimeUtc)
334+
&& cachedImageMetadata.CacheLastWriteTimeUtc > (DateTimeOffset.UtcNow - this.options.CacheMaxAge))
348335
{
349336
// We're pulling the image from the cache.
350337
using Stream cachedBuffer = await cachedImageResolver.OpenReadAsync();
@@ -416,20 +403,5 @@ private static string GetUri(HttpContext context, IDictionary<string, string> co
416403

417404
return sb.ToString().ToLowerInvariant();
418405
}
419-
420-
private DateTimeOffset GetMaxCacheDateTimeOffset()
421-
{
422-
return DateTimeOffset.UtcNow
423-
.AddDays(-this.options.MaxCacheDays)
424-
.AddMinutes(-this.options.MaxCacheMinutes)
425-
.AddSeconds(-this.options.MaxCacheSeconds);
426-
}
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-
}
434406
}
435407
}

src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,26 @@ public class ImageSharpMiddlewareOptions
2727
public RecyclableMemoryStreamManager MemoryStreamManager { get; set; } = new RecyclableMemoryStreamManager();
2828

2929
/// <summary>
30-
/// Gets or sets the number of days to store images in the browser cache.
31-
/// Defaults to 7.
30+
/// Gets or sets the duration to store images in the browser cache.
31+
/// If an image provider provides a Max-Age for a source image then that will override
32+
/// this value.
33+
/// <para>
34+
/// Defaults to 7 days.
35+
/// </para>
3236
/// </summary>
33-
public int MaxBrowserCacheDays { get; set; } = 7;
37+
public TimeSpan BrowserMaxAge { get; set; } = TimeSpan.FromDays(7);
3438

3539
/// <summary>
36-
/// Gets or sets the number of minutes to store images in the browser cache.
40+
/// Gets or sets the duration to store images in the image cache.
41+
/// <para>
42+
/// Defaults to 365 days.
43+
/// </para>
3744
/// </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-
45-
/// <summary>
46-
/// Gets or sets the number of days to store images in the image cache.
47-
/// Defaults to 365.
48-
/// </summary>
49-
public int MaxCacheDays { get; set; } = 365;
50-
51-
/// <summary>
52-
/// Gets or sets the number of seconds to store images in the image cache.
53-
/// </summary>
54-
public int MaxCacheMinutes { get; set; }
55-
56-
/// <summary>
57-
/// Gets or sets the number of minutes to store images in the image cache.
58-
/// </summary>
59-
public int MaxCacheSeconds { get; set; }
45+
public TimeSpan CacheMaxAge { get; set; } = TimeSpan.FromDays(365);
6046

6147
/// <summary>
6248
/// Gets or sets the length of the filename to use (minus the extension) when storing
63-
/// images in the image cache. Defaults to 12.
49+
/// images in the image cache. Defaults to 12 characters.
6450
/// </summary>
6551
public uint CachedNameLength { get; set; } = 12;
6652

tests/ImageSharp.Web.Tests/ImageSharpTestServer.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public static class ImageSharpTestServer
4141
{
4242
options.Configuration = Configuration.Default;
4343
options.MemoryStreamManager = new RecyclableMemoryStreamManager();
44-
options.MaxBrowserCacheDays = -1;
45-
options.MaxCacheDays = -1;
44+
options.BrowserMaxAge = TimeSpan.FromDays(-1);
45+
options.CacheMaxAge = TimeSpan.FromDays(-1);
4646
options.CachedNameLength = 12;
4747
options.OnParseCommands = _ => { };
4848
options.OnBeforeSave = _ => { };
@@ -73,8 +73,8 @@ public static class ImageSharpTestServer
7373
{
7474
options.Configuration = Configuration.Default;
7575
options.MemoryStreamManager = new RecyclableMemoryStreamManager();
76-
options.MaxBrowserCacheDays = -1;
77-
options.MaxCacheDays = -1;
76+
options.BrowserMaxAge = TimeSpan.FromDays(-1);
77+
options.CacheMaxAge = TimeSpan.FromDays(-1);
7878
options.CachedNameLength = 12;
7979
options.OnParseCommands = _ => { };
8080
options.OnBeforeSave = _ => { };
@@ -111,8 +111,8 @@ public static TestServer CreateWithActionsNoCache(
111111
{
112112
options.Configuration = Configuration.Default;
113113
options.MemoryStreamManager = new RecyclableMemoryStreamManager();
114-
options.MaxBrowserCacheDays = -1;
115-
options.MaxCacheDays = -1;
114+
options.BrowserMaxAge = TimeSpan.FromDays(-1);
115+
options.CacheMaxAge = TimeSpan.FromDays(-1);
116116
options.OnParseCommands = onParseCommands;
117117
options.OnBeforeSave = onBeforeSave;
118118
options.OnProcessed = onProcessed;
@@ -132,8 +132,8 @@ public static TestServer CreateWithActionsCache(
132132
{
133133
options.Configuration = Configuration.Default;
134134
options.MemoryStreamManager = new RecyclableMemoryStreamManager();
135-
options.MaxBrowserCacheDays = 7;
136-
options.MaxCacheDays = 365;
135+
options.BrowserMaxAge = TimeSpan.FromDays(7);
136+
options.CacheMaxAge = TimeSpan.FromDays(365);
137137
options.OnParseCommands = onParseCommands;
138138
options.OnBeforeSave = onBeforeSave;
139139
options.OnProcessed = onProcessed;

0 commit comments

Comments
 (0)