diff --git a/src/ImageProcessor.Web/NET45/Caching/DiskCache.cs b/src/ImageProcessor.Web/NET45/Caching/DiskCache.cs
index dbc1493a8..8e4cf7401 100644
--- a/src/ImageProcessor.Web/NET45/Caching/DiskCache.cs
+++ b/src/ImageProcessor.Web/NET45/Caching/DiskCache.cs
@@ -268,7 +268,7 @@ namespace ImageProcessor.Web.Caching
///
internal async Task TrimCachedFolderAsync(string path)
{
- await TaskHelpers.Run(() => this.TrimCachedFolder(path));
+ await TaskHelpers.Run(() => this.TrimCachedFolders(path));
}
#endregion
@@ -317,34 +317,40 @@ namespace ImageProcessor.Web.Caching
///
/// The path to the folder.
///
- private void TrimCachedFolder(string path)
+ private void TrimCachedFolders(string path)
{
// ReSharper disable once AssignNullToNotNullAttribute
DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName(path));
- IEnumerable files = directoryInfo.EnumerateFiles().OrderBy(f => f.CreationTimeUtc);
- int count = files.Count();
+ DirectoryInfo parentDirectoryInfo = directoryInfo.Parent;
- foreach (FileInfo fileInfo in files)
+ // ReSharper disable once PossibleNullReferenceException
+ foreach (DirectoryInfo enumerateDirectory in parentDirectoryInfo.EnumerateDirectories())
{
- try
+ IEnumerable files = enumerateDirectory.EnumerateFiles().OrderBy(f => f.CreationTimeUtc);
+ int count = files.Count();
+
+ foreach (FileInfo fileInfo in files)
{
- // If the group count is equal to the max count minus 1 then we know we
- // have reduced the number of items below the maximum allowed.
- // We'll cleanup any orphaned expired files though.
- if (!this.IsExpired(fileInfo.CreationTimeUtc) && count <= MaxFilesCount - 1)
+ try
{
- break;
- }
+ // If the group count is equal to the max count minus 1 then we know we
+ // have reduced the number of items below the maximum allowed.
+ // We'll cleanup any orphaned expired files though.
+ if (!this.IsExpired(fileInfo.CreationTimeUtc) && count <= MaxFilesCount - 1)
+ {
+ break;
+ }
- // Remove from the cache and delete each CachedImage.
- CacheIndexer.Remove(fileInfo.Name);
- fileInfo.Delete();
- count -= 1;
- }
- // ReSharper disable once EmptyGeneralCatchClause
- catch
- {
- // Do nothing; skip to the next file.
+ // Remove from the cache and delete each CachedImage.
+ CacheIndexer.Remove(fileInfo.Name);
+ fileInfo.Delete();
+ count -= 1;
+ }
+ // ReSharper disable once EmptyGeneralCatchClause
+ catch
+ {
+ // Do nothing; skip to the next file.
+ }
}
}
}