Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit e1ab8ce

Browse files
committed
Change the default Db to long? to avoid overriding db setting on connection string
1 parent 03266db commit e1ab8ce

File tree

4 files changed

+88
-12
lines changed

4 files changed

+88
-12
lines changed

src/ServiceStack.Redis/BasicRedisClientManager.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public partial class BasicRedisClientManager
4141

4242
public IRedisClientFactory RedisClientFactory { get; set; }
4343

44-
public long Db { get; private set; }
44+
public long? Db { get; private set; }
4545

4646
public Action<IRedisNativeClient> ConnectionFilter { get; set; }
4747

@@ -65,14 +65,14 @@ public BasicRedisClientManager(int initialDb, params string[] readWriteHosts)
6565
public BasicRedisClientManager(
6666
IEnumerable<string> readWriteHosts,
6767
IEnumerable<string> readOnlyHosts)
68-
: this(readWriteHosts, readOnlyHosts, RedisNativeClient.DefaultDb)
68+
: this(readWriteHosts, readOnlyHosts, null)
6969
{
7070
}
7171

7272
public BasicRedisClientManager(
7373
IEnumerable<string> readWriteHosts,
7474
IEnumerable<string> readOnlyHosts,
75-
long initalDb)
75+
long? initalDb)
7676
{
7777
this.Db = initalDb;
7878

@@ -127,8 +127,8 @@ private RedisClient InitNewClient(RedisEndpoint nextHost)
127127
client.IdleTimeOutSecs = this.IdleTimeOutSecs.Value;
128128
if (this.NamespacePrefix != null)
129129
client.NamespacePrefix = NamespacePrefix;
130-
if (client.Db != Db) //Reset database to default if changed
131-
client.ChangeDb(Db);
130+
if (Db != null && client.Db != Db) //Reset database to default if changed
131+
client.ChangeDb(Db.Value);
132132

133133
return client;
134134
}

src/ServiceStack.Redis/PooledRedisClientManager.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ public partial class PooledRedisClientManager
6060

6161
public IRedisClientFactory RedisClientFactory { get; set; }
6262

63-
public long Db { get; private set; }
63+
public long? Db { get; private set; }
6464

6565
public Action<IRedisNativeClient> ConnectionFilter { get; set; }
6666

6767
public PooledRedisClientManager() : this(RedisNativeClient.DefaultHost) { }
6868

6969
public PooledRedisClientManager(int poolSize, int poolTimeOutSeconds, params string[] readWriteHosts)
70-
: this(readWriteHosts, readWriteHosts, null, RedisNativeClient.DefaultDb, poolSize, poolTimeOutSeconds)
70+
: this(readWriteHosts, readWriteHosts, null, null, poolSize, poolTimeOutSeconds)
7171
{
7272
}
7373

@@ -96,7 +96,7 @@ public PooledRedisClientManager(
9696
IEnumerable<string> readWriteHosts,
9797
IEnumerable<string> readOnlyHosts,
9898
RedisClientManagerConfig config)
99-
: this(readWriteHosts, readOnlyHosts, config, RedisNativeClient.DefaultDb, null, null)
99+
: this(readWriteHosts, readOnlyHosts, config, null, null, null)
100100
{
101101
}
102102

@@ -112,12 +112,12 @@ public PooledRedisClientManager(
112112
IEnumerable<string> readWriteHosts,
113113
IEnumerable<string> readOnlyHosts,
114114
RedisClientManagerConfig config,
115-
long initalDb,
115+
long? initalDb,
116116
int? poolSizeMultiplier,
117117
int? poolTimeOutSeconds)
118118
{
119119
this.Db = config != null
120-
? config.DefaultDb.GetValueOrDefault(initalDb)
120+
? config.DefaultDb ?? initalDb
121121
: initalDb;
122122

123123
ReadWriteHosts = readWriteHosts.ToRedisEndPoints();
@@ -281,8 +281,8 @@ private void InitClient(RedisClient client)
281281
client.IdleTimeOutSecs = this.IdleTimeOutSecs.Value;
282282
if (this.NamespacePrefix != null)
283283
client.NamespacePrefix = NamespacePrefix;
284-
if (client.Db != Db) //Reset database to default if changed
285-
client.ChangeDb(Db);
284+
if (Db != null && client.Db != Db) //Reset database to default if changed
285+
client.ChangeDb(Db.Value);
286286
}
287287

288288
/// <summary>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
namespace ServiceStack.Redis.Tests.Issues
5+
{
6+
public class RedisCharacterizationTests
7+
{
8+
private IRedisClientsManager _db1ClientManager;
9+
private IRedisClientsManager _db2ClientManager;
10+
11+
[SetUp]
12+
public void SetUp()
13+
{
14+
foreach (var clientManager in new[] { _db1ClientManager, _db2ClientManager })
15+
{
16+
if (clientManager != null)
17+
{
18+
using (var cacheClient = clientManager.GetCacheClient())
19+
{
20+
cacheClient.Remove("key");
21+
}
22+
}
23+
}
24+
}
25+
26+
[Test]
27+
public void BasicRedisClientManager_WhenUsingADatabaseOnARedisConnectionString_CorrectDatabaseIsUsed()
28+
{
29+
TestForDatabaseOnConnectionString(connectionString => new BasicRedisClientManager(connectionString));
30+
}
31+
32+
[Test]
33+
public void PooledRedisClientManager_WhenUsingADatabaseOnARedisConnectionString_CorrectDatabaseIsUsed()
34+
{
35+
TestForDatabaseOnConnectionString(connectionString => new PooledRedisClientManager(connectionString));
36+
}
37+
38+
[Test]
39+
public void RedisManagerPool_WhenUsingADatabaseOnARedisConnectionString_CorrectDatabaseIsUsed()
40+
{
41+
TestForDatabaseOnConnectionString(connectionString => new RedisManagerPool(connectionString));
42+
}
43+
44+
private void TestForDatabaseOnConnectionString(Func<string, IRedisClientsManager> factory)
45+
{
46+
_db1ClientManager = factory("localhost?db=1");
47+
_db2ClientManager = factory("localhost?db=2");
48+
49+
using (var cacheClient = _db1ClientManager.GetCacheClient())
50+
{
51+
cacheClient.Set("key", "value");
52+
}
53+
using (var cacheClient = _db2ClientManager.GetCacheClient())
54+
{
55+
Assert.Null(cacheClient.Get<string>("key"));
56+
}
57+
}
58+
59+
[Test]
60+
public void WhenUsingAnInitialDatabase_CorrectDatabaseIsUsed()
61+
{
62+
_db1ClientManager = new BasicRedisClientManager(1, "localhost");
63+
_db2ClientManager = new BasicRedisClientManager(2, "localhost");
64+
65+
using (var cacheClient = _db1ClientManager.GetCacheClient())
66+
{
67+
cacheClient.Set("key", "value");
68+
}
69+
using (var cacheClient = _db2ClientManager.GetCacheClient())
70+
{
71+
Assert.Null(cacheClient.Get<string>("key"));
72+
}
73+
}
74+
}
75+
}

tests/ServiceStack.Redis.Tests/ServiceStack.Redis.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<Compile Include="AdhocClientTests.cs" />
180180
<Compile Include="ConfigTests.cs" />
181181
<Compile Include="CustomCommandTests.cs" />
182+
<Compile Include="Issues\RedisCharacterizationTests.cs" />
182183
<Compile Include="RedisManagerPoolTests.cs" />
183184
<Compile Include="DiagnosticTests.cs" />
184185
<Compile Include="Examples\ServiceStack_Redis_UseCase.cs" />

0 commit comments

Comments
 (0)