Skip to content

Commit 672bfe4

Browse files
committed
Fix missing cache lookups
1 parent b72064b commit 672bfe4

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/ImageSharp.Web/Middleware/ImageSharpMiddleware.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,8 @@ private static readonly ConcurrentTLruCache<string, ImageMetadata> SourceMetadat
5050
/// <summary>
5151
/// Used to temporarily store cache resolver reads to reduce the overhead of cache lookups.
5252
/// </summary>
53-
private static readonly ConcurrentTLruCache<string, IImageCacheResolver> CacheResolverLru
54-
= new ConcurrentTLruCache<string, IImageCacheResolver>(1024, TimeSpan.FromMinutes(5));
55-
56-
/// <summary>
57-
/// Used to temporarily store cache metadata reads to reduce the overhead of cache lookups.
58-
/// </summary>
59-
private static readonly ConcurrentTLruCache<string, ImageCacheMetadata> CacheMetadataLru
60-
= new ConcurrentTLruCache<string, ImageCacheMetadata>(1024, TimeSpan.FromMinutes(5));
53+
private static readonly ConcurrentTLruCache<string, (IImageCacheResolver, ImageCacheMetadata)> CacheResolverLru
54+
= new ConcurrentTLruCache<string, (IImageCacheResolver, ImageCacheMetadata)>(1024, TimeSpan.FromMinutes(5));
6155

6256
/// <summary>
6357
/// The function processing the Http request.
@@ -413,37 +407,40 @@ private async Task<ValueTuple<bool, ImageMetadata>> IsNewOrUpdatedAsync(
413407

414408
// Check to see if the cache contains this image.
415409
// If not, we return early. No further checks necessary.
416-
IImageCacheResolver cachedImageResolver = await
410+
(IImageCacheResolver, ImageCacheMetadata) cachedImageResolver = await
417411
CacheResolverLru.GetOrAddAsync(
418412
key,
419-
k => this.cache.GetAsync(k));
420-
421-
if (cachedImageResolver is null)
413+
async k =>
414+
{
415+
var resolver = await this.cache.GetAsync(k);
416+
ImageCacheMetadata metadata = default;
417+
if (resolver != null)
418+
{
419+
metadata = await resolver.GetMetaDataAsync();
420+
}
421+
return (resolver, metadata);
422+
});
423+
424+
if (cachedImageResolver.Item1 is null)
422425
{
423426
// Remove the null resolver from the store.
424427
CacheResolverLru.TryRemove(key);
425428
return (true, sourceImageMetadata);
426429
}
427430

428-
// Now resolve the cached image metadata storing the result.
429-
ImageCacheMetadata cachedImageMetadata = await
430-
CacheMetadataLru.GetOrAddAsync(
431-
key,
432-
_ => cachedImageResolver.GetMetaDataAsync());
433-
434431
// Has the cached image expired?
435432
// Or has the source image changed since the image was last cached?
436-
if (cachedImageMetadata.ContentLength == 0 // Fix for old cache without length property
437-
|| cachedImageMetadata.CacheLastWriteTimeUtc <= (DateTimeOffset.UtcNow - this.options.CacheMaxAge)
438-
|| cachedImageMetadata.SourceLastWriteTimeUtc != sourceImageMetadata.LastWriteTimeUtc)
433+
if (cachedImageResolver.Item2.ContentLength == 0 // Fix for old cache without length property
434+
|| cachedImageResolver.Item2.CacheLastWriteTimeUtc <= (DateTimeOffset.UtcNow - this.options.CacheMaxAge)
435+
|| cachedImageResolver.Item2.SourceLastWriteTimeUtc != sourceImageMetadata.LastWriteTimeUtc)
439436
{
440-
// We want to remove the metadata from the store so that the next check gets the updated file.
441-
CacheMetadataLru.TryRemove(key);
437+
// We want to remove the resolver from the store so that the next check gets the updated file.
438+
CacheResolverLru.TryRemove(key);
442439
return (true, sourceImageMetadata);
443440
}
444441

445442
// We're pulling the image from the cache.
446-
await this.SendResponseAsync(imageContext, key, cachedImageMetadata, null, cachedImageResolver);
443+
await this.SendResponseAsync(imageContext, key, cachedImageResolver.Item2, null, cachedImageResolver.Item1);
447444
return (false, sourceImageMetadata);
448445
}
449446
finally

0 commit comments

Comments
 (0)