ImageProcessor.Web.Caching.DiskCache.TrimCacheAsync C# (CSharp) Method

TrimCacheAsync() public method

Trims the cache of any expired items in an asynchronous manner.
public TrimCacheAsync ( ) : System.Threading.Tasks.Task
return System.Threading.Tasks.Task
        public override async Task TrimCacheAsync()
        {
            string directory = Path.GetDirectoryName(this.CachedPath);

            if (directory != null)
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(directory);
                DirectoryInfo parentDirectoryInfo = directoryInfo.Parent;

                if (parentDirectoryInfo != null)
                {
                    // UNC folders can throw exceptions if the file doesn't exist.
                    foreach (DirectoryInfo enumerateDirectory in await parentDirectoryInfo.SafeEnumerateDirectoriesAsync())
                    {
                        IEnumerable<FileInfo> files = enumerateDirectory.EnumerateFiles().OrderBy(f => f.CreationTimeUtc);
                        int count = files.Count();

                        foreach (FileInfo fileInfo in files)
                        {
                            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 -= 1;
                            }
                            // ReSharper disable once EmptyGeneralCatchClause
                            catch
                            {
                                // Log it but skip to the next file.
                                ImageProcessorBootstrapper.Instance.Logger.Log<DiskCache>("Unable to clean cached file: " + fileInfo.FullName);
                            }
                        }
                    }
                }
            }
        }