1
1
using System ;
2
+ using System . Collections . Concurrent ;
2
3
using System . Collections . Generic ;
3
4
using System . Data ;
4
5
using System . Linq ;
@@ -93,8 +94,8 @@ public void HandleEntityNotFound(string entityName, object id)
93
94
private static readonly IIdentifierGenerator UuidGenerator = new UUIDHexGenerator ( ) ;
94
95
95
96
[ 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 > ( ) ;
98
99
99
100
[ NonSerialized ]
100
101
private readonly IDictionary < string , IClassMetadata > classMetadata ;
@@ -147,7 +148,7 @@ public void HandleEntityNotFound(string entityName, object id)
147
148
private readonly IQueryCache queryCache ;
148
149
149
150
[ NonSerialized ]
150
- private readonly IDictionary < string , IQueryCache > queryCaches ;
151
+ private readonly ConcurrentDictionary < string , IQueryCache > queryCaches ;
151
152
[ NonSerialized ]
152
153
private readonly SchemaExport schemaExport ;
153
154
[ NonSerialized ]
@@ -160,7 +161,7 @@ public void HandleEntityNotFound(string entityName, object id)
160
161
[ NonSerialized ]
161
162
private readonly UpdateTimestampsCache updateTimestampsCache ;
162
163
[ 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 ) ;
164
165
private readonly string uuid ;
165
166
private bool disposed ;
166
167
@@ -247,7 +248,10 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
247
248
if ( cache != null )
248
249
{
249
250
caches . Add ( cacheRegion , cache ) ;
250
- allCacheRegions . Add ( cache . RegionName , cache . Cache ) ;
251
+ if ( ! allCacheRegions . TryAdd ( cache . RegionName , cache . Cache ) )
252
+ {
253
+ throw new HibernateException ( "An item with the same key has already been added to allCacheRegions." ) ;
254
+ }
251
255
}
252
256
}
253
257
IEntityPersister cp = PersisterFactory . CreateClassPersister ( model , cache , this , mapping ) ;
@@ -375,7 +379,7 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
375
379
{
376
380
updateTimestampsCache = new UpdateTimestampsCache ( settings , properties ) ;
377
381
queryCache = settings . QueryCacheFactory . GetQueryCache ( null , updateTimestampsCache , settings , properties ) ;
378
- queryCaches = new ThreadSafeDictionary < string , IQueryCache > ( new Dictionary < string , IQueryCache > ( ) ) ;
382
+ queryCaches = new ConcurrentDictionary < string , IQueryCache > ( ) ;
379
383
}
380
384
else
381
385
{
@@ -967,10 +971,8 @@ public UpdateTimestampsCache UpdateTimestampsCache
967
971
968
972
public IDictionary < string , ICache > GetAllSecondLevelCacheRegions ( )
969
973
{
970
- lock ( allCacheRegions . SyncRoot )
971
- {
972
- return new Dictionary < string , ICache > ( allCacheRegions ) ;
973
- }
974
+ // ToArray creates a moment in time snapshot
975
+ return allCacheRegions . ToArray ( ) . ToDictionary ( kv => kv . Key , kv => kv . Value ) ;
974
976
}
975
977
976
978
public ICache GetSecondLevelCacheRegion ( string regionName )
@@ -1002,18 +1004,14 @@ public IQueryCache GetQueryCache(string cacheRegion)
1002
1004
return null ;
1003
1005
}
1004
1006
1005
- lock ( allCacheRegions . SyncRoot )
1006
- {
1007
- IQueryCache currentQueryCache ;
1008
- if ( ! queryCaches . TryGetValue ( cacheRegion , out currentQueryCache ) )
1007
+ return queryCaches . GetOrAdd (
1008
+ cacheRegion ,
1009
+ cr =>
1009
1010
{
1010
- currentQueryCache =
1011
- settings . QueryCacheFactory . GetQueryCache ( cacheRegion , updateTimestampsCache , settings , properties ) ;
1012
- queryCaches [ cacheRegion ] = currentQueryCache ;
1011
+ IQueryCache currentQueryCache = settings . QueryCacheFactory . GetQueryCache ( cacheRegion , updateTimestampsCache , settings , properties ) ;
1013
1012
allCacheRegions [ currentQueryCache . RegionName ] = currentQueryCache . Cache ;
1014
- }
1015
- return currentQueryCache ;
1016
- }
1013
+ return currentQueryCache ;
1014
+ } ) ;
1017
1015
}
1018
1016
1019
1017
public void EvictQueries ( )
@@ -1277,4 +1275,4 @@ public string Uuid
1277
1275
1278
1276
#endregion
1279
1277
}
1280
- }
1278
+ }
0 commit comments