Servicestack. redis is a C # client that accesses redis. It can be said that all functions that require redis-CLI can be implemented through it. However, today I have set an access password on the primary redis instance, but I have not set the access password on slave. I use a cache factory to get the connection. You can directly set a password in redisclient instantiation.
1 /// <summary> 2 // cache Client Manager factory 3 /// </Summary> 4 public class poolmanagerfactory 5 {6 Private Static pooledredisclientmanager manager = NULL; 7 public static pooledredisclientmanager createmanager (string [] readwritehosts, string [] readonlyhosts, int initialdb = 0) 8 {9 If (Manager = NULL) 10 {11 manager = new pooledredisclientmanager (readwritehosts, readonlyhosts, new partition () 12 {13 maxwritepoolsize = 5, 14 maxreadpoolsize = 5, 15 autostart = true16}, initialdb, 50, 5 ); 17} 18 return manager; 19} 20 21}
I always think that only IP: port can be entered in the readwritehosts array to represent the connection of a redis instance. But how can I add the password to it? Unexpectedly, you can only download the source code for viewing. The original implementation is achieved by separating strings,
1 /// <summary> 2 // you can add auth to the IP Address [email protected]: port 3 /// </Summary> 4 /// <Param name = "hosts"> </param> 5 /// <returns> </returns> 6 public static list <redisendpoint> toredisendpoints (this ienumerable <string> hosts) 7 {8 If (hosts = NULL) return new list <redisendpoint> (); 9 // list of redis endpoints 10 var redisendpoints = new list <redisendpoint> (); 11 foreach (VAR host in hosts) 12 {13 redisendpoint endpoint; 14 string [] hostparts; 15 if (host. contains ("@") 16 {17 hostparts = host. splitonlast ('@'); 18 var Password = hostparts [0]; 19 hostparts = hostparts [1]. split (':'); 20 endpoint = getredisendpoint (hostparts); 21 endpoint. password = password; 22} 23 else24 {25 hostparts = host. split (':'); 26 endpoint = getredisendpoint (hostparts); 27} 28 redisendpoints. add (endpoint); 29} 30 return redisendpoints; 31}
Add @ in front of IP: port to indicate the password, for example, [email protected]: port. Now I know how happy the source code program is. Open source is great.
MASTER: Set Password: config set requirepass Password
Slave specifies the master password: config set masterauth password can be used to set the password on the master, and it is very convenient to restart the redis instance, but this method fails after restart.
Servicestack. redis set the password in pooledredisclientmanager