Configuration
There are many different ways to configure Redis, and Stackexchange.redis provides a rich configuration model that we can pass in when calling the Connect or connectasync method:
var conn = connectionmultiplexer.connect (configuration);
Here the parameter configure can be: configurationoptions Instance configuration string mode configuration
The latter is also a marker form of the previous one. basic configuration-through string configuration
The simplest configuration instance is configured with the hostname:
var conn = Connectionmultiplexer.connect ("localhost");
This connects to a single server on your computer and defaults to Redis's default port: 6379. There are also options to append in a comma-delimited manner. The port is usually represented by a colon (:). The name of the configuration option followed by a = sign. As shown below:
var conn = Connectionmultiplexer.connect ("Redis0:6380,redis1:6380,allowadmin=true");
We are free to convert each other between string and configurationoptions , as follows:
Configurationoptions options = Configurationoptions.parse (configstring);
Or
String configstring = Options. ToString ();
The most common way is to store the basic information in a string and then apply this specific basic information at run time:
String configstring = Getredisconfiguration ();
var options = Configurationoptions.parse (configstring);
Options. ClientName = Getappname (); The options are only known at run time
. Allowadmin = true;
conn = Connectionmultiplexer.connect (options);
Examples of using Redis and attaching passwords on Microsoft Azure:
var conn = Connectionmultiplexer.connect ("contoso5.redis.cache.windows.net,ssl=true,password= ...");
configuration Options
The configurationoptions object has many properties, and the description of all options is in the smart tip. The following table is a description of some of the most commonly used options:
Configuration String |
configurationoptions |
Description |
Abortconnect={bool} |
Abortonconnectfail |
If true, connect does not create a connection when no server is available |
Allowadmin={bool} |
Allowadmin |
If true, turn on some of the commands that are considered risky |
Channelprefix={string} |
Channelprefix |
Optional channel prefixes for all pub/sub operations |
Connectretry={int} |
Connectretry |
The number of connection retries when Connect is initialized |
Connecttimeout={int} |
ConnectTimeout |
Connection Timeout setting, time unit is MS |
Configchannel={string} |
Configurationchannel |
Set the broadcast channel name |
Defaultdatabase={int} |
DefaultDatabase |
Default database index, from 0 to databases-1 |
Keepalive={int} |
KeepAlive |
If there is no activity within the specified time (seconds), a message is sent to help the socket stay connected |
Name={string} |
ClientName |
Unique name used to identify the connection within Redis |
Password={string} |
Password |
Redis Server Password |
Proxy={proxy type} |
Proxy |
The type of proxy used (if any); such as "Twemproxy" |
Resolvedns={bool} |
Resolvedns |
Specifies that DNS parsing is displayed rather than implicitly |
Servicename={string} |
ServiceName |
Not currently implemented |
Ssl={bool} |
Ssl |
Specify to use SSL encryption |
Sslhost={string} |
Sslhost |
Enforcing SSL host recognition requires the use of server-side certificates |
Synctimeout={int} |
Synctimeout |
Asynchronous timeout setting (ms) |
Tiebreaker={string} |
Tiebreaker |
The main is to choose between a fuzzy host as the primary server |
Version={string} |
Defaultversion |
Redis version level (this option is useful when the server is unavailable) |
Writebuffer={int} |
WriteBuffer |
The size of the output buffer |
The tokens in the configuration string are separated by commas; Any service terminal that does not have a = symbol is assumed to be a redis. If SSL is not turned on and the terminal does not specify an explicit port, then 6379 will be used as the port, and 6380 will be the port if SSL is turned on. tokens that begin with ∗∗ are mapped as commands, for example: ∗∗** The starting tag is mapped as a command, for example: **config=cfg . automatic configuration and manual configuration
In many common cases, Stackexchange.redis will automatically configure multiple settings options, including server type and version, connection timeout, and master/slave relationship configuration. However, sometimes this command is forbidden in Redis server. In this case, it is very useful to provide more information:
configurationoptions config = new configurationoptions
{
endpoints =
{"
redis0", 6379},
{" Redis1 ", 6380}
},
Commandmap = commandmap.create (new hashset<string>
{//exclude several commands
" INFO "," CONFIG "," CLUSTER ",
" PING "," ECHO "," CLIENT "
}, Available:false),
KeepAlive =
defaultversion = New Version (2, 8, 8),
Password = "changeme"
};
The above configuration is equivalent to the following string configuration:
redis0:6379,redis1:6380,keepalive=180,version=2.8.8, $CLIENT =, $CLUSTER =, $CONFIG =, $ECHO =, $INFO =, $PING =
Rename command
Some of the less common features in Redis are that you can disable or rename individual commands. As shown earlier, this is achieved through Commandmap , not through hashset\
var commands = new Dictionary<string,string> {
"info", null},//disable
{"Select", "Use"},//rename to equivalent for some reason SQL
};
var options = new Configurationoptions {
//...
Commandmap = commandmap.create (commands),
//...
}
The above configuration is equivalent to the following string configuration (in the connection string):
$INFO =, $SELECT =use
Twemproxy
Twemproxy is a tool that allows multiple Redis instances to be used as a single service, with built-in sharding and fault tolerance (much like a redis cluster, but it is implemented separately). Twemproxy simplifies the usability of feature settings. To avoid manual configuration, theProxy option can be configured like this:
var options = new Configurationoptions
{
endpoints = {"My-server"},
Proxy = Proxy.twemproxy
};
Break the deadlock (tiebreakers or weigh decisions) and configure change announcements
Usually Stackexchange.redis will automatically resolve the master/slave node problem. However, you may not be using a management tool like a Redis cluster or Redis-sentinel , and you may encounter an occasion where you have multiple primary nodes (for example: when we reset a node for maintenance purposes), It may re-appear on the network as a primary node. To solve this problem, Stackexchange.redis can use the concept of breaking the deadlock (tradeoff decision), which applies only to situations where multiple hosts are discovered. To be compatible with Booksleeve, the default key name is "__booksleeve_tiebreak"(which is always in the database indexed as 0). This is used as a simple voting mechanism to help the decision-making that is the preferred master node, so that it works correctly for routing.
Similarly, when the configuration changes (especially in the master/slave configuration), this will be very important for the connection instance, which can make them aware of the new situation (through ifno,config , etc.). Stackexchange.redis pub/sub send such notifications via auto-subscription. For a similar reason, the default is the key name is "__booksleeve_masterchanged".
These two options can be customized or disabled (set to "") and can be passed . Configurationchannel and . Tiebreaker to configure the properties.
These settings can also be configured with Iserver.makemaster () to set up messages that break the deadlock (Tie-breaker) and broadcast configuration changes in the database. With the connectionmultiplexer.publishreconfigure method, configuration messages can also use the master/slave update alone to request that all nodes refresh their configuration.