Browse Source

Removing unnecessary async methods

Former-commit-id: 0391e03573f196b24788af269041fdd63da6f4e5
af/merge-core
James South 12 years ago
parent
commit
650f93cea7
  1. 55
      src/ImageProcessor.Web/Caching/CacheIndexer.cs
  2. 4
      src/ImageProcessor.Web/Caching/DiskCache.cs
  3. 2
      src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs

55
src/ImageProcessor.Web/Caching/CacheIndexer.cs

@ -10,13 +10,9 @@
namespace ImageProcessor.Web.Caching namespace ImageProcessor.Web.Caching
{ {
#region Using
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Runtime.Caching; using System.Runtime.Caching;
using System.Threading.Tasks;
using ImageProcessor.Web.Helpers;
#endregion
/// <summary> /// <summary>
/// Represents an in memory collection of keys and values whose operations are concurrent. /// Represents an in memory collection of keys and values whose operations are concurrent.
@ -34,19 +30,32 @@ namespace ImageProcessor.Web.Caching
/// The <see cref="CachedImage"/> matching the given key if the <see cref="CacheIndexer"/> contains an element with /// The <see cref="CachedImage"/> matching the given key if the <see cref="CacheIndexer"/> contains an element with
/// the specified key; otherwise, null. /// the specified key; otherwise, null.
/// </returns> /// </returns>
public static async Task<CachedImage> GetValueAsync(string cachedPath) public static CachedImage GetValue(string cachedPath)
{ {
string key = Path.GetFileNameWithoutExtension(cachedPath); string key = Path.GetFileNameWithoutExtension(cachedPath);
CachedImage cachedImage = (CachedImage)MemCache.GetItem(key); CachedImage cachedImage = (CachedImage)MemCache.GetItem(key);
if (cachedImage == null) if (cachedImage == null)
{ {
cachedImage = await Task.Run(() => GetCachedImage(cachedPath)); // FileInfo is thread safe.
FileInfo fileInfo = new FileInfo(cachedPath);
if (cachedImage != null) if (!fileInfo.Exists)
{ {
Add(cachedImage); return null;
} }
// Pull the latest info.
fileInfo.Refresh();
cachedImage = new CachedImage
{
Key = Path.GetFileNameWithoutExtension(cachedPath),
Path = cachedPath,
CreationTimeUtc = fileInfo.CreationTimeUtc
};
Add(cachedImage);
} }
return cachedImage; return cachedImage;
@ -87,35 +96,5 @@ namespace ImageProcessor.Web.Caching
return cachedImage; return cachedImage;
} }
#endregion #endregion
/// <summary>
/// Creates a new cached image from the cache instance on disk.
/// </summary>
/// <param name="cachePath">
/// The cache path.
/// </param>
/// <returns>
/// The <see cref="CachedImage"/> from the cache instance on disk.
/// </returns>
private static CachedImage GetCachedImage(string cachePath)
{
// FileInfo is thread safe.
FileInfo fileInfo = new FileInfo(cachePath);
if (!fileInfo.Exists)
{
return null;
}
// Pull the latest info.
fileInfo.Refresh();
return new CachedImage
{
Key = Path.GetFileNameWithoutExtension(cachePath),
Path = cachePath,
CreationTimeUtc = fileInfo.CreationTimeUtc
};
}
} }
} }

4
src/ImageProcessor.Web/Caching/DiskCache.cs

@ -159,10 +159,10 @@ namespace ImageProcessor.Web.Caching
/// <returns> /// <returns>
/// True if the the original file is new or has been updated; otherwise, false. /// True if the the original file is new or has been updated; otherwise, false.
/// </returns> /// </returns>
internal async Task<bool> IsNewOrUpdatedFileAsync(string cachedPath) internal bool IsNewOrUpdatedFile(string cachedPath)
{ {
bool isUpdated = false; bool isUpdated = false;
CachedImage cachedImage = await CacheIndexer.GetValueAsync(cachedPath); CachedImage cachedImage = CacheIndexer.GetValue(cachedPath);
if (cachedImage == null) if (cachedImage == null)
{ {

2
src/ImageProcessor.Web/HttpModules/ImageProcessingModule.cs

@ -386,7 +386,7 @@ namespace ImageProcessor.Web.HttpModules
if (isAllowed) if (isAllowed)
{ {
// Is the file new or updated? // Is the file new or updated?
bool isNewOrUpdated = await cache.IsNewOrUpdatedFileAsync(cachedPath); bool isNewOrUpdated = cache.IsNewOrUpdatedFile(cachedPath);
// Only process if the file has been updated. // Only process if the file has been updated.
if (isNewOrUpdated) if (isNewOrUpdated)

Loading…
Cancel
Save