Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 35 additions & 37 deletions src/ImageProcessor.Web/Caching/DiskCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,64 +211,62 @@ public override Task TrimCacheAsync()

this.ScheduleCacheTrimmer(token =>
{
string rootDirectory = Path.GetDirectoryName(this.CachedPath);

var rootDirectory = Path.GetDirectoryName(this.CachedPath);
if (rootDirectory != null)
{
// Jump up to the parent branch to clean through the cache.
// UNC folders can throw exceptions if the file doesn't exist.
IEnumerable<string> directories = SafeEnumerateDirectories(validatedAbsoluteCachePath).Reverse();

// Jump up to the parent branch to clean through the cache
// UNC folders can throw exceptions if the file doesn't exist
var directories = SafeEnumerateDirectories(validatedAbsoluteCachePath).Reverse().ToList();
foreach (string directory in directories)
{
if (!Directory.Exists(directory))
{
continue;
}

if (token.IsCancellationRequested)
{
break;
}

IEnumerable<FileInfo> files = Directory.EnumerateFiles(directory)
.Select(f => new FileInfo(f))
.OrderBy(f => f.CreationTimeUtc);
int count = files.Count();

foreach (FileInfo fileInfo in files)
try
{
if (token.IsCancellationRequested)
{
break;
}

try
var files = Directory.EnumerateFiles(directory).Select(f => new FileInfo(f)).OrderBy(f => f.CreationTimeUtc).ToList();
var count = files.Count;
foreach (var 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)
if (token.IsCancellationRequested)
{
break;
}

// Remove from the cache and delete each CachedImage.
CacheIndexer.Remove(fileInfo.Name);
fileInfo.Delete();
count--;
}
catch (Exception ex)
{
// Log it but skip to the next file.
ImageProcessorBootstrapper.Instance.Logger.Log<DiskCache>($"Unable to clean cached file: {fileInfo.FullName}, {ex.Message}");
try
{
// 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--;
}
catch (Exception ex)
{
// Log it but skip to the next file
ImageProcessorBootstrapper.Instance.Logger.Log<DiskCache>($"Unable to clean cached file: {fileInfo.FullName}, {ex.Message}");
}
}
}
catch (Exception ex)
{
// Log it but skip to the next directory
ImageProcessorBootstrapper.Instance.Logger.Log<DiskCache>($"Unable to clean cached directory: {directory}, {ex.Message}");
}

// If the directory is empty of files delete it to remove the FCN.
// If the directory is empty of files delete it to remove the FCN
this.RecursivelyDeleteEmptyDirectories(directory, validatedAbsoluteCachePath, token);
}
}

return Task.FromResult(0);
});

Expand Down