Skip to content

Commit 3b4bc55

Browse files
2.0.1.0
1 parent 014c46f commit 3b4bc55

File tree

6 files changed

+54
-90
lines changed

6 files changed

+54
-90
lines changed

src/ImageProcessor.Web/Caching/PersistantDictionary.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ namespace ImageProcessor.Web.Caching
1010
#region Using
1111
using System;
1212
using System.Collections.Generic;
13+
14+
using ImageProcessor.Web.Helpers;
15+
1316
#endregion
1417

1518
/// <summary>

src/ImageProcessor.Web/Caching/LockedDictionary.cs renamed to src/ImageProcessor.Web/Helpers/LockedDictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// </copyright>
66
// -----------------------------------------------------------------------
77

8-
namespace ImageProcessor.Web.Caching
8+
namespace ImageProcessor.Web.Helpers
99
{
1010
#region Using
1111
using System.Collections.Generic;

src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs

Lines changed: 47 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ namespace ImageProcessor.Web.HttpModules
1919
using ImageProcessor.Web.Caching;
2020
using ImageProcessor.Web.Config;
2121
using ImageProcessor.Web.Helpers;
22-
using System.Collections.Concurrent;
23-
using System.Threading.Tasks;
24-
using System.Threading;
2522
#endregion
2623

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

5350
/// <summary>
54-
/// The thread safe fifo queue.
51+
/// A value indicating whether the application has started.
5552
/// </summary>
56-
private static ConcurrentQueue<Action> imageOperations;
53+
private static bool hasModuleInitialized;
54+
#endregion
5755

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

6465
#region IHttpModule Members
6566
/// <summary>
@@ -72,23 +73,20 @@ public class ImageProcessingModule : IHttpModule
7273
/// </param>
7374
public void Init(HttpApplication context)
7475
{
75-
if (!hasAppStarted)
76+
if (!hasModuleInitialized)
7677
{
7778
lock (SyncRoot)
7879
{
79-
if (!hasAppStarted)
80+
if (!hasModuleInitialized)
8081
{
81-
imageOperations = new ConcurrentQueue<Action>();
8282
DiskCache.CreateCacheDirectories();
83-
hasAppStarted = true;
83+
hasModuleInitialized = true;
8484
}
8585
}
8686
}
8787

88-
context.AddOnBeginRequestAsync(OnBeginAsync, OnEndAsync);
89-
//context.BeginRequest += this.ContextBeginRequest;
88+
context.AddOnBeginRequestAsync(this.OnBeginAsync, this.OnEndAsync);
9089
context.PreSendRequestHeaders += this.ContextPreSendRequestHeaders;
91-
9290
}
9391

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

103101
/// <summary>
104102
/// The <see cref="T:System.Web.BeginEventHandler"/> that starts asynchronous processing
105-
/// of the <see cref="T:System.Web.HttpApplication.BeginRequest"/>.
103+
/// of the <see cref="System.Web.HttpApplication.BeginRequest"/>.
106104
/// </summary>
107105
/// <param name="sender">The source of the event.</param>
108106
/// <param name="e">
109107
/// An <see cref="T:System.EventArgs">EventArgs</see> that contains
110108
/// the event data.
111109
/// </param>
112-
/// <param name="cb">
110+
/// <param name="callBack">
113111
/// The delegate to call when the asynchronous method call is complete.
114-
/// If cb is null, the delegate is not called.
112+
/// If the callback is null, the delegate is not called.
115113
/// </param>
116-
/// <param name="extraData">
114+
/// <param name="state">
117115
/// Any additional data needed to process the request.
118116
/// </param>
119-
/// <returns></returns>
120-
IAsyncResult OnBeginAsync(object sender, EventArgs e, AsyncCallback cb, object extraData)
117+
/// <returns>
118+
/// The status of the asynchronous operation.
119+
/// </returns>
120+
private IAsyncResult OnBeginAsync(object sender, EventArgs e, AsyncCallback callBack, object state)
121121
{
122122
HttpContext context = ((HttpApplication)sender).Context;
123-
EnqueueDelegate enqueueDelegate = new EnqueueDelegate(Enqueue);
124123

125-
return enqueueDelegate.BeginInvoke(context, cb, extraData);
124+
ProcessImageDelegate processImage = this.ProcessImage;
126125

126+
return processImage.BeginInvoke(context, callBack, state);
127127
}
128128

129129
/// <summary>
@@ -133,53 +133,13 @@ IAsyncResult OnBeginAsync(object sender, EventArgs e, AsyncCallback cb, object e
133133
/// The <see cref="T:System.IAsyncResult"/> that is the result of the
134134
/// <see cref="T:System.Web.BeginEventHandler"/> operation.
135135
/// </param>
136-
public void OnEndAsync(IAsyncResult result)
136+
private void OnEndAsync(IAsyncResult result)
137137
{
138-
// An action to consume the ConcurrentQueue.
139-
Action action = () =>
138+
// Ensure our ProcessImage has completed in the background.
139+
while (!result.IsCompleted)
140140
{
141-
Action op;
142-
143-
while (imageOperations.TryDequeue(out op))
144-
{
145-
op();
146-
}
147-
};
148-
149-
// Start 4 concurrent consuming actions.
150-
Parallel.Invoke(action, action, action, action);
151-
}
152-
153-
/// <summary>
154-
/// The delegate void representing the Enqueue method.
155-
/// </summary>
156-
/// <param name="context">
157-
/// the <see cref="T:System.Web.HttpContext">HttpContext</see> object that provides
158-
/// references to the intrinsic server objects
159-
/// </param>
160-
private delegate void EnqueueDelegate(HttpContext context);
161-
162-
/// <summary>
163-
/// Adds the method to the queue.
164-
/// </summary>
165-
/// <param name="context">
166-
/// the <see cref="T:System.Web.HttpContext">HttpContext</see> object that provides
167-
/// references to the intrinsic server objects
168-
/// </param>
169-
private void Enqueue(HttpContext context)
170-
{
171-
imageOperations.Enqueue(() => ProcessImage(context));
172-
}
173-
174-
/// <summary>
175-
/// Occurs as the first event in the HTTP pipeline chain of execution when ASP.NET responds to a request.
176-
/// </summary>
177-
/// <param name="sender">The source of the event.</param>
178-
/// <param name="e">An <see cref="T:System.EventArgs">EventArgs</see> that contains the event data.</param>
179-
private void ContextBeginRequest(object sender, EventArgs e)
180-
{
181-
HttpContext context = ((HttpApplication)sender).Context;
182-
imageOperations.Enqueue(() => ProcessImage(context));
141+
System.Threading.Thread.Sleep(1);
142+
}
183143
}
184144

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

160+
// Set the headers
200161
this.SetHeaders(context, responseType);
201162

202163
context.Items[CachedResponseTypeKey] = null;
@@ -270,21 +231,21 @@ private void ProcessImage(HttpContext context)
270231
{
271232
//lock (SyncRoot)
272233
//{
273-
// Trim the cache.
274-
DiskCache.TrimCachedFolders();
234+
// Trim the cache.
235+
DiskCache.TrimCachedFolders();
275236

276-
responseStream.CopyTo(memoryStream);
237+
responseStream.CopyTo(memoryStream);
277238

278-
imageFactory.Load(memoryStream)
279-
.AddQueryString(queryString)
280-
.Format(ImageUtils.GetImageFormat(imageName))
281-
.AutoProcess().Save(cachedPath);
239+
imageFactory.Load(memoryStream)
240+
.AddQueryString(queryString)
241+
.Format(ImageUtils.GetImageFormat(imageName))
242+
.AutoProcess().Save(cachedPath);
282243

283-
// Ensure that the LastWriteTime property of the source and cached file match.
284-
DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath, true);
244+
// Ensure that the LastWriteTime property of the source and cached file match.
245+
DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath, true);
285246

286-
// Add to the cache.
287-
DiskCache.AddImageToCache(cachedPath, dateTime);
247+
// Add to the cache.
248+
DiskCache.AddImageToCache(cachedPath, dateTime);
288249
//}
289250
}
290251
}
@@ -294,16 +255,16 @@ private void ProcessImage(HttpContext context)
294255
{
295256
//lock (SyncRoot)
296257
//{
297-
// Trim the cache.
298-
DiskCache.TrimCachedFolders();
258+
// Trim the cache.
259+
DiskCache.TrimCachedFolders();
299260

300-
imageFactory.Load(fullPath).AutoProcess().Save(cachedPath);
261+
imageFactory.Load(fullPath).AutoProcess().Save(cachedPath);
301262

302-
// Ensure that the LastWriteTime property of the source and cached file match.
303-
DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath, false);
263+
// Ensure that the LastWriteTime property of the source and cached file match.
264+
DateTime dateTime = DiskCache.SetCachedLastWriteTime(path, cachedPath, false);
304265

305-
// Add to the cache.
306-
DiskCache.AddImageToCache(cachedPath, dateTime);
266+
// Add to the cache.
267+
DiskCache.AddImageToCache(cachedPath, dateTime);
307268
//}
308269
}
309270
}

src/ImageProcessor.Web/ImageProcessor.Web.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
<ItemGroup>
8989
<Compile Include="Caching\CachedImage.cs" />
9090
<Compile Include="Caching\DiskCache.cs" />
91-
<Compile Include="Caching\LockedDictionary.cs" />
91+
<Compile Include="Helpers\LockedDictionary.cs" />
9292
<Compile Include="Caching\PersistantDictionary.cs" />
9393
<Compile Include="Caching\SQLContext.cs" />
9494
<Compile Include="Config\ImageCacheSection.cs" />

src/ImageProcessor.Web/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
//
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
34-
[assembly: AssemblyVersion("2.0.0.0")]
35-
[assembly: AssemblyFileVersion("2.0.0.0")]
34+
[assembly: AssemblyVersion("2.0.1.0")]
35+
[assembly: AssemblyFileVersion("2.0.1.0")]
20 KB
Binary file not shown.

0 commit comments

Comments
 (0)