11using System;
2+ using System.Collections.Concurrent;
23using System.Collections.Generic;
34using System.Data;
45using System.Linq;
@@ -93,8 +94,8 @@ public void HandleEntityNotFound(string entityName, object id)
9394 private static readonly IIdentifierGenerator UuidGenerator = new UUIDHexGenerator();
9495
9596 [NonSerialized]
96- private readonly ThreadSafeDictionary <string, ICache> allCacheRegions =
97- new ThreadSafeDictionary <string, ICache>(new Dictionary<string, ICache>() );
97+ private readonly ConcurrentDictionary <string, ICache> allCacheRegions =
98+ new ConcurrentDictionary <string, ICache>();
9899
99100 [NonSerialized]
100101 private readonly IDictionary<string, IClassMetadata> classMetadata;
@@ -147,7 +148,7 @@ public void HandleEntityNotFound(string entityName, object id)
147148 private readonly IQueryCache queryCache;
148149
149150 [NonSerialized]
150- private readonly IDictionary <string, IQueryCache> queryCaches;
151+ private readonly ConcurrentDictionary <string, IQueryCache> queryCaches;
151152 [NonSerialized]
152153 private readonly SchemaExport schemaExport;
153154 [NonSerialized]
@@ -160,7 +161,7 @@ public void HandleEntityNotFound(string entityName, object id)
160161 [NonSerialized]
161162 private readonly UpdateTimestampsCache updateTimestampsCache;
162163 [NonSerialized]
163- private readonly IDictionary<string, string[]> entityNameImplementorsMap = new ThreadSafeDictionary <string, string[]>(new Dictionary<string, string[]>( 100) );
164+ private readonly IDictionary<string, string[]> entityNameImplementorsMap = new ConcurrentDictionary <string, string[]>(4 * System.Environment.ProcessorCount, 100);
164165 private readonly string uuid;
165166 private bool disposed;
166167
@@ -247,7 +248,7 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
247248 if (cache != null)
248249 {
249250 caches.Add(cacheRegion, cache);
250- allCacheRegions.Add(cache.RegionName, cache.Cache);
251+ ((IDictionary<string, ICache>) allCacheRegions) .Add(cache.RegionName, cache.Cache);
251252 }
252253 }
253254 IEntityPersister cp = PersisterFactory.CreateClassPersister(model, cache, this, mapping);
@@ -375,7 +376,7 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
375376 {
376377 updateTimestampsCache = new UpdateTimestampsCache(settings, properties);
377378 queryCache = settings.QueryCacheFactory.GetQueryCache(null, updateTimestampsCache, settings, properties);
378- queryCaches = new ThreadSafeDictionary <string, IQueryCache>(new Dictionary<string, IQueryCache>() );
379+ queryCaches = new ConcurrentDictionary <string, IQueryCache>();
379380 }
380381 else
381382 {
@@ -967,10 +968,7 @@ public UpdateTimestampsCache UpdateTimestampsCache
967968
968969 public IDictionary<string, ICache> GetAllSecondLevelCacheRegions()
969970 {
970- lock (allCacheRegions.SyncRoot)
971- {
972- return new Dictionary<string, ICache>(allCacheRegions);
973- }
971+ return allCacheRegions.ToDictionary(kv => kv.Key, kv => kv.Value);
974972 }
975973
976974 public ICache GetSecondLevelCacheRegion(string regionName)
@@ -1002,18 +1000,14 @@ public IQueryCache GetQueryCache(string cacheRegion)
10021000 return null;
10031001 }
10041002
1005- lock (allCacheRegions.SyncRoot)
1006- {
1007- IQueryCache currentQueryCache;
1008- if (!queryCaches.TryGetValue(cacheRegion, out currentQueryCache))
1003+ return queryCaches.GetOrAdd(
1004+ cacheRegion,
1005+ cr =>
10091006 {
1010- currentQueryCache =
1011- settings.QueryCacheFactory.GetQueryCache(cacheRegion, updateTimestampsCache, settings, properties);
1012- queryCaches[cacheRegion] = currentQueryCache;
1007+ IQueryCache currentQueryCache = settings.QueryCacheFactory.GetQueryCache(cacheRegion, updateTimestampsCache, settings, properties);
10131008 allCacheRegions[currentQueryCache.RegionName] = currentQueryCache.Cache;
1014- }
1015- return currentQueryCache;
1016- }
1009+ return currentQueryCache;
1010+ });
10171011 }
10181012
10191013 public void EvictQueries()
@@ -1277,4 +1271,4 @@ public string Uuid
12771271
12781272 #endregion
12791273 }
1280- }
1274+ }
0 commit comments