Skip to content

Commit

Permalink
2.0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants committed Mar 21, 2013
1 parent 014c46f commit 3b4bc55
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 90 deletions.
3 changes: 3 additions & 0 deletions src/ImageProcessor.Web/Caching/PersistantDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace ImageProcessor.Web.Caching
#region Using
using System;
using System.Collections.Generic;

using ImageProcessor.Web.Helpers;

#endregion

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// </copyright>
// -----------------------------------------------------------------------

namespace ImageProcessor.Web.Caching
namespace ImageProcessor.Web.Helpers
{
#region Using
using System.Collections.Generic;
Expand Down
133 changes: 47 additions & 86 deletions src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ namespace ImageProcessor.Web.HttpModules
using ImageProcessor.Web.Caching;
using ImageProcessor.Web.Config;
using ImageProcessor.Web.Helpers;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using System.Threading;
#endregion

/// <summary>
Expand Down Expand Up @@ -51,15 +48,19 @@ public class ImageProcessingModule : IHttpModule
private static readonly string AssemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();

/// <summary>
/// The thread safe fifo queue.
/// A value indicating whether the application has started.
/// </summary>
private static ConcurrentQueue<Action> imageOperations;
private static bool hasModuleInitialized;
#endregion

/// <summary>
/// A value indicating whether the application has started.
/// The delegate void representing the ProcessImage method.
/// </summary>
private static bool hasAppStarted = false;
#endregion
/// <param name="context">
/// the <see cref="T:System.Web.HttpContext">HttpContext</see> object that provides
/// references to the intrinsic server objects
/// </param>
private delegate void ProcessImageDelegate(HttpContext context);

#region IHttpModule Members
/// <summary>
Expand All @@ -72,23 +73,20 @@ public class ImageProcessingModule : IHttpModule
/// </param>
public void Init(HttpApplication context)
{
if (!hasAppStarted)
if (!hasModuleInitialized)
{
lock (SyncRoot)
{
if (!hasAppStarted)
if (!hasModuleInitialized)
{
imageOperations = new ConcurrentQueue<Action>();
DiskCache.CreateCacheDirectories();
hasAppStarted = true;
hasModuleInitialized = true;
}
}
}

context.AddOnBeginRequestAsync(OnBeginAsync, OnEndAsync);
//context.BeginRequest += this.ContextBeginRequest;
context.AddOnBeginRequestAsync(this.OnBeginAsync, this.OnEndAsync);
context.PreSendRequestHeaders += this.ContextPreSendRequestHeaders;

}

/// <summary>
Expand All @@ -102,28 +100,30 @@ public void Dispose()

/// <summary>
/// The <see cref="T:System.Web.BeginEventHandler"/> that starts asynchronous processing
/// of the <see cref="T:System.Web.HttpApplication.BeginRequest"/>.
/// of the <see cref="System.Web.HttpApplication.BeginRequest"/>.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">
/// An <see cref="T:System.EventArgs">EventArgs</see> that contains
/// the event data.
/// </param>
/// <param name="cb">
/// <param name="callBack">
/// The delegate to call when the asynchronous method call is complete.
/// If cb is null, the delegate is not called.
/// If the callback is null, the delegate is not called.
/// </param>
/// <param name="extraData">
/// <param name="state">
/// Any additional data needed to process the request.
/// </param>
/// <returns></returns>
IAsyncResult OnBeginAsync(object sender, EventArgs e, AsyncCallback cb, object extraData)
/// <returns>
/// The status of the asynchronous operation.
/// </returns>
private IAsyncResult OnBeginAsync(object sender, EventArgs e, AsyncCallback callBack, object state)
{
HttpContext context = ((HttpApplication)sender).Context;
EnqueueDelegate enqueueDelegate = new EnqueueDelegate(Enqueue);

return enqueueDelegate.BeginInvoke(context, cb, extraData);
ProcessImageDelegate processImage = this.ProcessImage;

return processImage.BeginInvoke(context, callBack, state);
}

/// <summary>
Expand All @@ -133,53 +133,13 @@ IAsyncResult OnBeginAsync(object sender, EventArgs e, AsyncCallback cb, object e
/// The <see cref="T:System.IAsyncResult"/> that is the result of the
/// <see cref="T:System.Web.BeginEventHandler"/> operation.
/// </param>
public void OnEndAsync(IAsyncResult result)
private void OnEndAsync(IAsyncResult result)
{
// An action to consume the ConcurrentQueue.
Action action = () =>
// Ensure our ProcessImage has completed in the background.
while (!result.IsCompleted)
{
Action op;

while (imageOperations.TryDequeue(out op))
{
op();
}
};

// Start 4 concurrent consuming actions.
Parallel.Invoke(action, action, action, action);
}

/// <summary>
/// The delegate void representing the Enqueue method.
/// </summary>
/// <param name="context">
/// the <see cref="T:System.Web.HttpContext">HttpContext</see> object that provides
/// references to the intrinsic server objects
/// </param>
private delegate void EnqueueDelegate(HttpContext context);

/// <summary>
/// Adds the method to the queue.
/// </summary>
/// <param name="context">
/// the <see cref="T:System.Web.HttpContext">HttpContext</see> object that provides
/// references to the intrinsic server objects
/// </param>
private void Enqueue(HttpContext context)
{
imageOperations.Enqueue(() => ProcessImage(context));
}

/// <summary>
/// Occurs as the first event in the HTTP pipeline chain of execution when ASP.NET responds to a request.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">An <see cref="T:System.EventArgs">EventArgs</see> that contains the event data.</param>
private void ContextBeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
imageOperations.Enqueue(() => ProcessImage(context));
System.Threading.Thread.Sleep(1);
}
}

/// <summary>
Expand All @@ -197,6 +157,7 @@ private void ContextPreSendRequestHeaders(object sender, EventArgs e)
{
string responseType = (string)responseTypeObject;

// Set the headers
this.SetHeaders(context, responseType);

context.Items[CachedResponseTypeKey] = null;
Expand Down Expand Up @@ -270,21 +231,21 @@ private void ProcessImage(HttpContext context)
{
//lock (SyncRoot)
//{
// Trim the cache.
DiskCache.TrimCachedFolders();
// Trim the cache.
DiskCache.TrimCachedFolders();

responseStream.CopyTo(memoryStream);
responseStream.CopyTo(memoryStream);

imageFactory.Load(memoryStream)
.AddQueryString(queryString)
.Format(ImageUtils.GetImageFormat(imageName))
.AutoProcess().Save(cachedPath);
imageFactory.Load(memoryStream)
.AddQueryString(queryString)
.Format(ImageUtils.GetImageFormat(imageName))
.AutoProcess().Save(cachedPath);

// Ensure that the LastWriteTime property of the source and cached file match.
DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath, true);
// Ensure that the LastWriteTime property of the source and cached file match.
DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath, true);

// Add to the cache.
DiskCache.AddImageToCache(cachedPath, dateTime);
// Add to the cache.
DiskCache.AddImageToCache(cachedPath, dateTime);
//}
}
}
Expand All @@ -294,16 +255,16 @@ private void ProcessImage(HttpContext context)
{
//lock (SyncRoot)
//{
// Trim the cache.
DiskCache.TrimCachedFolders();
// Trim the cache.
DiskCache.TrimCachedFolders();

imageFactory.Load(fullPath).AutoProcess().Save(cachedPath);
imageFactory.Load(fullPath).AutoProcess().Save(cachedPath);

// Ensure that the LastWriteTime property of the source and cached file match.
DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath, false);
// Ensure that the LastWriteTime property of the source and cached file match.
DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath, false);

// Add to the cache.
DiskCache.AddImageToCache(cachedPath, dateTime);
// Add to the cache.
DiskCache.AddImageToCache(cachedPath, dateTime);
//}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ImageProcessor.Web/ImageProcessor.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<ItemGroup>
<Compile Include="Caching\CachedImage.cs" />
<Compile Include="Caching\DiskCache.cs" />
<Compile Include="Caching\LockedDictionary.cs" />
<Compile Include="Helpers\LockedDictionary.cs" />
<Compile Include="Caching\PersistantDictionary.cs" />
<Compile Include="Caching\SQLContext.cs" />
<Compile Include="Config\ImageCacheSection.cs" />
Expand Down
4 changes: 2 additions & 2 deletions src/ImageProcessor.Web/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyVersion("2.0.1.0")]
[assembly: AssemblyFileVersion("2.0.1.0")]
Binary file added src/Nuget/ImageProcessor.Web.2.0.1.0.nupkg
Binary file not shown.

0 comments on commit 3b4bc55

Please sign in to comment.