@@ -211,64 +211,62 @@ public override Task TrimCacheAsync()
211
211
212
212
this . ScheduleCacheTrimmer ( token =>
213
213
{
214
- string rootDirectory = Path . GetDirectoryName ( this . CachedPath ) ;
215
-
214
+ var rootDirectory = Path . GetDirectoryName ( this . CachedPath ) ;
216
215
if ( rootDirectory != null )
217
216
{
218
- // Jump up to the parent branch to clean through the cache.
219
- // UNC folders can throw exceptions if the file doesn't exist.
220
- IEnumerable < string > directories = SafeEnumerateDirectories ( validatedAbsoluteCachePath ) . Reverse ( ) ;
221
-
217
+ // Jump up to the parent branch to clean through the cache
218
+ // UNC folders can throw exceptions if the file doesn't exist
219
+ var directories = SafeEnumerateDirectories ( validatedAbsoluteCachePath ) . Reverse ( ) . ToList ( ) ;
222
220
foreach ( string directory in directories )
223
221
{
224
- if ( ! Directory . Exists ( directory ) )
225
- {
226
- continue ;
227
- }
228
-
229
222
if ( token . IsCancellationRequested )
230
223
{
231
224
break ;
232
225
}
233
226
234
- IEnumerable < FileInfo > files = Directory . EnumerateFiles ( directory )
235
- . Select ( f => new FileInfo ( f ) )
236
- . OrderBy ( f => f . CreationTimeUtc ) ;
237
- int count = files . Count ( ) ;
238
-
239
- foreach ( FileInfo fileInfo in files )
227
+ try
240
228
{
241
- if ( token . IsCancellationRequested )
242
- {
243
- break ;
244
- }
245
-
246
- try
229
+ var files = Directory . EnumerateFiles ( directory ) . Select ( f => new FileInfo ( f ) ) . OrderBy ( f => f . CreationTimeUtc ) . ToList ( ) ;
230
+ var count = files . Count ;
231
+ foreach ( var fileInfo in files )
247
232
{
248
- // If the group count is equal to the max count minus 1 then we know we
249
- // have reduced the number of items below the maximum allowed.
250
- // We'll cleanup any orphaned expired files though.
251
- if ( ! this . IsExpired ( fileInfo . CreationTimeUtc ) && count <= MaxFilesCount - 1 )
233
+ if ( token . IsCancellationRequested )
252
234
{
253
235
break ;
254
236
}
255
237
256
- // Remove from the cache and delete each CachedImage.
257
- CacheIndexer . Remove ( fileInfo . Name ) ;
258
- fileInfo . Delete ( ) ;
259
- count -- ;
260
- }
261
- catch ( Exception ex )
262
- {
263
- // Log it but skip to the next file.
264
- ImageProcessorBootstrapper . Instance . Logger . Log < DiskCache > ( $ "Unable to clean cached file: { fileInfo . FullName } , { ex . Message } ") ;
238
+ try
239
+ {
240
+ // 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
241
+ // We'll cleanup any orphaned expired files though
242
+ if ( ! this . IsExpired ( fileInfo . CreationTimeUtc ) && count <= MaxFilesCount - 1 )
243
+ {
244
+ break ;
245
+ }
246
+
247
+ // Remove from the cache and delete each CachedImage
248
+ CacheIndexer . Remove ( fileInfo . Name ) ;
249
+ fileInfo . Delete ( ) ;
250
+ count -- ;
251
+ }
252
+ catch ( Exception ex )
253
+ {
254
+ // Log it but skip to the next file
255
+ ImageProcessorBootstrapper . Instance . Logger . Log < DiskCache > ( $ "Unable to clean cached file: { fileInfo . FullName } , { ex . Message } ") ;
256
+ }
265
257
}
266
258
}
259
+ catch ( Exception ex )
260
+ {
261
+ // Log it but skip to the next directory
262
+ ImageProcessorBootstrapper . Instance . Logger . Log < DiskCache > ( $ "Unable to clean cached directory: { directory } , { ex . Message } ") ;
263
+ }
267
264
268
- // If the directory is empty of files delete it to remove the FCN.
265
+ // If the directory is empty of files delete it to remove the FCN
269
266
this . RecursivelyDeleteEmptyDirectories ( directory , validatedAbsoluteCachePath , token ) ;
270
267
}
271
268
}
269
+
272
270
return Task . FromResult ( 0 ) ;
273
271
} ) ;
274
272
0 commit comments