Skip to content

Commit a6b7851

Browse files
maca88fredericDelaporte
authored andcommitted
Added the ability to set the default configuration options by a static property
1 parent 54371f2 commit a6b7851

File tree

5 files changed

+152
-37
lines changed

5 files changed

+152
-37
lines changed

StackExRedis/NHibernate.Caches.StackExRedis.Tests/RedisCacheProviderFixture.cs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,37 @@ public void TestExpiration()
5555
"unexpected expiration value for noExplicitExpiration region with cache.default_expiration");
5656
}
5757

58+
[Test]
59+
public void TestDefaultCacheConfiguration()
60+
{
61+
var connectionProvider = Substitute.For<IConnectionMultiplexerProvider>();
62+
var databaseProvider = Substitute.For<IDatabaseProvider>();
63+
var retryDelayProvider = Substitute.For<ICacheLockRetryDelayProvider>();
64+
var lockValueProvider = Substitute.For<ICacheLockValueProvider>();
65+
var regionStrategyFactory = Substitute.For<ICacheRegionStrategyFactory>();
66+
var serialzer = Substitute.For<IRedisSerializer>();
67+
68+
var defaultConfig = RedisCacheProvider.DefaultCacheConfiguration;
69+
defaultConfig.ConnectionMultiplexerProvider = connectionProvider;
70+
defaultConfig.DatabaseProvider = databaseProvider;
71+
defaultConfig.LockConfiguration.ValueProvider = lockValueProvider;
72+
defaultConfig.LockConfiguration.RetryDelayProvider = retryDelayProvider;
73+
defaultConfig.RegionStrategyFactory = regionStrategyFactory;
74+
defaultConfig.Serializer = serialzer;
75+
76+
var provider = (RedisCacheProvider) GetNewProvider();
77+
var config = provider.CacheConfiguration;
78+
79+
Assert.That(config.ConnectionMultiplexerProvider, Is.EqualTo(connectionProvider));
80+
Assert.That(config.DatabaseProvider, Is.EqualTo(databaseProvider));
81+
Assert.That(config.LockConfiguration.RetryDelayProvider, Is.EqualTo(retryDelayProvider));
82+
Assert.That(config.LockConfiguration.ValueProvider, Is.EqualTo(lockValueProvider));
83+
Assert.That(config.RegionStrategyFactory, Is.EqualTo(regionStrategyFactory));
84+
Assert.That(config.Serializer, Is.EqualTo(serialzer));
85+
86+
RedisCacheProvider.DefaultCacheConfiguration = new RedisCacheConfiguration();
87+
}
88+
5889
[Test]
5990
public void TestUserProvidedObjectsFactory()
6091
{
@@ -63,31 +94,31 @@ public void TestUserProvidedObjectsFactory()
6394
BindingFlags.Instance | BindingFlags.NonPublic);
6495

6596
var customObjectsFactory = new CustomObjectsFactory();
97+
var connectionProvider = Substitute.For<IConnectionMultiplexerProvider>();
98+
var databaseProvider = Substitute.For<IDatabaseProvider>();
99+
var retryDelayProvider = Substitute.For<ICacheLockRetryDelayProvider>();
100+
var lockValueProvider = Substitute.For<ICacheLockValueProvider>();
66101
var regionStrategyFactory = Substitute.For<ICacheRegionStrategyFactory>();
67102
var serialzer = Substitute.For<IRedisSerializer>();
68-
var lockValueProvider = Substitute.For<ICacheLockValueProvider>();
69-
var retryDelayProvider = Substitute.For<ICacheLockRetryDelayProvider>();
70-
var databaseProvider = Substitute.For<IDatabaseProvider>();
71-
var connectionProvider = Substitute.For<IConnectionMultiplexerProvider>();
72103

73-
customObjectsFactory.RegisterSingleton(serialzer);
104+
customObjectsFactory.RegisterSingleton(connectionProvider);
105+
customObjectsFactory.RegisterSingleton(databaseProvider);
106+
customObjectsFactory.RegisterSingleton(retryDelayProvider);
74107
customObjectsFactory.RegisterSingleton(lockValueProvider);
75108
customObjectsFactory.RegisterSingleton(regionStrategyFactory);
76-
customObjectsFactory.RegisterSingleton(retryDelayProvider);
77-
customObjectsFactory.RegisterSingleton(databaseProvider);
78-
customObjectsFactory.RegisterSingleton(connectionProvider);
109+
customObjectsFactory.RegisterSingleton(serialzer);
79110

80111
field.SetValue(Cfg.Environment.BytecodeProvider, customObjectsFactory);
81112

82113
var provider = (RedisCacheProvider)GetNewProvider();
83114
var config = provider.CacheConfiguration;
84115

85-
Assert.That(regionStrategyFactory, Is.EqualTo(config.RegionStrategyFactory));
86-
Assert.That(serialzer, Is.EqualTo(config.Serializer));
87-
Assert.That(lockValueProvider, Is.EqualTo(config.LockConfiguration.ValueProvider));
88-
Assert.That(retryDelayProvider, Is.EqualTo(config.LockConfiguration.RetryDelayProvider));
89-
Assert.That(databaseProvider, Is.EqualTo(config.DatabaseProvider));
90-
Assert.That(connectionProvider, Is.EqualTo(config.ConnectionMultiplexerProvider));
116+
Assert.That(config.ConnectionMultiplexerProvider, Is.EqualTo(connectionProvider));
117+
Assert.That(config.DatabaseProvider, Is.EqualTo(databaseProvider));
118+
Assert.That(config.LockConfiguration.RetryDelayProvider, Is.EqualTo(retryDelayProvider));
119+
Assert.That(config.LockConfiguration.ValueProvider, Is.EqualTo(lockValueProvider));
120+
Assert.That(config.RegionStrategyFactory, Is.EqualTo(regionStrategyFactory));
121+
Assert.That(config.Serializer, Is.EqualTo(serialzer));
91122

92123
field.SetValue(Cfg.Environment.BytecodeProvider, new ActivatorObjectsFactory());
93124
}

StackExRedis/NHibernate.Caches.StackExRedis/RedisCacheConfiguration.cs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

73
namespace NHibernate.Caches.StackExRedis
84
{
@@ -11,10 +7,26 @@ namespace NHibernate.Caches.StackExRedis
117
/// </summary>
128
public class RedisCacheConfiguration
139
{
10+
private static readonly IRedisSerializer DefaultSerializer = new BinaryRedisSerializer();
11+
private static readonly ICacheRegionStrategyFactory DefaultRegionStrategyFactory = new DefaultCacheRegionStrategyFactory();
12+
private static readonly IConnectionMultiplexerProvider DefaultConnectionMultiplexerProvider = new DefaultConnectionMultiplexerProvider();
13+
private static readonly IDatabaseProvider DefaultDatabaseProvider = new DefaultDatabaseProvider();
14+
private static readonly System.Type DefaultRegionStrategyType = typeof(DefaultRegionStrategy);
15+
16+
private IRedisSerializer _serializer;
17+
private ICacheRegionStrategyFactory _regionStrategyFactory;
18+
private IConnectionMultiplexerProvider _connectionMultiplexerProvider;
19+
private IDatabaseProvider _databaseProvider;
20+
private System.Type _defaultRegionStrategy;
21+
1422
/// <summary>
1523
/// The <see cref="IRedisSerializer"/> instance.
1624
/// </summary>
17-
public IRedisSerializer Serializer { get; set; } = new BinaryRedisSerializer();
25+
public IRedisSerializer Serializer
26+
{
27+
get => _serializer ?? DefaultSerializer;
28+
set => _serializer = value;
29+
}
1830

1931
/// <summary>
2032
/// The prefix that will be prepended before each cache key in order to avoid having collisions when multiple clients
@@ -57,22 +69,38 @@ public class RedisCacheConfiguration
5769
/// <summary>
5870
/// The <see cref="ICacheRegionStrategyFactory"/> instance.
5971
/// </summary>
60-
public ICacheRegionStrategyFactory RegionStrategyFactory { get; set; } = new DefaultCacheRegionStrategyFactory();
72+
public ICacheRegionStrategyFactory RegionStrategyFactory
73+
{
74+
get => _regionStrategyFactory ?? DefaultRegionStrategyFactory;
75+
set => _regionStrategyFactory = value;
76+
}
6177

6278
/// <summary>
6379
/// The <see cref="IConnectionMultiplexerProvider"/> instance.
6480
/// </summary>
65-
public IConnectionMultiplexerProvider ConnectionMultiplexerProvider { get; set; } = new DefaultConnectionMultiplexerProvider();
81+
public IConnectionMultiplexerProvider ConnectionMultiplexerProvider
82+
{
83+
get => _connectionMultiplexerProvider ?? DefaultConnectionMultiplexerProvider;
84+
set => _connectionMultiplexerProvider = value;
85+
}
6686

6787
/// <summary>
6888
/// The <see cref="IDatabaseProvider"/> instance.
6989
/// </summary>
70-
public IDatabaseProvider DatabaseProvider { get; set; } = new DefaultDatabaseProvider();
90+
public IDatabaseProvider DatabaseProvider
91+
{
92+
get => _databaseProvider ?? DefaultDatabaseProvider;
93+
set => _databaseProvider = value;
94+
}
7195

7296
/// <summary>
7397
/// The default <see cref="AbstractRegionStrategy"/> type.
7498
/// </summary>
75-
public System.Type DefaultRegionStrategy { get; set; } = typeof(DefaultRegionStrategy);
99+
public System.Type DefaultRegionStrategy
100+
{
101+
get => _defaultRegionStrategy ?? DefaultRegionStrategyType;
102+
set => _defaultRegionStrategy = value;
103+
}
76104

77105
/// <summary>
78106
/// The configuration for locking keys.

StackExRedis/NHibernate.Caches.StackExRedis/RedisCacheLockConfiguration.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ namespace NHibernate.Caches.StackExRedis
88
/// </summary>
99
public class RedisCacheLockConfiguration
1010
{
11+
private static readonly ICacheLockValueProvider DefaultValueProvider = new DefaultCacheLockValueProvider();
12+
private static readonly ICacheLockRetryDelayProvider DefaultRetryDelayProvider = new DefaultCacheLockRetryDelayProvider();
13+
14+
private ICacheLockValueProvider _valueProvider;
15+
private ICacheLockRetryDelayProvider _retryDelayProvider;
16+
1117
/// <summary>
1218
/// The timeout for a lock key to expire.
1319
/// </summary>
@@ -41,12 +47,20 @@ public class RedisCacheLockConfiguration
4147
/// <summary>
4248
/// The <see cref="ICacheLockValueProvider"/> instance.
4349
/// </summary>
44-
public ICacheLockValueProvider ValueProvider { get; set; } = new DefaultCacheLockValueProvider();
50+
public ICacheLockValueProvider ValueProvider
51+
{
52+
get => _valueProvider ?? DefaultValueProvider;
53+
set => _valueProvider = value;
54+
}
4555

4656
/// <summary>
4757
/// The <see cref="ICacheLockRetryDelayProvider"/> instance.
4858
/// </summary>
49-
public ICacheLockRetryDelayProvider RetryDelayProvider { get; set; } = new DefaultCacheLockRetryDelayProvider();
59+
public ICacheLockRetryDelayProvider RetryDelayProvider
60+
{
61+
get => _retryDelayProvider ?? DefaultRetryDelayProvider;
62+
set => _retryDelayProvider = value;
63+
}
5064

5165
/// <inheritdoc />
5266
public override string ToString()

StackExRedis/NHibernate.Caches.StackExRedis/RedisCacheProvider.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,19 @@ public class RedisCacheProvider : ICacheProvider
1515
private static readonly INHibernateLogger Log;
1616
private static readonly Dictionary<string, RegionConfig> ConfiguredCacheRegions;
1717
private static readonly CacheConfig ConfiguredCache;
18+
private static RedisCacheConfiguration _defaultCacheConfiguration = new RedisCacheConfiguration();
1819

1920
private IConnectionMultiplexer _connectionMultiplexer;
2021

22+
/// <summary>
23+
/// The default configuration that will be used for creating the <see cref="CacheConfiguration"/>.
24+
/// </summary>
25+
public static RedisCacheConfiguration DefaultCacheConfiguration
26+
{
27+
get => _defaultCacheConfiguration;
28+
set => _defaultCacheConfiguration = value ?? new RedisCacheConfiguration();
29+
}
30+
2131
static RedisCacheProvider()
2232
{
2333
Log = NHibernateLogger.For(typeof(RedisCacheProvider));
@@ -33,10 +43,42 @@ static RedisCacheProvider()
3343
}
3444
}
3545

46+
private static RedisCacheConfiguration CreateCacheConfiguration()
47+
{
48+
var defaultConfiguration = DefaultCacheConfiguration;
49+
var defaultLockConfiguration = defaultConfiguration.LockConfiguration;
50+
return new RedisCacheConfiguration
51+
{
52+
DefaultRegionStrategy = defaultConfiguration.DefaultRegionStrategy,
53+
DatabaseProvider = defaultConfiguration.DatabaseProvider,
54+
ConnectionMultiplexerProvider = defaultConfiguration.ConnectionMultiplexerProvider,
55+
RegionPrefix = defaultConfiguration.RegionPrefix,
56+
LockConfiguration =
57+
{
58+
RetryTimes = defaultLockConfiguration.RetryTimes,
59+
RetryDelayProvider = defaultLockConfiguration.RetryDelayProvider,
60+
MaxRetryDelay = defaultLockConfiguration.MaxRetryDelay,
61+
ValueProvider = defaultLockConfiguration.ValueProvider,
62+
KeyTimeout = defaultLockConfiguration.KeyTimeout,
63+
AcquireTimeout = defaultLockConfiguration.AcquireTimeout,
64+
KeySuffix = defaultLockConfiguration.KeySuffix,
65+
MinRetryDelay = defaultLockConfiguration.MinRetryDelay
66+
},
67+
Serializer = defaultConfiguration.Serializer,
68+
RegionStrategyFactory = defaultConfiguration.RegionStrategyFactory,
69+
CacheKeyPrefix = defaultConfiguration.CacheKeyPrefix,
70+
DefaultUseSlidingExpiration = defaultConfiguration.DefaultUseSlidingExpiration,
71+
DefaultExpiration = defaultConfiguration.DefaultExpiration,
72+
DefaultDatabase = defaultConfiguration.DefaultDatabase,
73+
DefaultAppendHashcode = defaultConfiguration.DefaultAppendHashcode,
74+
EnvironmentName = defaultConfiguration.EnvironmentName
75+
};
76+
}
77+
3678
/// <summary>
3779
/// The Redis cache configuration that is populated by the NHibernate configuration.
3880
/// </summary>
39-
public RedisCacheConfiguration CacheConfiguration { get; } = new RedisCacheConfiguration();
81+
public RedisCacheConfiguration CacheConfiguration { get; } = CreateCacheConfiguration();
4082

4183
/// <inheritdoc />
4284
public ICache BuildCache(string regionName, IDictionary<string, string> properties)

StackExRedis/NHibernate.Caches.StackExRedis/RedisCacheRegionConfiguration.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,58 +35,58 @@ public RedisCacheRegionConfiguration(string regionName)
3535
/// The name of the environment that will be prepended before each cache key in order to allow having
3636
/// multiple environments on the same Redis database.
3737
/// </summary>
38-
public string EnvironmentName { get; set; }
38+
public string EnvironmentName { get; internal set; }
3939

4040
/// <summary>
4141
/// The prefix that will be prepended before each cache key in order to avoid having collisions when multiple clients
4242
/// uses the same Redis database.
4343
/// </summary>
44-
public string CacheKeyPrefix { get; set; }
44+
public string CacheKeyPrefix { get; internal set; }
4545

4646
/// <summary>
4747
/// The expiration time for the keys to expire.
4848
/// </summary>
49-
public TimeSpan Expiration { get; set; }
49+
public TimeSpan Expiration { get; internal set; }
5050

5151
/// <summary>
5252
/// The prefix that will be prepended before the region name when building a cache key.
5353
/// </summary>
54-
public string RegionPrefix { get; set; }
54+
public string RegionPrefix { get; internal set; }
5555

5656
/// <summary>
5757
/// The Redis database index.
5858
/// </summary>
59-
public int Database { get; set; }
59+
public int Database { get; internal set; }
6060

6161
/// <summary>
6262
/// Whether the expiration is sliding or not.
6363
/// </summary>
64-
public bool UseSlidingExpiration { get; set; }
64+
public bool UseSlidingExpiration { get; internal set; }
6565

6666
/// <summary>
6767
/// The <see cref="AbstractRegionStrategy"/> type.
6868
/// </summary>
69-
public System.Type RegionStrategy { get; set; }
69+
public System.Type RegionStrategy { get; internal set; }
7070

7171
/// <summary>
7272
/// Whether the hash code of the key should be added to the cache key.
7373
/// </summary>
74-
public bool AppendHashcode { get; set; }
74+
public bool AppendHashcode { get; internal set; }
7575

7676
/// <summary>
7777
/// The <see cref="IRedisSerializer"/> to be used.
7878
/// </summary>
79-
public IRedisSerializer Serializer { get; set; }
79+
public IRedisSerializer Serializer { get; internal set; }
8080

8181
/// <summary>
8282
/// The <see cref="IDatabaseProvider"/> instance.
8383
/// </summary>
84-
public IDatabaseProvider DatabaseProvider { get; set; } = new DefaultDatabaseProvider();
84+
public IDatabaseProvider DatabaseProvider { get; internal set; }
8585

8686
/// <summary>
8787
/// The configuration for locking keys.
8888
/// </summary>
89-
public RedisCacheLockConfiguration LockConfiguration { get; set; }
89+
public RedisCacheLockConfiguration LockConfiguration { get; internal set; }
9090

9191
/// <inheritdoc />
9292
public override string ToString()

0 commit comments

Comments
 (0)