Browse Source

Swapping out for Semaphore Slim

Former-commit-id: a96a9d15c690f0b8aecca059a589f3fff3c2384c
af/merge-core
James South 13 years ago
parent
commit
a9e2a80c7f
  1. 54
      src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs

54
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(); private static readonly string AssemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
/// <summary> /// <summary>
/// The collection of Semaphores for identifying given locking individual queries. /// The collection of SemaphoreSlims for identifying given locking individual queries.
/// </summary> /// </summary>
private static readonly Dictionary<string, Semaphore> Semaphores = new Dictionary<string, Semaphore>(); private static readonly Dictionary<string, SemaphoreSlim> SemaphoreSlims = new Dictionary<string, SemaphoreSlim>();
/// <summary> /// <summary>
/// A value indicating whether this instance of the given entity has been disposed. /// A value indicating whether this instance of the given entity has been disposed.
@ -134,25 +134,25 @@ namespace ImageProcessor.Web.HttpModules
} }
/// <summary> /// <summary>
/// Gets the specific <see cref="T:System.Threading.Semaphore"/> for the given id. /// Gets the specific <see cref="T:System.Threading.SemaphoreSlim"/> for the given id.
/// </summary> /// </summary>
/// <param name="id"> /// <param name="id">
/// The id representing the <see cref="T:System.Threading.Semaphore"/>. /// The id representing the <see cref="T:System.Threading.SemaphoreSlim"/>.
/// </param> /// </param>
/// <returns> /// <returns>
/// The <see cref="T:System.Threading.Mutex"/> for the given id. /// The <see cref="T:System.Threading.Mutex"/> for the given id.
/// </returns> /// </returns>
private static Semaphore GetSemaphore(string id) private static SemaphoreSlim GetSemaphoreSlim(string id)
{ {
id = id.ToMD5Fingerprint(); id = id.ToMD5Fingerprint();
if (Semaphores.ContainsKey(id)) if (SemaphoreSlims.ContainsKey(id))
{ {
return Semaphores[id]; return SemaphoreSlims[id];
} }
Semaphore semaphore = new Semaphore(1, 1, id); SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
Semaphores.Add(id, semaphore); SemaphoreSlims.Add(id, semaphore);
return semaphore; return semaphore;
} }
@ -170,12 +170,12 @@ namespace ImageProcessor.Web.HttpModules
if (disposing) if (disposing)
{ {
// Dispose of any managed resources here. // Dispose of any managed resources here.
foreach (KeyValuePair<string, Semaphore> semaphore in Semaphores) foreach (KeyValuePair<string, SemaphoreSlim> semaphore in SemaphoreSlims)
{ {
semaphore.Value.Dispose(); semaphore.Value.Dispose();
} }
Semaphores.Clear(); SemaphoreSlims.Clear();
} }
// Call the appropriate methods to clean up // Call the appropriate methods to clean up
@ -382,20 +382,20 @@ namespace ImageProcessor.Web.HttpModules
// Prevent response blocking. // Prevent response blocking.
WebResponse webResponse = await remoteFile.GetWebResponseAsync().ConfigureAwait(false); 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); if (responseStream != null)
Semaphore semaphore = GetSemaphore(cachedPath);
try
{ {
semaphore.WaitOne(); responseStream.CopyTo(memoryStream);
// Process the Image // Process the Image
imageFactory.Load(memoryStream) imageFactory.Load(memoryStream)
@ -403,7 +403,7 @@ namespace ImageProcessor.Web.HttpModules
.Format(ImageUtils.GetImageFormat(imageName)) .Format(ImageUtils.GetImageFormat(imageName))
.AutoProcess() .AutoProcess()
.Save(cachedPath); .Save(cachedPath);
// Ensure that the LastWriteTime property of the source and cached file match. // Ensure that the LastWriteTime property of the source and cached file match.
Tuple<DateTime, DateTime> creationAndLastWriteDateTimes = await cache.SetCachedLastWriteTimeAsync(); Tuple<DateTime, DateTime> creationAndLastWriteDateTimes = await cache.SetCachedLastWriteTimeAsync();
@ -413,14 +413,14 @@ namespace ImageProcessor.Web.HttpModules
// Trim the cache. // Trim the cache.
await cache.TrimCachedFolderAsync(cachedPath); await cache.TrimCachedFolderAsync(cachedPath);
} }
finally
{
semaphore.Release();
}
} }
} }
} }
} }
finally
{
semaphore.Release();
}
} }
else else
{ {
@ -433,10 +433,10 @@ namespace ImageProcessor.Web.HttpModules
throw new HttpException(404, "No image exists at " + fullPath); throw new HttpException(404, "No image exists at " + fullPath);
} }
Semaphore semaphore = GetSemaphore(cachedPath); SemaphoreSlim semaphore = GetSemaphoreSlim(cachedPath);
try try
{ {
semaphore.WaitOne(); semaphore.Wait();
// Process the Image // Process the Image
imageFactory.Load(fullPath).AutoProcess().Save(cachedPath); imageFactory.Load(fullPath).AutoProcess().Save(cachedPath);

Loading…
Cancel
Save