Browse Source

A little bit more thread safety and accuracy.

Former-commit-id: 208b32eadfdeaa74aa683f51f2641ea3bc7b0607
af/merge-core
James South 13 years ago
parent
commit
edddb6e0fd
  1. 5
      src/ImageProcessor.Web/NET45/Caching/CacheIndexer.cs
  2. 3
      src/ImageProcessor.Web/NET45/Caching/DiskCache.cs
  3. 25
      src/ImageProcessor.Web/NET45/Caching/MemCache.cs

5
src/ImageProcessor.Web/NET45/Caching/CacheIndexer.cs

@ -11,7 +11,6 @@
namespace ImageProcessor.Web.Caching namespace ImageProcessor.Web.Caching
{ {
#region Using #region Using
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Runtime.Caching; using System.Runtime.Caching;
@ -100,6 +99,7 @@ namespace ImageProcessor.Web.Caching
/// </returns> /// </returns>
private static CachedImage GetCachedImage(string cachePath) private static CachedImage GetCachedImage(string cachePath)
{ {
// FileInfo is thread safe.
FileInfo fileInfo = new FileInfo(cachePath); FileInfo fileInfo = new FileInfo(cachePath);
if (!fileInfo.Exists) if (!fileInfo.Exists)
@ -107,6 +107,9 @@ namespace ImageProcessor.Web.Caching
return null; return null;
} }
// Pull the latest info.
fileInfo.Refresh();
return new CachedImage return new CachedImage
{ {
Key = Path.GetFileNameWithoutExtension(cachePath), Key = Path.GetFileNameWithoutExtension(cachePath),

3
src/ImageProcessor.Web/NET45/Caching/DiskCache.cs

@ -201,6 +201,9 @@ namespace ImageProcessor.Web.Caching
if (imageFileInfo.Exists) if (imageFileInfo.Exists)
{ {
// Pull the latest info.
imageFileInfo.Refresh();
// Check to see if the last write time is different of whether the // Check to see if the last write time is different of whether the
// cached image is set to expire or if the max age is different. // cached image is set to expire or if the max age is different.
if (!this.RoughDateTimeCompare(imageFileInfo.LastWriteTimeUtc, cachedImage.LastWriteTimeUtc) if (!this.RoughDateTimeCompare(imageFileInfo.LastWriteTimeUtc, cachedImage.LastWriteTimeUtc)

25
src/ImageProcessor.Web/NET45/Caching/MemCache.cs

@ -12,6 +12,7 @@ namespace ImageProcessor.Web.Caching
{ {
#region Using #region Using
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.Caching; using System.Runtime.Caching;
#endregion #endregion
@ -30,7 +31,7 @@ namespace ImageProcessor.Web.Caching
/// <summary> /// <summary>
/// An internal list of cache keys to allow bulk removal. /// An internal list of cache keys to allow bulk removal.
/// </summary> /// </summary>
private static readonly Dictionary<string, string> CacheItems = new Dictionary<string, string>(); private static readonly ConcurrentDictionary<string, string> CacheItems = new ConcurrentDictionary<string, string>();
#endregion #endregion
#region Methods #region Methods
@ -69,11 +70,19 @@ namespace ImageProcessor.Web.Caching
policy = new CacheItemPolicy(); policy = new CacheItemPolicy();
} }
isAdded = Cache.Add(key, value, policy, regionName); try
{
Cache.Set(key, value, policy, regionName);
isAdded = true;
}
catch
{
isAdded = false;
}
if (isAdded) if (isAdded)
{ {
CacheItems.Add(key, regionName); CacheItems[key] = regionName;
} }
} }
@ -172,7 +181,7 @@ namespace ImageProcessor.Web.Caching
if (isRemoved) if (isRemoved)
{ {
CacheItems.Remove(key); CacheItems.Keys.Remove(key);
} }
} }
@ -196,7 +205,7 @@ namespace ImageProcessor.Web.Caching
{ {
// You can't remove items from a collection whilst you are iterating over it so you need to // You can't remove items from a collection whilst you are iterating over it so you need to
// create a collection to store the items to remove. // create a collection to store the items to remove.
Dictionary<string, string> tempDictionary = new Dictionary<string, string>(); ConcurrentDictionary<string, string> tempDictionary = new ConcurrentDictionary<string, string>();
foreach (KeyValuePair<string, string> cacheItem in CacheItems) foreach (KeyValuePair<string, string> cacheItem in CacheItems)
{ {
@ -207,7 +216,9 @@ namespace ImageProcessor.Web.Caching
if (isCleared) if (isCleared)
{ {
tempDictionary.Add(cacheItem.Key, cacheItem.Value); string key = cacheItem.Key;
string value = cacheItem.Value;
tempDictionary.AddOrUpdate(key, value, (oldkey, oldValue) => value);
} }
} }
} }
@ -217,7 +228,7 @@ namespace ImageProcessor.Web.Caching
// Loop through and clear out the dictionary of cache keys. // Loop through and clear out the dictionary of cache keys.
foreach (KeyValuePair<string, string> cacheItem in tempDictionary) foreach (KeyValuePair<string, string> cacheItem in tempDictionary)
{ {
CacheItems.Remove(cacheItem.Key); CacheItems.Keys.Remove(cacheItem.Key);
} }
} }
} }

Loading…
Cancel
Save