@@ -148,7 +148,7 @@ public void HandleEntityNotFound(string entityName, object id)
148
148
private readonly IQueryCache queryCache ;
149
149
150
150
[ NonSerialized ]
151
- private readonly ConcurrentDictionary < string , IQueryCache > queryCaches ;
151
+ private readonly ConcurrentDictionary < string , Lazy < IQueryCache > > queryCaches ;
152
152
[ NonSerialized ]
153
153
private readonly SchemaExport schemaExport ;
154
154
[ NonSerialized ]
@@ -379,7 +379,7 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
379
379
{
380
380
updateTimestampsCache = new UpdateTimestampsCache ( settings , properties ) ;
381
381
queryCache = settings . QueryCacheFactory . GetQueryCache ( null , updateTimestampsCache , settings , properties ) ;
382
- queryCaches = new ConcurrentDictionary < string , IQueryCache > ( ) ;
382
+ queryCaches = new ConcurrentDictionary < string , Lazy < IQueryCache > > ( ) ;
383
383
}
384
384
else
385
385
{
@@ -845,9 +845,9 @@ public void Close()
845
845
{
846
846
queryCache . Destroy ( ) ;
847
847
848
- foreach ( IQueryCache cache in queryCaches . Values )
848
+ foreach ( var cache in queryCaches . Values )
849
849
{
850
- cache . Destroy ( ) ;
850
+ cache . Value . Destroy ( ) ;
851
851
}
852
852
853
853
updateTimestampsCache . Destroy ( ) ;
@@ -1027,21 +1027,18 @@ public IQueryCache GetQueryCache(string cacheRegion)
1027
1027
return null ;
1028
1028
}
1029
1029
1030
- var wasAdded = false ;
1031
1030
// The factory may be run concurrently by threads trying to get the same region.
1032
- // But the GetOrAdd will yield the same cache for all threads, so in case of add, use
1033
- // it to update allCacheRegions
1034
- var cache = queryCaches . GetOrAdd (
1031
+ // But the GetOrAdd will yield the same lazy for all threads, so only one will
1032
+ // initialize. https://stackoverflow.com/a/31637510/1178314
1033
+ return queryCaches . GetOrAdd (
1035
1034
cacheRegion ,
1036
- cr =>
1037
- {
1038
- var currentQueryCache = settings . QueryCacheFactory . GetQueryCache ( cacheRegion , updateTimestampsCache , settings , properties ) ;
1039
- wasAdded = true ;
1040
- return currentQueryCache ;
1041
- } ) ;
1042
- if ( wasAdded )
1043
- allCacheRegions [ cache . RegionName ] = cache . Cache ;
1044
- return cache ;
1035
+ cr => new Lazy < IQueryCache > (
1036
+ ( ) =>
1037
+ {
1038
+ var currentQueryCache = settings . QueryCacheFactory . GetQueryCache ( cr , updateTimestampsCache , settings , properties ) ;
1039
+ allCacheRegions [ currentQueryCache . RegionName ] = currentQueryCache . Cache ;
1040
+ return currentQueryCache ;
1041
+ } ) ) . Value ;
1045
1042
}
1046
1043
1047
1044
public void EvictQueries ( )
@@ -1067,10 +1064,9 @@ public void EvictQueries(string cacheRegion)
1067
1064
{
1068
1065
if ( settings . IsQueryCacheEnabled )
1069
1066
{
1070
- IQueryCache currentQueryCache ;
1071
- if ( queryCaches . TryGetValue ( cacheRegion , out currentQueryCache ) )
1067
+ if ( queryCaches . TryGetValue ( cacheRegion , out var currentQueryCache ) )
1072
1068
{
1073
- currentQueryCache . Clear ( ) ;
1069
+ currentQueryCache . Value . Clear ( ) ;
1074
1070
}
1075
1071
}
1076
1072
}
0 commit comments