From b2be561e3f37364406e2eb6d00f3f9ec9b97285c Mon Sep 17 00:00:00 2001 From: Renaud Paquay Date: Wed, 28 Jul 2010 13:06:27 -0700 Subject: [PATCH] Use ConcurrentDictionary to simplify code --HG-- branch : dev --- src/Orchard/Caching/DefaultCacheHolder.cs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/Orchard/Caching/DefaultCacheHolder.cs b/src/Orchard/Caching/DefaultCacheHolder.cs index 57b0ec140..972882b1a 100644 --- a/src/Orchard/Caching/DefaultCacheHolder.cs +++ b/src/Orchard/Caching/DefaultCacheHolder.cs @@ -1,27 +1,20 @@ using System; -using System.Collections.Generic; -using Castle.Core; +using System.Collections.Concurrent; namespace Orchard.Caching { public class DefaultCacheHolder : ICacheHolder { - private readonly IDictionary _caches = new Dictionary(); + private readonly ConcurrentDictionary _caches = new ConcurrentDictionary(); - class CacheKey : Pair> { + class CacheKey : Tuple { public CacheKey(Type component, Type key, Type result) - : base(component, new Pair(key, result)) { + : base(component, key, result) { } } public ICache GetCache(Type component) { var cacheKey = new CacheKey(component, typeof(TKey), typeof(TResult)); - lock (_caches) { - object value; - if (!_caches.TryGetValue(cacheKey, out value)) { - value = new Cache(); - _caches[cacheKey] = value; - } - return (ICache)value; - } + var result = _caches.GetOrAdd(cacheKey, k => new Cache()); + return (Cache)result; } } } \ No newline at end of file