1
1
using System ;
2
+ using System . Collections . Concurrent ;
2
3
using System . Collections . Generic ;
3
4
using System . Data . Common ;
4
5
using System . Linq ;
@@ -101,8 +102,8 @@ public void HandleEntityNotFound(string entityName, object id)
101
102
private static readonly IIdentifierGenerator UuidGenerator = new UUIDHexGenerator ( ) ;
102
103
103
104
[ NonSerialized ]
104
- private readonly ThreadSafeDictionary < string , ICache > allCacheRegions =
105
- new ThreadSafeDictionary < string , ICache > ( new Dictionary < string , ICache > ( ) ) ;
105
+ private readonly ConcurrentDictionary < string , ICache > allCacheRegions =
106
+ new ConcurrentDictionary < string , ICache > ( ) ;
106
107
107
108
[ NonSerialized ]
108
109
private readonly IDictionary < string , IClassMetadata > classMetadata ;
@@ -155,7 +156,7 @@ public void HandleEntityNotFound(string entityName, object id)
155
156
private readonly IQueryCache queryCache ;
156
157
157
158
[ NonSerialized ]
158
- private readonly IDictionary < string , IQueryCache > queryCaches ;
159
+ private readonly ConcurrentDictionary < string , IQueryCache > queryCaches ;
159
160
[ NonSerialized ]
160
161
private readonly SchemaExport schemaExport ;
161
162
[ NonSerialized ]
@@ -168,7 +169,7 @@ public void HandleEntityNotFound(string entityName, object id)
168
169
[ NonSerialized ]
169
170
private readonly UpdateTimestampsCache updateTimestampsCache ;
170
171
[ NonSerialized ]
171
- private readonly IDictionary < string , string [ ] > entityNameImplementorsMap = new ThreadSafeDictionary < string , string [ ] > ( new Dictionary < string , string [ ] > ( 100 ) ) ;
172
+ private readonly IDictionary < string , string [ ] > entityNameImplementorsMap = new ConcurrentDictionary < string , string [ ] > ( 4 * System . Environment . ProcessorCount , 100 ) ;
172
173
private readonly string uuid ;
173
174
private bool disposed ;
174
175
@@ -255,7 +256,7 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
255
256
if ( cache != null )
256
257
{
257
258
caches . Add ( cacheRegion , cache ) ;
258
- allCacheRegions . Add ( cache . RegionName , cache . Cache ) ;
259
+ ( ( IDictionary < string , ICache > ) allCacheRegions ) . Add ( cache . RegionName , cache . Cache ) ;
259
260
}
260
261
}
261
262
IEntityPersister cp = PersisterFactory . CreateClassPersister ( model , cache , this , mapping ) ;
@@ -383,7 +384,7 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
383
384
{
384
385
updateTimestampsCache = new UpdateTimestampsCache ( settings , properties ) ;
385
386
queryCache = settings . QueryCacheFactory . GetQueryCache ( null , updateTimestampsCache , settings , properties ) ;
386
- queryCaches = new ThreadSafeDictionary < string , IQueryCache > ( new Dictionary < string , IQueryCache > ( ) ) ;
387
+ queryCaches = new ConcurrentDictionary < string , IQueryCache > ( ) ;
387
388
}
388
389
else
389
390
{
@@ -975,10 +976,7 @@ public UpdateTimestampsCache UpdateTimestampsCache
975
976
976
977
public IDictionary < string , ICache > GetAllSecondLevelCacheRegions ( )
977
978
{
978
- lock ( allCacheRegions . SyncRoot )
979
- {
980
- return new Dictionary < string , ICache > ( allCacheRegions ) ;
981
- }
979
+ return allCacheRegions . ToDictionary ( kv => kv . Key , kv => kv . Value ) ;
982
980
}
983
981
984
982
public ICache GetSecondLevelCacheRegion ( string regionName )
@@ -1010,18 +1008,14 @@ public IQueryCache GetQueryCache(string cacheRegion)
1010
1008
return null ;
1011
1009
}
1012
1010
1013
- lock ( allCacheRegions . SyncRoot )
1014
- {
1015
- IQueryCache currentQueryCache ;
1016
- if ( ! queryCaches . TryGetValue ( cacheRegion , out currentQueryCache ) )
1011
+ return queryCaches . GetOrAdd (
1012
+ cacheRegion ,
1013
+ cr =>
1017
1014
{
1018
- currentQueryCache =
1019
- settings . QueryCacheFactory . GetQueryCache ( cacheRegion , updateTimestampsCache , settings , properties ) ;
1020
- queryCaches [ cacheRegion ] = currentQueryCache ;
1015
+ IQueryCache currentQueryCache = settings . QueryCacheFactory . GetQueryCache ( cacheRegion , updateTimestampsCache , settings , properties ) ;
1021
1016
allCacheRegions [ currentQueryCache . RegionName ] = currentQueryCache . Cache ;
1022
- }
1023
- return currentQueryCache ;
1024
- }
1017
+ return currentQueryCache ;
1018
+ } ) ;
1025
1019
}
1026
1020
1027
1021
public void EvictQueries ( )
0 commit comments