Skip to content

Commit 4d97ed1

Browse files
committed
Remove delegate based logic
1 parent 5ceb682 commit 4d97ed1

File tree

5 files changed

+47
-58
lines changed

5 files changed

+47
-58
lines changed

src/NHibernate/Cache/CacheFactory.cs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,36 +39,28 @@ public static ICacheConcurrencyStrategy CreateCache(
3939
Settings settings,
4040
IDictionary<string, string> properties)
4141
{
42-
var cache = CreateCache(
43-
usage, name, settings,
44-
r => settings.CacheProvider.BuildCache(r, properties).AsCacheBase());
42+
if (usage == null || !settings.IsSecondLevelCacheEnabled) return null;
4543

46-
if (cache != null && mutable && usage == ReadOnly)
47-
log.Warn("read-only cache configured for mutable: {0}", name);
44+
var cache = BuildCacheBase(name, settings, properties);
45+
46+
var ccs = CreateCache(usage,cache);
4847

49-
return cache;
48+
if (mutable && usage == ReadOnly)
49+
log.Warn("read-only cache configured for mutable: {0}", name);
50+
51+
return ccs;
5052
}
5153

5254
/// <summary>
5355
/// Creates an <see cref="ICacheConcurrencyStrategy"/> from the parameters.
5456
/// </summary>
5557
/// <param name="usage">The name of the strategy that <see cref="ICacheProvider"/> should use for the class.</param>
56-
/// <param name="name">The name of the cache region the strategy is being created for.</param>
57-
/// <param name="settings">Used to retrieve the global cache region prefix.</param>
58-
/// <param name="regionAndUsageCacheGetter">The delegate for obtaining the <see cref="ICache" /> to use for the region.</param>
58+
/// <param name="cache">The <see cref="CacheBase"/> used for this strategy.</param>
5959
/// <returns>An <see cref="ICacheConcurrencyStrategy"/> to use for this object in the <see cref="ICache"/>.</returns>
60-
public static ICacheConcurrencyStrategy CreateCache(
61-
string usage,
62-
string name,
63-
Settings settings,
64-
Func<string, CacheBase> regionAndUsageCacheGetter)
60+
public static ICacheConcurrencyStrategy CreateCache(string usage, CacheBase cache)
6561
{
66-
if (usage == null || !settings.IsSecondLevelCacheEnabled) return null; //no cache
67-
6862
if (log.IsDebugEnabled())
69-
{
70-
log.Debug("cache for: {0} usage strategy: {1}", name, usage);
71-
}
63+
log.Debug("cache for: {0} usage strategy: {1}", cache.RegionName, usage);
7264

7365
ICacheConcurrencyStrategy ccs;
7466
switch (usage)
@@ -90,16 +82,21 @@ public static ICacheConcurrencyStrategy CreateCache(
9082
"cache usage attribute should be read-write, read-only or nonstrict-read-write");
9183
}
9284

85+
ccs.Cache = cache;
86+
87+
return ccs;
88+
}
89+
90+
internal static CacheBase BuildCacheBase(string name, Settings settings, IDictionary<string, string> properties)
91+
{
9392
try
9493
{
95-
ccs.Cache = regionAndUsageCacheGetter(name);
94+
return settings.CacheProvider.BuildCache(name, properties).AsCacheBase();
9695
}
9796
catch (CacheException e)
9897
{
9998
throw new HibernateException("Could not instantiate cache implementation", e);
10099
}
101-
102-
return ccs;
103100
}
104101
}
105102
}

src/NHibernate/Cache/StandardQueryCache.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,13 @@ public StandardQueryCache(
3131
string regionName)
3232
: this(
3333
updateTimestampsCache,
34-
settings.CacheProvider.BuildCache(
35-
(!string.IsNullOrEmpty(settings.CacheRegionPrefix) ? settings.CacheRegionPrefix + '.' : "") +
36-
(regionName ?? typeof (StandardQueryCache).FullName),
34+
CacheFactory.BuildCacheBase(
35+
settings.GetFullCacheRegionName(regionName ?? typeof(StandardQueryCache).FullName),
36+
settings,
3737
props))
3838
{
3939
}
4040

41-
// Since v5.2
42-
[Obsolete]
43-
private StandardQueryCache(UpdateTimestampsCache updateTimestampsCache, ICache cache)
44-
: this(updateTimestampsCache, cache.AsCacheBase())
45-
{
46-
}
47-
4841
/// <summary>
4942
/// Build a query cache.
5043
/// </summary>
@@ -60,7 +53,6 @@ public StandardQueryCache(
6053
_regionName = regionCache.RegionName;
6154
Log.Info("starting query cache at region: {0}", _regionName);
6255

63-
Cache = regionCache;
6456
_cache = regionCache;
6557
_updateTimestampsCache = updateTimestampsCache;
6658
}
@@ -69,7 +61,7 @@ public StandardQueryCache(
6961

7062
// 6.0 TODO: type as CacheBase instead
7163
#pragma warning disable 618
72-
public ICache Cache { get; }
64+
public ICache Cache => _cache;
7365
#pragma warning restore 618
7466

7567
public string RegionName

src/NHibernate/Cache/UpdateTimestampsCache.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,10 @@ public virtual void Clear()
2929
[Obsolete("Please use overload with an CacheBase parameter.")]
3030
public UpdateTimestampsCache(Settings settings, IDictionary<string, string> props)
3131
: this(
32-
settings.CacheProvider.BuildCache(
33-
(settings.CacheRegionPrefix == null ? "" : settings.CacheRegionPrefix + '.') +
34-
typeof(UpdateTimestampsCache).Name,
35-
props)) {}
36-
37-
// Since v5.2
38-
[Obsolete]
39-
private UpdateTimestampsCache(ICache cache)
40-
: this(cache.AsCacheBase())
32+
CacheFactory.BuildCacheBase(
33+
settings.GetFullCacheRegionName(nameof(UpdateTimestampsCache)),
34+
settings,
35+
props))
4136
{
4237
}
4338

src/NHibernate/Cfg/Settings.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,5 +137,13 @@ public Settings()
137137
public IQueryModelRewriterFactory QueryModelRewriterFactory { get; internal set; }
138138

139139
#endregion
140+
141+
internal string GetFullCacheRegionName(string name)
142+
{
143+
var prefix = CacheRegionPrefix;
144+
if (!string.IsNullOrEmpty(prefix))
145+
return prefix + '.' + name;
146+
return name;
147+
}
140148
}
141149
}

src/NHibernate/Impl/SessionFactoryImpl.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -433,18 +433,15 @@ private ICacheConcurrencyStrategy GetCacheConcurrencyStrategy(
433433
Dictionary<Tuple<string, string>, ICacheConcurrencyStrategy> caches)
434434
{
435435
var cacheKey = new Tuple<string, string>(cacheRegion, strategy);
436-
if (!caches.TryGetValue(cacheKey, out var cache))
437-
{
438-
cache = CacheFactory.CreateCache(
439-
strategy,
440-
cacheRegion,
441-
settings,
442-
BuildCache);
443-
if (cache != null)
444-
caches.Add(cacheKey, cache);
445-
}
436+
if (strategy == null || !settings.IsSecondLevelCacheEnabled)
437+
return null;
438+
439+
if (caches.TryGetValue(cacheKey, out var cache))
440+
return cache;
446441

447-
if (cache != null && isMutable && strategy == CacheFactory.ReadOnly)
442+
cache = CacheFactory.CreateCache(strategy, BuildCache(cacheRegion));
443+
caches.Add(cacheKey, cache);
444+
if (isMutable && strategy == CacheFactory.ReadOnly)
448445
log.Warn("read-only cache configured for mutable: {0}", name);
449446

450447
return cache;
@@ -1093,11 +1090,11 @@ private CacheBase BuildCache(string cacheRegion)
10931090
// concurrent creation call for the same region, so this will not happen.
10941091
// Otherwise the dictionary will have to be changed for using a lazy, see
10951092
// https://stackoverflow.com/a/31637510/1178314
1096-
var prefix = settings.CacheRegionPrefix;
1097-
if (!string.IsNullOrEmpty(prefix))
1098-
cacheRegion = prefix + '.' + cacheRegion;
1093+
cacheRegion = settings.GetFullCacheRegionName(cacheRegion);
10991094

1100-
return _allCacheRegions.GetOrAdd(cacheRegion, cr => settings.CacheProvider.BuildCache(cr, properties).AsCacheBase());
1095+
return _allCacheRegions.GetOrAdd(
1096+
cacheRegion,
1097+
cr => CacheFactory.BuildCacheBase(cr, settings, properties));
11011098
}
11021099

11031100
/// <summary> Statistics SPI</summary>

0 commit comments

Comments
 (0)