Skip to content

Commit

Permalink
Fix: for #86 Race condition (concurrency issue) in SurrogateBuilderPr…
Browse files Browse the repository at this point in the history
…ovider
  • Loading branch information
Toby Henderson committed Aug 11, 2015
1 parent 59a40e1 commit 309804c
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/OpenRasta/TypeSystem/Surrogated/SurrogateBuilderProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;

using OpenRasta.DI;
using OpenRasta.TypeSystem.Surrogates;

Expand All @@ -9,8 +10,8 @@ namespace OpenRasta.TypeSystem.Surrogated
public class SurrogateBuilderProvider : ISurrogateProvider
{
readonly ISurrogateBuilder[] _builders;
static Dictionary<IType, IType> _typeCache = new Dictionary<IType, IType>();
static Dictionary<IProperty, IProperty> _propCache = new Dictionary<IProperty, IProperty>();
static readonly ConcurrentDictionary<IType, IType> _typeCache = new ConcurrentDictionary<IType, IType>();
static readonly ConcurrentDictionary<IProperty, IProperty> _propCache = new ConcurrentDictionary<IProperty, IProperty>();
// HACK: Waiting to push Func<IEnumerable<T>> resolution
// in container. Remove when done.
public SurrogateBuilderProvider(IDependencyResolver resolver)
Expand Down Expand Up @@ -56,17 +57,9 @@ IType FindTypeSurrogate(IType type)
});

}
T Cached<T>(Dictionary<T, T> cache, T value, Func<T, T> createCached)
T Cached<T>(ConcurrentDictionary<T, T> cache, T value, Func<T, T> createCached)
{
T cachedValue;
if (cache.TryGetValue(value, out cachedValue))
return cachedValue;
lock (cache)
{
cachedValue = createCached(value);
cache.Add(value, cachedValue);
return cachedValue;
}
return cache.GetOrAdd(value, createCached);
}
}
}

0 comments on commit 309804c

Please sign in to comment.