Skip to content

Commit f6eb22b

Browse files
Add CacheFolderDepth option
1 parent ddb4653 commit f6eb22b

3 files changed

Lines changed: 21 additions & 30 deletions

File tree

samples/ImageSharp.Web.Sample/Startup.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public void ConfigureServices(IServiceCollection services)
5151
return new PhysicalFileSystemCache(
5252
provider.GetRequiredService<IOptions<PhysicalFileSystemCacheOptions>>(),
5353
provider.GetRequiredService<IWebHostEnvironment>(),
54-
provider.GetRequiredService<IOptions<ImageSharpMiddlewareOptions>>(),
5554
provider.GetRequiredService<FormatUtilities>());
5655
})
5756
.SetCacheKey<UriRelativeLowerInvariantCacheKey>()
@@ -127,7 +126,6 @@ private void ConfigureCustomServicesAndCustomOptions(IServiceCollection services
127126
return new PhysicalFileSystemCache(
128127
provider.GetRequiredService<IOptions<PhysicalFileSystemCacheOptions>>(),
129128
provider.GetRequiredService<IWebHostEnvironment>(),
130-
provider.GetRequiredService<IOptions<ImageSharpMiddlewareOptions>>(),
131129
provider.GetRequiredService<FormatUtilities>());
132130
})
133131
.SetCacheKey<UriRelativeLowerInvariantCacheKey>()

src/ImageSharp.Web/Caching/PhysicalFileSystemCache.cs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using Microsoft.AspNetCore.Hosting;
1010
using Microsoft.Extensions.FileProviders;
1111
using Microsoft.Extensions.Options;
12-
using SixLabors.ImageSharp.Web.Middleware;
1312
using SixLabors.ImageSharp.Web.Resolvers;
1413

1514
namespace SixLabors.ImageSharp.Web.Caching
@@ -34,16 +33,6 @@ public class PhysicalFileSystemCache : IImageCache
3433
/// </summary>
3534
private readonly IFileProvider fileProvider;
3635

37-
/// <summary>
38-
/// The cache configuration options.
39-
/// </summary>
40-
private readonly PhysicalFileSystemCacheOptions cacheOptions;
41-
42-
/// <summary>
43-
/// The middleware configuration options.
44-
/// </summary>
45-
private readonly ImageSharpMiddlewareOptions options;
46-
4736
/// <summary>
4837
/// Contains various format helper methods based on the current configuration.
4938
/// </summary>
@@ -52,35 +41,31 @@ public class PhysicalFileSystemCache : IImageCache
5241
/// <summary>
5342
/// Initializes a new instance of the <see cref="PhysicalFileSystemCache"/> class.
5443
/// </summary>
55-
/// <param name="cacheOptions">The cache configuration options.</param>
44+
/// <param name="options">The cache configuration options.</param>
5645
/// <param name="environment">The hosting environment the application is running in.</param>
57-
/// <param name="options">The middleware configuration options.</param>
5846
/// <param name="formatUtilities">Contains various format helper methods based on the current configuration.</param>
5947
public PhysicalFileSystemCache(
60-
IOptions<PhysicalFileSystemCacheOptions> cacheOptions,
48+
IOptions<PhysicalFileSystemCacheOptions> options,
6149
#if NETCOREAPP2_1
6250
IHostingEnvironment environment,
6351
#else
6452
IWebHostEnvironment environment,
6553
#endif
66-
IOptions<ImageSharpMiddlewareOptions> options,
6754
FormatUtilities formatUtilities)
6855
{
6956
Guard.NotNull(environment, nameof(environment));
7057
Guard.NotNull(options, nameof(options));
7158
Guard.NotNullOrWhiteSpace(environment.WebRootPath, nameof(environment.WebRootPath));
7259

7360
// Allow configuration of the cache without having to register everything.
74-
this.cacheOptions = cacheOptions != null ? cacheOptions.Value : new PhysicalFileSystemCacheOptions();
75-
this.cacheRootPath = GetCacheRoot(this.cacheOptions, environment.WebRootPath, environment.ContentRootPath);
76-
if (!Directory.Exists(this.cacheRootPath))
77-
{
78-
Directory.CreateDirectory(this.cacheRootPath);
79-
}
61+
PhysicalFileSystemCacheOptions cacheOptions = options != null ? options.Value : new PhysicalFileSystemCacheOptions();
62+
this.cacheRootPath = GetCacheRoot(cacheOptions, environment.WebRootPath, environment.ContentRootPath);
63+
this.cacheFolderDepth = (int)cacheOptions.CacheFolderDepth;
64+
65+
// Ensure cache directory is created before initializing the file provider
66+
Directory.CreateDirectory(this.cacheRootPath);
8067

8168
this.fileProvider = new PhysicalFileProvider(this.cacheRootPath);
82-
this.options = options.Value;
83-
this.cacheFolderDepth = (int)this.options.CacheHashLength;
8469
this.formatUtilities = formatUtilities;
8570
}
8671

@@ -124,10 +109,8 @@ public async Task SetAsync(string key, Stream stream, ImageCacheMetadata metadat
124109
string metaPath = this.ToMetaDataFilePath(path);
125110
string directory = Path.GetDirectoryName(path);
126111

127-
if (!Directory.Exists(directory))
128-
{
129-
Directory.CreateDirectory(directory);
130-
}
112+
// Ensure cache directory is created before creating files
113+
Directory.CreateDirectory(directory);
131114

132115
using (FileStream fileStream = File.Create(imagePath))
133116
{
@@ -167,6 +150,11 @@ private string ToImageFilePath(string path, in ImageCacheMetadata metaData)
167150
[MethodImpl(MethodImplOptions.AggressiveInlining)]
168151
internal static unsafe string ToFilePath(string key, int cacheFolderDepth)
169152
{
153+
if (cacheFolderDepth > key.Length)
154+
{
155+
cacheFolderDepth = key.Length;
156+
}
157+
170158
// Each key substring char + separator + key
171159
int length = (cacheFolderDepth * 2) + key.Length;
172160
fixed (char* keyPtr = key)

src/ImageSharp.Web/Caching/PhysicalFileSystemCacheOptions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace SixLabors.ImageSharp.Web.Caching
55
{
66
/// <summary>
7-
/// Configuration options for the <see cref="PhysicalFileSystemCache"/>.
7+
/// Configuration options for the <see cref="PhysicalFileSystemCache" />.
88
/// </summary>
99
public class PhysicalFileSystemCacheOptions
1010
{
@@ -13,6 +13,11 @@ public class PhysicalFileSystemCacheOptions
1313
/// </summary>
1414
public string CacheFolder { get; set; } = "is-cache";
1515

16+
/// <summary>
17+
/// Gets or sets the depth of the nested cache folders structure to store the images.
18+
/// </summary>
19+
public uint CacheFolderDepth { get; set; } = 8;
20+
1621
/// <summary>
1722
/// Gets or sets the optional cache root folder.
1823
/// <para>

0 commit comments

Comments
 (0)