Skip to content

Commit c46f56d

Browse files
Add CacheKeyCaseSensitive option
1 parent d2db58d commit c46f56d

6 files changed

Lines changed: 20 additions & 26 deletions

File tree

src/ImageSharp.Web/Caching/CacheKey.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Web.Caching
1313
public class CacheKey : ICacheKey
1414
{
1515
/// <inheritdoc/>
16-
public string Create(HttpContext context, CommandCollection commands)
16+
public string Create(HttpContext context, CommandCollection commands, bool caseSensitive)
1717
{
1818
var sb = new StringBuilder(context.Request.Host.ToString());
1919

@@ -31,7 +31,7 @@ public string Create(HttpContext context, CommandCollection commands)
3131

3232
sb.Append(QueryString.Create(commands));
3333

34-
return sb.ToString().ToLowerInvariant();
34+
return caseSensitive ? sb.ToString() : sb.ToString().ToLowerInvariant();
3535
}
3636
}
3737
}

src/ImageSharp.Web/Caching/ICacheKey.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ public interface ICacheKey
1616
/// </summary>
1717
/// <param name="context">The HTTP context.</param>
1818
/// <param name="commands">The commands.</param>
19+
/// <param name="caseSensitive">If set to <c>true</c> the cache key should be generated in a case sensitive manner.</param>
1920
/// <returns>
2021
/// The cache key.
2122
/// </returns>
22-
string Create(HttpContext context, CommandCollection commands);
23+
string Create(HttpContext context, CommandCollection commands, bool caseSensitive);
2324
}
2425
}

src/ImageSharp.Web/Caching/UriAbsoluteCacheKey.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,11 @@ namespace SixLabors.ImageSharp.Web.Caching
1313
public class UriAbsoluteCacheKey : ICacheKey
1414
{
1515
/// <inheritdoc/>
16-
public virtual string Create(HttpContext context, CommandCollection commands)
17-
=> UriHelper.BuildAbsolute(context.Request.Scheme, context.Request.Host, context.Request.PathBase, context.Request.Path, QueryString.Create(commands));
18-
}
16+
public string Create(HttpContext context, CommandCollection commands, bool caseSensitive)
17+
{
18+
string cacheKey = UriHelper.BuildAbsolute(context.Request.Scheme, context.Request.Host, context.Request.PathBase, context.Request.Path, QueryString.Create(commands));
1919

20-
/// <summary>
21-
/// Creates a cache key based on the lowercased request scheme, host, path and commands.
22-
/// </summary>
23-
public class UriAbsoluteLowercaseCacheKey : UriAbsoluteCacheKey
24-
{
25-
/// <inheritdoc/>
26-
public override string Create(HttpContext context, CommandCollection commands)
27-
=> base.Create(context, commands).ToLowerInvariant();
20+
return caseSensitive ? cacheKey : cacheKey.ToLowerInvariant();
21+
}
2822
}
2923
}

src/ImageSharp.Web/Caching/UriRelativeCacheKey.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,11 @@ namespace SixLabors.ImageSharp.Web.Caching
1313
public class UriRelativeCacheKey : ICacheKey
1414
{
1515
/// <inheritdoc/>
16-
public virtual string Create(HttpContext context, CommandCollection commands)
17-
=> UriHelper.BuildRelative(context.Request.PathBase, context.Request.Path, QueryString.Create(commands));
18-
}
16+
public string Create(HttpContext context, CommandCollection commands, bool caseSensitive)
17+
{
18+
string cacheKey = UriHelper.BuildRelative(context.Request.PathBase, context.Request.Path, QueryString.Create(commands));
1919

20-
/// <summary>
21-
/// Creates a cache key based on the lowercased request path and commands.
22-
/// </summary>
23-
public class UriRelativeLowercaseCacheKey : UriRelativeCacheKey
24-
{
25-
/// <inheritdoc/>
26-
public override string Create(HttpContext context, CommandCollection commands)
27-
=> base.Create(context, commands).ToLowerInvariant();
20+
return caseSensitive ? cacheKey : cacheKey.ToLowerInvariant();
21+
}
2822
}
2923
}

src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ private async Task ProcessRequestAsync(
269269
CommandCollection commands)
270270
{
271271
// Create a cache key and hash
272-
string cacheKey = this.cacheKey.Create(context, commands);
272+
string cacheKey = this.cacheKey.Create(context, commands, this.options.CacheKeyCaseSensitive);
273273
string cacheHash = this.cacheHash.Create(cacheKey, this.options.CachedNameLength);
274274

275275
// Check the cache, if present, not out of date and not requiring an update

src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public class ImageSharpMiddlewareOptions
8484
/// </summary>
8585
public TimeSpan CacheMaxAge { get; set; } = TimeSpan.FromDays(365);
8686

87+
/// <summary>
88+
/// Gets or sets a value indicating whether the cache key should be case sensitive.
89+
/// </summary>
90+
public bool CacheKeyCaseSensitive { get; set; } = false;
91+
8792
/// <summary>
8893
/// Gets or sets the length of the filename to use (minus the extension) when storing
8994
/// images in the image cache. Defaults to 12 characters.

0 commit comments

Comments
 (0)