Browse Source

Cleanup now searches from parent directory

Former-commit-id: 7f08acb1b3a8cb53fcb1501d00703c73d061cfa3
af/merge-core
James South 12 years ago
parent
commit
84607a8533
  1. 48
      src/ImageProcessor.Web/NET45/Caching/DiskCache.cs

48
src/ImageProcessor.Web/NET45/Caching/DiskCache.cs

@ -268,7 +268,7 @@ namespace ImageProcessor.Web.Caching
/// </returns> /// </returns>
internal async Task TrimCachedFolderAsync(string path) internal async Task TrimCachedFolderAsync(string path)
{ {
await TaskHelpers.Run(() => this.TrimCachedFolder(path)); await TaskHelpers.Run(() => this.TrimCachedFolders(path));
} }
#endregion #endregion
@ -317,34 +317,40 @@ namespace ImageProcessor.Web.Caching
/// <param name="path"> /// <param name="path">
/// The path to the folder. /// The path to the folder.
/// </param> /// </param>
private void TrimCachedFolder(string path) private void TrimCachedFolders(string path)
{ {
// ReSharper disable once AssignNullToNotNullAttribute // ReSharper disable once AssignNullToNotNullAttribute
DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName(path)); DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName(path));
IEnumerable<FileInfo> files = directoryInfo.EnumerateFiles().OrderBy(f => f.CreationTimeUtc); DirectoryInfo parentDirectoryInfo = directoryInfo.Parent;
int count = files.Count();
foreach (FileInfo fileInfo in files) // ReSharper disable once PossibleNullReferenceException
foreach (DirectoryInfo enumerateDirectory in parentDirectoryInfo.EnumerateDirectories())
{ {
try IEnumerable<FileInfo> 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 try
// 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; // 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. // Remove from the cache and delete each CachedImage.
CacheIndexer.Remove(fileInfo.Name); CacheIndexer.Remove(fileInfo.Name);
fileInfo.Delete(); fileInfo.Delete();
count -= 1; count -= 1;
} }
// ReSharper disable once EmptyGeneralCatchClause // ReSharper disable once EmptyGeneralCatchClause
catch catch
{ {
// Do nothing; skip to the next file. // Do nothing; skip to the next file.
}
} }
} }
} }

Loading…
Cancel
Save