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,7 @@ 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
+ ( ( IDictionary < string , ICache > ) allCacheRegions ) . Add ( cache . RegionName , cache . Cache ) ;
251
252
}
252
253
}
253
254
IEntityPersister cp = PersisterFactory . CreateClassPersister ( model , cache , this , mapping ) ;
@@ -375,7 +376,7 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
375
376
{
376
377
updateTimestampsCache = new UpdateTimestampsCache ( settings , properties ) ;
377
378
queryCache = settings . QueryCacheFactory . GetQueryCache ( null , updateTimestampsCache , settings , properties ) ;
378
- queryCaches = new ThreadSafeDictionary < string , IQueryCache > ( new Dictionary < string , IQueryCache > ( ) ) ;
379
+ queryCaches = new ConcurrentDictionary < string , IQueryCache > ( ) ;
379
380
}
380
381
else
381
382
{
@@ -967,10 +968,7 @@ public UpdateTimestampsCache UpdateTimestampsCache
967
968
968
969
public IDictionary < string , ICache > GetAllSecondLevelCacheRegions ( )
969
970
{
970
- lock ( allCacheRegions . SyncRoot )
971
- {
972
- return new Dictionary < string , ICache > ( allCacheRegions ) ;
973
- }
971
+ return allCacheRegions . ToDictionary ( kv => kv . Key , kv => kv . Value ) ;
974
972
}
975
973
976
974
public ICache GetSecondLevelCacheRegion ( string regionName )
@@ -1002,18 +1000,14 @@ public IQueryCache GetQueryCache(string cacheRegion)
1002
1000
return null ;
1003
1001
}
1004
1002
1005
- lock ( allCacheRegions . SyncRoot )
1006
- {
1007
- IQueryCache currentQueryCache ;
1008
- if ( ! queryCaches . TryGetValue ( cacheRegion , out currentQueryCache ) )
1003
+ return queryCaches . GetOrAdd (
1004
+ cacheRegion ,
1005
+ cr =>
1009
1006
{
1010
- currentQueryCache =
1011
- settings . QueryCacheFactory . GetQueryCache ( cacheRegion , updateTimestampsCache , settings , properties ) ;
1012
- queryCaches [ cacheRegion ] = currentQueryCache ;
1007
+ IQueryCache currentQueryCache = settings . QueryCacheFactory . GetQueryCache ( cacheRegion , updateTimestampsCache , settings , properties ) ;
1013
1008
allCacheRegions [ currentQueryCache . RegionName ] = currentQueryCache . Cache ;
1014
- }
1015
- return currentQueryCache ;
1016
- }
1009
+ return currentQueryCache ;
1010
+ } ) ;
1017
1011
}
1018
1012
1019
1013
public void EvictQueries ( )
@@ -1277,4 +1271,4 @@ public string Uuid
1277
1271
1278
1272
#endregion
1279
1273
}
1280
- }
1274
+ }
0 commit comments