Skip to content

Commit bee970b

Browse files
committed
Cleanup wrapping of ICache to CacheBase
1 parent e377772 commit bee970b

9 files changed

+27
-33
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace NHibernate.Cache
4+
{
5+
//6.0 TODO: Remove
6+
internal static class CacheExtensions
7+
{
8+
#pragma warning disable 618
9+
public static CacheBase AsCacheBase(this ICache cache)
10+
#pragma warning restore 618
11+
{
12+
if (cache == null) throw new ArgumentNullException(nameof(cache));
13+
return cache as CacheBase ?? new ObsoleteCacheWrapper(cache);
14+
}
15+
}
16+
}

src/NHibernate/Cache/CacheFactory.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ public static ICacheConcurrencyStrategy CreateCache(
4141
{
4242
var cache = CreateCache(
4343
usage, name, settings,
44-
(r, u) =>
45-
{
46-
var c = settings.CacheProvider.BuildCache(r, properties);
47-
return c as CacheBase ?? new ObsoleteCacheWrapper(c);
48-
});
44+
(r, u) => settings.CacheProvider.BuildCache(r, properties).AsCacheBase());
4945

5046
if (cache != null && mutable && usage == ReadOnly)
5147
log.Warn("read-only cache configured for mutable: {0}", name);

src/NHibernate/Cache/ICacheConcurrencyStrategy.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,7 @@ internal static CacheBase GetCacheBase(this ICacheConcurrencyStrategy cache)
206206
{
207207
if (cache is IBatchableCacheConcurrencyStrategy batchableCache)
208208
return batchableCache.Cache;
209-
var concreteCache = cache.Cache;
210-
if (concreteCache == null)
211-
return null;
212-
return concreteCache as CacheBase ?? new ObsoleteCacheWrapper(concreteCache);
209+
return cache.Cache?.AsCacheBase();
213210
}
214211
}
215212
}

src/NHibernate/Cache/NonstrictReadWriteCache.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public partial class NonstrictReadWriteCache : IBatchableCacheConcurrencyStrateg
1818
{
1919
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(NonstrictReadWriteCache));
2020

21-
// 6.0 TODO: remove
2221
private CacheBase _cache;
2322

2423
/// <summary>
@@ -35,10 +34,7 @@ public ICache Cache
3534
#pragma warning restore 618
3635
{
3736
get { return _cache; }
38-
set
39-
{
40-
_cache = value as CacheBase ?? new ObsoleteCacheWrapper(value);
41-
}
37+
set { _cache = value.AsCacheBase(); }
4238
}
4339

4440
// 6.0 TODO: make implicit and switch to auto-property

src/NHibernate/Cache/ReadOnlyCache.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public partial class ReadOnlyCache : IBatchableCacheConcurrencyStrategy
1313
{
1414
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(ReadOnlyCache));
1515

16-
// 6.0 TODO: remove
1716
private CacheBase _cache;
1817

1918
/// <summary>
@@ -30,10 +29,7 @@ public ICache Cache
3029
#pragma warning restore 618
3130
{
3231
get { return _cache; }
33-
set
34-
{
35-
_cache = value as CacheBase ?? new ObsoleteCacheWrapper(value);
36-
}
32+
set { _cache = value.AsCacheBase(); }
3733
}
3834

3935
// 6.0 TODO: make implicit and switch to auto-property

src/NHibernate/Cache/ReadWriteCache.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public interface ILockable
3535
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(ReadWriteCache));
3636

3737
private readonly object _lockObject = new object();
38-
// 6.0 TODO: remove
3938
private CacheBase _cache;
4039
private int _nextLockId;
4140

@@ -53,10 +52,7 @@ public ICache Cache
5352
#pragma warning restore 618
5453
{
5554
get { return _cache; }
56-
set
57-
{
58-
_cache = value as CacheBase ?? new ObsoleteCacheWrapper(value);
59-
}
55+
set { _cache = value.AsCacheBase(); }
6056
}
6157

6258
// 6.0 TODO: make implicit and switch to auto-property

src/NHibernate/Cache/StandardQueryCache.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ public partial class StandardQueryCache : IQueryCache, IBatchableQueryCache
2020
private static readonly INHibernateLogger Log = NHibernateLogger.For(typeof (StandardQueryCache));
2121
private readonly string _regionName;
2222
private readonly UpdateTimestampsCache _updateTimestampsCache;
23-
// 6.0 TODO: remove
2423
private readonly CacheBase _cache;
2524

2625
// Since v5.2
27-
[Obsolete("Please use overload with an ICache parameter.")]
26+
[Obsolete("Please use overload with an CacheBase parameter.")]
2827
public StandardQueryCache(
2928
Settings settings,
3029
IDictionary<string, string> props,
@@ -41,10 +40,8 @@ public StandardQueryCache(
4140

4241
// Since v5.2
4342
[Obsolete]
44-
private StandardQueryCache(
45-
UpdateTimestampsCache updateTimestampsCache,
46-
ICache cache)
47-
: this(updateTimestampsCache, cache as CacheBase ?? new ObsoleteCacheWrapper(cache))
43+
private StandardQueryCache(UpdateTimestampsCache updateTimestampsCache, ICache cache)
44+
: this(updateTimestampsCache, cache.AsCacheBase())
4845
{
4946
}
5047

src/NHibernate/Cache/UpdateTimestampsCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public virtual void Clear()
2626
}
2727

2828
// Since v5.2
29-
[Obsolete("Please use overload with an ICache parameter.")]
29+
[Obsolete("Please use overload with an CacheBase parameter.")]
3030
public UpdateTimestampsCache(Settings settings, IDictionary<string, string> props)
3131
: this(
3232
settings.CacheProvider.BuildCache(
@@ -37,7 +37,7 @@ public UpdateTimestampsCache(Settings settings, IDictionary<string, string> prop
3737
// Since v5.2
3838
[Obsolete]
3939
private UpdateTimestampsCache(ICache cache)
40-
: this(cache as CacheBase ?? new ObsoleteCacheWrapper(cache))
40+
: this(cache.AsCacheBase())
4141
{
4242
}
4343

src/NHibernate/Impl/SessionFactoryImpl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ private CacheBase BuildCache(string cacheRegion, string type)
11121112
throw new InvalidOperationException($"A cache has already been built for region {cacheRegion} and type {type}.");
11131113
}
11141114

1115-
return cache as CacheBase ?? new ObsoleteCacheWrapper(cache);
1115+
return cache.AsCacheBase();
11161116
}
11171117

11181118
/// <summary> Statistics SPI</summary>

0 commit comments

Comments
 (0)