From 1a6accd64f8a2a3b32bec9c3e4fad24d7ff0d61c Mon Sep 17 00:00:00 2001 From: James South Date: Mon, 16 Dec 2013 11:07:14 +0000 Subject: [PATCH] A little bit more thread safety and accuracy. Former-commit-id: 4ddd4844876e8609c82c40a465c3b4b291f57159 --- .../NET45/Caching/CacheIndexer.cs | 5 +++- .../NET45/Caching/DiskCache.cs | 3 +++ .../NET45/Caching/MemCache.cs | 25 +++++++++++++------ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/ImageProcessor.Web/NET45/Caching/CacheIndexer.cs b/src/ImageProcessor.Web/NET45/Caching/CacheIndexer.cs index b925c9044..40c089ffe 100644 --- a/src/ImageProcessor.Web/NET45/Caching/CacheIndexer.cs +++ b/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 /// 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), diff --git a/src/ImageProcessor.Web/NET45/Caching/DiskCache.cs b/src/ImageProcessor.Web/NET45/Caching/DiskCache.cs index fa164668b..bf516756b 100644 --- a/src/ImageProcessor.Web/NET45/Caching/DiskCache.cs +++ b/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) diff --git a/src/ImageProcessor.Web/NET45/Caching/MemCache.cs b/src/ImageProcessor.Web/NET45/Caching/MemCache.cs index bd7c471d0..0eb42a887 100644 --- a/src/ImageProcessor.Web/NET45/Caching/MemCache.cs +++ b/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 /// /// An internal list of cache keys to allow bulk removal. /// - private static readonly Dictionary CacheItems = new Dictionary(); + private static readonly ConcurrentDictionary CacheItems = new ConcurrentDictionary(); #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 tempDictionary = new Dictionary(); + ConcurrentDictionary tempDictionary = new ConcurrentDictionary(); foreach (KeyValuePair 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 cacheItem in tempDictionary) { - CacheItems.Remove(cacheItem.Key); + CacheItems.Keys.Remove(cacheItem.Key); } } }