Skip to content

Commit 5ceb682

Browse files
committed
Remove grouping of caches by type
1 parent b71dfd6 commit 5ceb682

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

src/NHibernate/Cache/CacheFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static ICacheConcurrencyStrategy CreateCache(
4141
{
4242
var cache = CreateCache(
4343
usage, name, settings,
44-
(r, u) => settings.CacheProvider.BuildCache(r, properties).AsCacheBase());
44+
r => settings.CacheProvider.BuildCache(r, properties).AsCacheBase());
4545

4646
if (cache != null && mutable && usage == ReadOnly)
4747
log.Warn("read-only cache configured for mutable: {0}", name);
@@ -61,7 +61,7 @@ public static ICacheConcurrencyStrategy CreateCache(
6161
string usage,
6262
string name,
6363
Settings settings,
64-
Func<string, string, CacheBase> regionAndUsageCacheGetter)
64+
Func<string, CacheBase> regionAndUsageCacheGetter)
6565
{
6666
if (usage == null || !settings.IsSecondLevelCacheEnabled) return null; //no cache
6767

@@ -92,7 +92,7 @@ public static ICacheConcurrencyStrategy CreateCache(
9292

9393
try
9494
{
95-
ccs.Cache = regionAndUsageCacheGetter(name, usage);
95+
ccs.Cache = regionAndUsageCacheGetter(name);
9696
}
9797
catch (CacheException e)
9898
{

src/NHibernate/Impl/SessionFactoryImpl.cs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ public void HandleEntityNotFound(string entityName, object id)
9595
private static readonly IIdentifierGenerator UuidGenerator = new UUIDHexGenerator();
9696

9797
[NonSerialized]
98-
private readonly ConcurrentDictionary<string, ConcurrentDictionary<string, CacheBase>> allCachePerRegionThenType =
99-
new ConcurrentDictionary<string, ConcurrentDictionary<string, CacheBase>>();
98+
private readonly ConcurrentDictionary<string, CacheBase> _allCacheRegions =
99+
new ConcurrentDictionary<string, CacheBase>();
100100

101101
[NonSerialized]
102102
private readonly IDictionary<string, IClassMetadata> classMetadata;
@@ -148,8 +148,6 @@ public void HandleEntityNotFound(string entityName, object id)
148148
[NonSerialized]
149149
private readonly IQueryCache queryCache;
150150

151-
private const string QueryCacheType = "QueryCache";
152-
153151
[NonSerialized]
154152
private readonly ConcurrentDictionary<string, Lazy<IQueryCache>> queryCaches;
155153
[NonSerialized]
@@ -370,7 +368,7 @@ public SessionFactoryImpl(Configuration cfg, IMapping mapping, Settings settings
370368
if (settings.IsQueryCacheEnabled)
371369
{
372370
var updateTimestampsCacheName = typeof(UpdateTimestampsCache).Name;
373-
updateTimestampsCache = new UpdateTimestampsCache(BuildCache(updateTimestampsCacheName, updateTimestampsCacheName));
371+
updateTimestampsCache = new UpdateTimestampsCache(BuildCache(updateTimestampsCacheName));
374372
var queryCacheName = typeof(StandardQueryCache).FullName;
375373
queryCache = BuildQueryCache(queryCacheName);
376374
queryCaches = new ConcurrentDictionary<string, Lazy<IQueryCache>>();
@@ -417,7 +415,7 @@ private IQueryCache BuildQueryCache(string queryCacheName)
417415
settings.QueryCacheFactory.GetQueryCache(
418416
updateTimestampsCache,
419417
properties,
420-
BuildCache(queryCacheName, QueryCacheType))
418+
BuildCache(queryCacheName))
421419
// 6.0 TODO: remove the coalesce once IQueryCacheFactory todos are done
422420
#pragma warning disable 618
423421
?? settings.QueryCacheFactory.GetQueryCache(
@@ -1071,12 +1069,11 @@ public IDictionary<string, ICache> GetAllSecondLevelCacheRegions()
10711069
#pragma warning restore 618
10721070
{
10731071
return
1074-
allCachePerRegionThenType
1072+
_allCacheRegions
10751073
// ToArray creates a moment in time snapshot
10761074
.ToArray()
1077-
// Caches are not unique per region, take the first one.
10781075
#pragma warning disable 618
1079-
.ToDictionary(kv => kv.Key, kv => (ICache)kv.Value.Values.First());
1076+
.ToDictionary(kv => kv.Key, kv => (ICache) kv.Value);
10801077
#pragma warning restore 618
10811078
}
10821079

@@ -1085,13 +1082,11 @@ public IDictionary<string, ICache> GetAllSecondLevelCacheRegions()
10851082
public ICache GetSecondLevelCacheRegion(string regionName)
10861083
#pragma warning restore 618
10871084
{
1088-
if (!allCachePerRegionThenType.TryGetValue(regionName, out var result))
1089-
return null;
1090-
// Caches are not unique per region, take the first one.
1091-
return result.Values.First();
1085+
_allCacheRegions.TryGetValue(regionName, out var result);
1086+
return result;
10921087
}
10931088

1094-
private CacheBase BuildCache(string cacheRegion, string type)
1089+
private CacheBase BuildCache(string cacheRegion)
10951090
{
10961091
// If run concurrently for the same region and type, this may built many caches for the same region and type.
10971092
// Currently only GetQueryCache may be run concurrently, and its implementation prevents
@@ -1101,15 +1096,8 @@ private CacheBase BuildCache(string cacheRegion, string type)
11011096
var prefix = settings.CacheRegionPrefix;
11021097
if (!string.IsNullOrEmpty(prefix))
11031098
cacheRegion = prefix + '.' + cacheRegion;
1104-
var cachesPerType = allCachePerRegionThenType.GetOrAdd(cacheRegion, cr => new ConcurrentDictionary<string, CacheBase>());
1105-
var cache = settings.CacheProvider.BuildCache(cacheRegion, properties).AsCacheBase();
1106-
if (!cachesPerType.TryAdd(type, cache))
1107-
{
1108-
cache.Destroy();
1109-
throw new InvalidOperationException($"A cache has already been built for region {cacheRegion} and type {type}.");
1110-
}
11111099

1112-
return cache;
1100+
return _allCacheRegions.GetOrAdd(cacheRegion, cr => settings.CacheProvider.BuildCache(cr, properties).AsCacheBase());
11131101
}
11141102

11151103
/// <summary> Statistics SPI</summary>

0 commit comments

Comments
 (0)