diff --git a/src/Tools/Source/RunTests/Cache/LocalDataStorage.cs b/src/Tools/Source/RunTests/Cache/LocalDataStorage.cs index 2343f8c63dd2d52935439cf15cc9cac68dabefb1..a8e34a5c41b34a84e0791a26ca99dd5887efb44a 100644 --- a/src/Tools/Source/RunTests/Cache/LocalDataStorage.cs +++ b/src/Tools/Source/RunTests/Cache/LocalDataStorage.cs @@ -26,6 +26,7 @@ private enum StorageKind Content } + internal const int MaxStorageCount = 200; internal const string DirectoryName = "RunTestsStorage"; private readonly string _storagePath; @@ -129,5 +130,32 @@ private string Read(string checksum, StorageKind kind) var filePath = GetStoragePath(checksum, kind); return File.ReadAllText(filePath); } + + private void CleanupStorage() + { + try + { + var files = Directory.GetFiles(_storagePath); + if (files.Length < MaxStorageCount) + { + return; + } + + var clean = files.Length - (MaxStorageCount / 2); + var items = files + .Select(x => new DirectoryInfo(x)) + .OrderBy(x => x.CreationTimeUtc) + .Take(clean); + + foreach (var item in items) + { + FileUtil.DeleteDirectory(item.Name); + } + } + catch (Exception ex) + { + Console.WriteLine($"Unable to cleanup storage {ex.Message}"); + } + } } }