@@ -70,6 +70,11 @@ private static readonly ConcurrentTLruCache<string, ImageMetadata> SourceMetadat
7070 /// </summary>
7171 private readonly IImageWebProcessor [ ] processors ;
7272
73+ /// <summary>
74+ /// The cache key.
75+ /// </summary>
76+ private readonly ICacheKey cacheKey ;
77+
7378 /// <summary>
7479 /// The image cache.
7580 /// </summary>
@@ -115,6 +120,7 @@ private static readonly ConcurrentTLruCache<string, ImageMetadata> SourceMetadat
115120 /// <param name="resolvers">A collection of <see cref="IImageProvider"/> instances used to resolve images.</param>
116121 /// <param name="processors">A collection of <see cref="IImageWebProcessor"/> instances used to process images.</param>
117122 /// <param name="cache">An <see cref="IImageCache"/> instance used for caching images.</param>
123+ /// <param name="cacheKey">An <see cref="ICacheKey"/> instance used for creating cache keys.</param>
118124 /// <param name="cacheHash">An <see cref="ICacheHash"/>instance used for calculating cached file names.</param>
119125 /// <param name="commandParser">The command parser</param>
120126 /// <param name="formatUtilities">Contains various format helper methods based on the current configuration.</param>
@@ -127,6 +133,7 @@ public ImageSharpMiddleware(
127133 IEnumerable < IImageProvider > resolvers ,
128134 IEnumerable < IImageWebProcessor > processors ,
129135 IImageCache cache ,
136+ ICacheKey cacheKey ,
130137 ICacheHash cacheHash ,
131138 CommandParser commandParser ,
132139 FormatUtilities formatUtilities ,
@@ -139,6 +146,7 @@ public ImageSharpMiddleware(
139146 Guard . NotNull ( resolvers , nameof ( resolvers ) ) ;
140147 Guard . NotNull ( processors , nameof ( processors ) ) ;
141148 Guard . NotNull ( cache , nameof ( cache ) ) ;
149+ Guard . NotNull ( cacheKey , nameof ( cacheKey ) ) ;
142150 Guard . NotNull ( cacheHash , nameof ( cacheHash ) ) ;
143151 Guard . NotNull ( commandParser , nameof ( commandParser ) ) ;
144152 Guard . NotNull ( formatUtilities , nameof ( formatUtilities ) ) ;
@@ -150,6 +158,7 @@ public ImageSharpMiddleware(
150158 this . providers = resolvers as IImageProvider [ ] ?? resolvers . ToArray ( ) ;
151159 this . processors = processors as IImageWebProcessor [ ] ?? processors . ToArray ( ) ;
152160 this . cache = cache ;
161+ this . cacheKey = cacheKey ;
153162 this . cacheHash = cacheHash ;
154163 this . commandParser = commandParser ;
155164 this . parserCulture = this . options . UseInvariantParsingCulture
@@ -259,21 +268,21 @@ private async Task ProcessRequestAsync(
259268 ImageContext imageContext ,
260269 CommandCollection commands )
261270 {
262- // Create a cache key based on all the components of the requested url
263- string uri = GetUri ( context , commands ) ;
264- string key = this . cacheHash . Create ( uri , this . options . CachedNameLength ) ;
271+ // Create a cache key and hash
272+ string cacheKey = this . cacheKey . Create ( context , commands ) ;
273+ string cacheHash = this . cacheHash . Create ( cacheKey , this . options . CachedNameLength ) ;
265274
266275 // Check the cache, if present, not out of date and not requiring an update
267276 // we'll simply serve the file from there.
268277 ImageWorkerResult readResult = default ;
269- using ( await this . asyncKeyLock . ReaderLockAsync ( key ) )
278+ using ( await this . asyncKeyLock . ReaderLockAsync ( cacheHash ) )
270279 {
271- readResult = await this . IsNewOrUpdatedAsync ( sourceImageResolver , key ) ;
280+ readResult = await this . IsNewOrUpdatedAsync ( sourceImageResolver , cacheHash ) ;
272281 }
273282
274283 if ( ! readResult . IsNewOrUpdated )
275284 {
276- await this . SendResponseAsync ( imageContext , key , readResult . CacheImageMetadata , readResult . Resolver , null ) ;
285+ await this . SendResponseAsync ( imageContext , cacheHash , readResult . CacheImageMetadata , readResult . Resolver , null ) ;
277286 return ;
278287 }
279288
@@ -285,7 +294,7 @@ private async Task ProcessRequestAsync(
285294 RecyclableMemoryStream outStream = null ;
286295 try
287296 {
288- Task < IDisposable > takeLockTask = this . asyncKeyLock . WriterLockAsync ( key ) ;
297+ Task < IDisposable > takeLockTask = this . asyncKeyLock . WriterLockAsync ( cacheHash ) ;
289298 bool lockWasAlreadyHeld = takeLockTask . Status != TaskStatus . RanToCompletion ;
290299 using ( await takeLockTask )
291300 {
@@ -294,7 +303,7 @@ private async Task ProcessRequestAsync(
294303 // the cache one more time
295304 if ( lockWasAlreadyHeld )
296305 {
297- readResult = await this . IsNewOrUpdatedAsync ( sourceImageResolver , key ) ;
306+ readResult = await this . IsNewOrUpdatedAsync ( sourceImageResolver , cacheHash ) ;
298307 }
299308
300309 if ( readResult . IsNewOrUpdated )
@@ -356,12 +365,12 @@ private async Task ProcessRequestAsync(
356365 outStream . Length ) ;
357366
358367 // Save the image to the cache and send the response to the caller.
359- await this . cache . SetAsync ( key , outStream , cachedImageMetadata ) ;
368+ await this . cache . SetAsync ( cacheHash , outStream , cachedImageMetadata ) ;
360369 outStream . Position = 0 ;
361370
362371 // Remove any resolver from the cache so we always resolve next request
363372 // for the same key.
364- CacheResolverLru . TryRemove ( key ) ;
373+ CacheResolverLru . TryRemove ( cacheHash ) ;
365374
366375 readResult = new ImageWorkerResult ( cachedImageMetadata , null ) ;
367376 }
@@ -375,7 +384,7 @@ private async Task ProcessRequestAsync(
375384 }
376385 }
377386
378- await this . SendResponseAsync ( imageContext , key , readResult . CacheImageMetadata , readResult . Resolver , outStream ) ;
387+ await this . SendResponseAsync ( imageContext , cacheHash , readResult . CacheImageMetadata , readResult . Resolver , outStream ) ;
379388 }
380389 finally
381390 {
@@ -507,26 +516,5 @@ private async Task SendResponseAsync(
507516 throw exception ;
508517 }
509518 }
510-
511- private static string GetUri ( HttpContext context , CommandCollection commands )
512- {
513- var sb = new StringBuilder ( context . Request . Host . ToString ( ) ) ;
514-
515- string pathBase = context . Request . PathBase . ToString ( ) ;
516- if ( ! string . IsNullOrWhiteSpace ( pathBase ) )
517- {
518- sb . AppendFormat ( "{0}/" , pathBase ) ;
519- }
520-
521- string path = context . Request . Path . ToString ( ) ;
522- if ( ! string . IsNullOrWhiteSpace ( path ) )
523- {
524- sb . Append ( path ) ;
525- }
526-
527- sb . Append ( QueryString . Create ( commands ) ) ;
528-
529- return sb . ToString ( ) . ToLowerInvariant ( ) ;
530- }
531519 }
532520}
0 commit comments