Browse Source

A little bit more thread safety and accuracy.

Former-commit-id: 208b32eadfdeaa74aa683f51f2641ea3bc7b0607
pull/17/head
James South 12 years ago
parent
commit
aa69e12f9b
  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
{
#region Using
using System.Collections.Generic;
using System.IO;
using System.Runtime.Caching;
@ -100,6 +99,7 @@ namespace ImageProcessor.Web.Caching
/// </returns>
private static CachedImage GetCachedImage(string cachePath)
{
// FileInfo is thread safe.
FileInfo fileInfo = new FileInfo(cachePath);
if (!fileInfo.Exists)
@ -107,6 +107,9 @@ namespace ImageProcessor.Web.Caching
return null;
}
// Pull the latest info.
fileInfo.Refresh();
return new CachedImage
{
Key = Path.GetFileNameWithoutExtension(cachePath),

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

@ -201,6 +201,9 @@ namespace ImageProcessor.Web.Caching
if (imageFileInfo.Exists)
{
// Pull the latest info.
imageFileInfo.Refresh();
// 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.
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.Caching;
#endregion
@ -30,7 +31,7 @@ namespace ImageProcessor.Web.Caching
/// <summary>
/// An internal list of cache keys to allow bulk removal.
/// </summary>
private static readonly Dictionary<string, string> CacheItems = new Dictionary<string, string>();
private static readonly ConcurrentDictionary<string, string> CacheItems = new ConcurrentDictionary<string, string>();
#endregion
#region Methods
@ -69,11 +70,19 @@ namespace ImageProcessor.Web.Caching
policy = new CacheItemPolicy();
}
isAdded = Cache.Add(key, value, policy, regionName);
try
{
Cache.Set(key, value, policy, regionName);
isAdded = true;
}
catch
{
isAdded = false;
}
if (isAdded)
{
CacheItems.Add(key, regionName);
CacheItems[key] = regionName;
}
}
@ -172,7 +181,7 @@ namespace ImageProcessor.Web.Caching
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
// 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)
{
@ -207,7 +216,9 @@ namespace ImageProcessor.Web.Caching
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.
foreach (KeyValuePair<string, string> cacheItem in tempDictionary)
{
CacheItems.Remove(cacheItem.Key);
CacheItems.Keys.Remove(cacheItem.Key);
}
}
}

Loading…
Cancel
Save