From 3fdde317bb846ceec32cf0578a14da2dd62edc10 Mon Sep 17 00:00:00 2001 From: James South Date: Wed, 15 Jan 2014 00:56:07 +0000 Subject: [PATCH] Swapping out for Semaphore Slim Former-commit-id: 5f2e7aaa36a9295a43c80466266be241ee567c90 --- .../HttpModules/ImageProcessingModule.cs | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs b/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs index 6cf7df761..edd48f88e 100644 --- a/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs +++ b/src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs @@ -55,9 +55,9 @@ namespace ImageProcessor.Web.HttpModules private static readonly string AssemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); /// - /// The collection of Semaphores for identifying given locking individual queries. + /// The collection of SemaphoreSlims for identifying given locking individual queries. /// - private static readonly Dictionary Semaphores = new Dictionary(); + private static readonly Dictionary SemaphoreSlims = new Dictionary(); /// /// A value indicating whether this instance of the given entity has been disposed. @@ -134,25 +134,25 @@ namespace ImageProcessor.Web.HttpModules } /// - /// Gets the specific for the given id. + /// Gets the specific for the given id. /// /// - /// The id representing the . + /// The id representing the . /// /// /// The for the given id. /// - private static Semaphore GetSemaphore(string id) + private static SemaphoreSlim GetSemaphoreSlim(string id) { id = id.ToMD5Fingerprint(); - if (Semaphores.ContainsKey(id)) + if (SemaphoreSlims.ContainsKey(id)) { - return Semaphores[id]; + return SemaphoreSlims[id]; } - Semaphore semaphore = new Semaphore(1, 1, id); - Semaphores.Add(id, semaphore); + SemaphoreSlim semaphore = new SemaphoreSlim(1, 1); + SemaphoreSlims.Add(id, semaphore); return semaphore; } @@ -170,12 +170,12 @@ namespace ImageProcessor.Web.HttpModules if (disposing) { // Dispose of any managed resources here. - foreach (KeyValuePair semaphore in Semaphores) + foreach (KeyValuePair semaphore in SemaphoreSlims) { semaphore.Value.Dispose(); } - Semaphores.Clear(); + SemaphoreSlims.Clear(); } // Call the appropriate methods to clean up @@ -382,20 +382,20 @@ namespace ImageProcessor.Web.HttpModules // Prevent response blocking. WebResponse webResponse = await remoteFile.GetWebResponseAsync().ConfigureAwait(false); - using (MemoryStream memoryStream = new MemoryStream()) + SemaphoreSlim semaphore = GetSemaphoreSlim(cachedPath); + try { - using (WebResponse response = webResponse) + semaphore.Wait(); + + using (MemoryStream memoryStream = new MemoryStream()) { - using (Stream responseStream = response.GetResponseStream()) + using (WebResponse response = webResponse) { - if (responseStream != null) + using (Stream responseStream = response.GetResponseStream()) { - responseStream.CopyTo(memoryStream); - - Semaphore semaphore = GetSemaphore(cachedPath); - try + if (responseStream != null) { - semaphore.WaitOne(); + responseStream.CopyTo(memoryStream); // Process the Image imageFactory.Load(memoryStream) @@ -403,7 +403,7 @@ namespace ImageProcessor.Web.HttpModules .Format(ImageUtils.GetImageFormat(imageName)) .AutoProcess() .Save(cachedPath); - + // Ensure that the LastWriteTime property of the source and cached file match. Tuple creationAndLastWriteDateTimes = await cache.SetCachedLastWriteTimeAsync(); @@ -413,14 +413,14 @@ namespace ImageProcessor.Web.HttpModules // Trim the cache. await cache.TrimCachedFolderAsync(cachedPath); } - finally - { - semaphore.Release(); - } } } } } + finally + { + semaphore.Release(); + } } else { @@ -433,10 +433,10 @@ namespace ImageProcessor.Web.HttpModules throw new HttpException(404, "No image exists at " + fullPath); } - Semaphore semaphore = GetSemaphore(cachedPath); + SemaphoreSlim semaphore = GetSemaphoreSlim(cachedPath); try { - semaphore.WaitOne(); + semaphore.Wait(); // Process the Image imageFactory.Load(fullPath).AutoProcess().Save(cachedPath);