Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
StackExchange.Redis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
StackExchange.Redis
Commits
c7be3110
Commit
c7be3110
authored
Apr 22, 2015
by
Chad Gilbert
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added a way to specify the default database in the connection string
parent
07c23fbc
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
84 additions
and
9 deletions
+84
-9
DefaultDatabase.cs
StackExchange.Redis.Tests/Issues/DefaultDatabase.cs
+63
-0
StackExchange.Redis.Tests.csproj
StackExchange.Redis.Tests/StackExchange.Redis.Tests.csproj
+1
-0
ConfigurationOptions.cs
...xchange.Redis/StackExchange/Redis/ConfigurationOptions.cs
+15
-5
ConnectionMultiplexer.cs
...change.Redis/StackExchange/Redis/ConnectionMultiplexer.cs
+5
-4
No files found.
StackExchange.Redis.Tests/Issues/DefaultDatabase.cs
0 → 100644
View file @
c7be3110
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
using
NUnit.Framework
;
using
StackExchange.Redis
;
namespace
StackExchange.Redis.Tests.Issues
{
[
TestFixture
]
public
class
DefaultDatabase
:
TestBase
{
[
Test
]
public
void
UnspecifiedDbId_ReturnsNull
()
{
var
config
=
ConfigurationOptions
.
Parse
(
"localhost"
);
Assert
.
IsNull
(
config
.
DefaultDatabase
);
}
[
Test
]
public
void
SpecifiedDbId_ReturnsExpected
()
{
var
config
=
ConfigurationOptions
.
Parse
(
"localhost,defaultDatabase=3"
);
Assert
.
AreEqual
(
3
,
config
.
DefaultDatabase
);
}
[
Test
]
public
void
ConfigurationOptions_UnspecifiedDefaultDb
()
{
var
log
=
new
StringWriter
();
try
{
using
(
var
conn
=
ConnectionMultiplexer
.
Connect
(
string
.
Format
(
"{0}:{1}"
,
PrimaryServer
,
PrimaryPort
),
log
))
{
var
db
=
conn
.
GetDatabase
();
Assert
.
AreEqual
(
0
,
db
.
Database
);
}
}
finally
{
Console
.
WriteLine
(
log
);
}
}
[
Test
]
public
void
ConfigurationOptions_SpecifiedDefaultDb
()
{
var
log
=
new
StringWriter
();
try
{
using
(
var
conn
=
ConnectionMultiplexer
.
Connect
(
string
.
Format
(
"{0}:{1},defaultDatabase=3"
,
PrimaryServer
,
PrimaryPort
),
log
))
{
var
db
=
conn
.
GetDatabase
();
Assert
.
AreEqual
(
3
,
db
.
Database
);
}
}
finally
{
Console
.
WriteLine
(
log
);
}
}
}
}
StackExchange.Redis.Tests/StackExchange.Redis.Tests.csproj
View file @
c7be3110
...
@@ -71,6 +71,7 @@
...
@@ -71,6 +71,7 @@
<Compile
Include=
"ConnectingFailDetection.cs"
/>
<Compile
Include=
"ConnectingFailDetection.cs"
/>
<Compile
Include=
"ConnectToUnexistingHost.cs"
/>
<Compile
Include=
"ConnectToUnexistingHost.cs"
/>
<Compile
Include=
"HyperLogLog.cs"
/>
<Compile
Include=
"HyperLogLog.cs"
/>
<Compile
Include=
"Issues\DefaultDatabase.cs"
/>
<Compile
Include=
"Issues\Issue182.cs"
/>
<Compile
Include=
"Issues\Issue182.cs"
/>
<Compile
Include=
"WrapperBaseTests.cs"
/>
<Compile
Include=
"WrapperBaseTests.cs"
/>
<Compile
Include=
"TransactionWrapperTests.cs"
/>
<Compile
Include=
"TransactionWrapperTests.cs"
/>
...
...
StackExchange.Redis/StackExchange/Redis/ConfigurationOptions.cs
View file @
c7be3110
...
@@ -73,7 +73,7 @@ internal static void Unknown(string key)
...
@@ -73,7 +73,7 @@ internal static void Unknown(string key)
TieBreaker
=
"tiebreaker"
,
WriteBuffer
=
"writeBuffer"
,
Ssl
=
"ssl"
,
SslHost
=
"sslHost"
,
TieBreaker
=
"tiebreaker"
,
WriteBuffer
=
"writeBuffer"
,
Ssl
=
"ssl"
,
SslHost
=
"sslHost"
,
ConfigChannel
=
"configChannel"
,
AbortOnConnectFail
=
"abortConnect"
,
ResolveDns
=
"resolveDns"
,
ConfigChannel
=
"configChannel"
,
AbortOnConnectFail
=
"abortConnect"
,
ResolveDns
=
"resolveDns"
,
ChannelPrefix
=
"channelPrefix"
,
Proxy
=
"proxy"
,
ConnectRetry
=
"connectRetry"
,
ChannelPrefix
=
"channelPrefix"
,
Proxy
=
"proxy"
,
ConnectRetry
=
"connectRetry"
,
ConfigCheckSeconds
=
"configCheckSeconds"
,
ResponseTimeout
=
"responseTimeout"
;
ConfigCheckSeconds
=
"configCheckSeconds"
,
ResponseTimeout
=
"responseTimeout"
,
DefaultDatabase
=
"defaultDatabase"
;
private
static
readonly
Dictionary
<
string
,
string
>
normalizedOptions
=
new
[]
private
static
readonly
Dictionary
<
string
,
string
>
normalizedOptions
=
new
[]
{
{
AllowAdmin
,
SyncTimeout
,
AllowAdmin
,
SyncTimeout
,
...
@@ -82,7 +82,7 @@ internal static void Unknown(string key)
...
@@ -82,7 +82,7 @@ internal static void Unknown(string key)
TieBreaker
,
WriteBuffer
,
Ssl
,
SslHost
,
TieBreaker
,
WriteBuffer
,
Ssl
,
SslHost
,
ConfigChannel
,
AbortOnConnectFail
,
ResolveDns
,
ConfigChannel
,
AbortOnConnectFail
,
ResolveDns
,
ChannelPrefix
,
Proxy
,
ConnectRetry
,
ChannelPrefix
,
Proxy
,
ConnectRetry
,
ConfigCheckSeconds
ConfigCheckSeconds
,
DefaultDatabase
,
}.
ToDictionary
(
x
=>
x
,
StringComparer
.
InvariantCultureIgnoreCase
);
}.
ToDictionary
(
x
=>
x
,
StringComparer
.
InvariantCultureIgnoreCase
);
public
static
string
TryNormalize
(
string
value
)
public
static
string
TryNormalize
(
string
value
)
...
@@ -107,7 +107,7 @@ public static string TryNormalize(string value)
...
@@ -107,7 +107,7 @@ public static string TryNormalize(string value)
private
Version
defaultVersion
;
private
Version
defaultVersion
;
private
int
?
keepAlive
,
syncTimeout
,
connectTimeout
,
responseTimeout
,
writeBuffer
,
connectRetry
,
configCheckSeconds
;
private
int
?
keepAlive
,
syncTimeout
,
connectTimeout
,
responseTimeout
,
writeBuffer
,
connectRetry
,
configCheckSeconds
,
defaultDatabase
;
private
Proxy
?
proxy
;
private
Proxy
?
proxy
;
...
@@ -264,6 +264,11 @@ public CommandMap CommandMap
...
@@ -264,6 +264,11 @@ public CommandMap CommandMap
/// </summary>
/// </summary>
public
int
WriteBuffer
{
get
{
return
writeBuffer
.
GetValueOrDefault
(
4096
);
}
set
{
writeBuffer
=
value
;
}
}
public
int
WriteBuffer
{
get
{
return
writeBuffer
.
GetValueOrDefault
(
4096
);
}
set
{
writeBuffer
=
value
;
}
}
/// <summary>
/// Specifies the default database to be used when calling ConnectionMultiplexer.GetDatabase() without any parameters
/// </summary>
public
int
?
DefaultDatabase
{
get
{
return
defaultDatabase
;
}
set
{
defaultDatabase
=
value
;
}
}
internal
LocalCertificateSelectionCallback
CertificateSelectionCallback
{
get
{
return
CertificateSelection
;
}
private
set
{
CertificateSelection
=
value
;
}
}
internal
LocalCertificateSelectionCallback
CertificateSelectionCallback
{
get
{
return
CertificateSelection
;
}
private
set
{
CertificateSelection
=
value
;
}
}
// these just rip out the underlying handlers, bypassing the event accessors - needed when creating the SSL stream
// these just rip out the underlying handlers, bypassing the event accessors - needed when creating the SSL stream
...
@@ -327,7 +332,8 @@ public ConfigurationOptions Clone()
...
@@ -327,7 +332,8 @@ public ConfigurationOptions Clone()
SocketManager
=
SocketManager
,
SocketManager
=
SocketManager
,
connectRetry
=
connectRetry
,
connectRetry
=
connectRetry
,
configCheckSeconds
=
configCheckSeconds
,
configCheckSeconds
=
configCheckSeconds
,
responseTimeout
=
responseTimeout
responseTimeout
=
responseTimeout
,
defaultDatabase
=
defaultDatabase
,
};
};
foreach
(
var
item
in
endpoints
)
foreach
(
var
item
in
endpoints
)
options
.
endpoints
.
Add
(
item
);
options
.
endpoints
.
Add
(
item
);
...
@@ -373,6 +379,7 @@ public override string ToString()
...
@@ -373,6 +379,7 @@ public override string ToString()
Append
(
sb
,
OptionKeys
.
Proxy
,
proxy
);
Append
(
sb
,
OptionKeys
.
Proxy
,
proxy
);
Append
(
sb
,
OptionKeys
.
ConfigCheckSeconds
,
configCheckSeconds
);
Append
(
sb
,
OptionKeys
.
ConfigCheckSeconds
,
configCheckSeconds
);
Append
(
sb
,
OptionKeys
.
ResponseTimeout
,
responseTimeout
);
Append
(
sb
,
OptionKeys
.
ResponseTimeout
,
responseTimeout
);
Append
(
sb
,
OptionKeys
.
DefaultDatabase
,
defaultDatabase
);
if
(
commandMap
!=
null
)
commandMap
.
AppendDeltas
(
sb
);
if
(
commandMap
!=
null
)
commandMap
.
AppendDeltas
(
sb
);
return
sb
.
ToString
();
return
sb
.
ToString
();
}
}
...
@@ -462,7 +469,7 @@ static bool IsOption(string option, string prefix)
...
@@ -462,7 +469,7 @@ static bool IsOption(string option, string prefix)
void
Clear
()
void
Clear
()
{
{
clientName
=
serviceName
=
password
=
tieBreaker
=
sslHost
=
configChannel
=
null
;
clientName
=
serviceName
=
password
=
tieBreaker
=
sslHost
=
configChannel
=
null
;
keepAlive
=
syncTimeout
=
connectTimeout
=
writeBuffer
=
connectRetry
=
configCheckSeconds
=
null
;
keepAlive
=
syncTimeout
=
connectTimeout
=
writeBuffer
=
connectRetry
=
configCheckSeconds
=
defaultDatabase
=
null
;
allowAdmin
=
abortOnConnectFail
=
resolveDns
=
ssl
=
null
;
allowAdmin
=
abortOnConnectFail
=
resolveDns
=
ssl
=
null
;
defaultVersion
=
null
;
defaultVersion
=
null
;
endpoints
.
Clear
();
endpoints
.
Clear
();
...
@@ -568,6 +575,9 @@ private void DoParse(string configuration, bool ignoreUnknown)
...
@@ -568,6 +575,9 @@ private void DoParse(string configuration, bool ignoreUnknown)
case
OptionKeys
.
ResponseTimeout
:
case
OptionKeys
.
ResponseTimeout
:
ResponseTimeout
=
OptionKeys
.
ParseInt32
(
key
,
value
,
minValue
:
1
);
ResponseTimeout
=
OptionKeys
.
ParseInt32
(
key
,
value
,
minValue
:
1
);
break
;
break
;
case
OptionKeys
.
DefaultDatabase
:
defaultDatabase
=
OptionKeys
.
ParseInt32
(
key
,
value
);
break
;
default
:
default
:
if
(!
string
.
IsNullOrEmpty
(
key
)
&&
key
[
0
]
==
'$'
)
if
(!
string
.
IsNullOrEmpty
(
key
)
&&
key
[
0
]
==
'$'
)
{
{
...
...
StackExchange.Redis/StackExchange/Redis/ConnectionMultiplexer.cs
View file @
c7be3110
...
@@ -897,11 +897,12 @@ public ISubscriber GetSubscriber(object asyncState = null)
...
@@ -897,11 +897,12 @@ public ISubscriber GetSubscriber(object asyncState = null)
/// <summary>
/// <summary>
/// Obtain an interactive connection to a database inside redis
/// Obtain an interactive connection to a database inside redis
/// </summary>
/// </summary>
public
IDatabase
GetDatabase
(
int
db
=
0
,
object
asyncState
=
null
)
public
IDatabase
GetDatabase
(
int
?
db
=
null
,
object
asyncState
=
null
)
{
{
if
(
db
<
0
)
throw
new
ArgumentOutOfRangeException
(
"db"
);
int
dbId
=
db
??
configuration
.
DefaultDatabase
??
0
;
if
(
db
!=
0
&&
RawConfig
.
Proxy
==
Proxy
.
Twemproxy
)
throw
new
NotSupportedException
(
"Twemproxy only supports database 0"
);
if
(
dbId
<
0
)
throw
new
ArgumentOutOfRangeException
(
"db"
);
return
new
RedisDatabase
(
this
,
db
,
asyncState
);
if
(
dbId
!=
0
&&
RawConfig
.
Proxy
==
Proxy
.
Twemproxy
)
throw
new
NotSupportedException
(
"Twemproxy only supports database 0"
);
return
new
RedisDatabase
(
this
,
dbId
,
asyncState
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment