Now more and more developers use service-stack.redis to access redis, but there are multiple ways to obtain the redisclient, one of which gets the client from the buffer pool is very recognized by everyone.
1 List <string> listwrite = new list <string> () {"[email protected]: 6380"}; 2 list <string> readhosts = new list <string> () {"192.168.8.245: 6381", "192.168.8.245: 6382"}; 3 pooledredisclientmanager clientmanager = poolmanagerfactory. createmanager (listwrite. toarray (), readhosts. toarray ()); 4 /// you can add password verification in the cache manager because no corresponding password field is displayed 5 // get a client through getclient to connect 6 using (iredisclient redisclient = clientmanager. getclient () 7 {8 iredistypedclient <phone> phones = redisclient. as <phone> (); 9 10 phone phonefive = phones. getvalue ("5"); 11 if (phonefive = NULL) 12 {13 phonefive = new phone () 14 {15 Id = 5, 16 manufacturer = "Nokia ", 17 model = "guozhiqi", 18 owner = new person () 19 {20 21 Id = 1, 22 name = "Yuan Jinzhou", 23 surname = "old" 24} 25 }; 26. Phones. setentry (phonefive. id. tostring (), phonefive); 27} 28 console. writeline ("ownerid" + phones. getvalue ("5 "). owner. name); 29}
Please note the fifth line of the code above, using (iredisclient redisclient = clientmanager. getclient ()){}
Use the clientmanager. getclient method to obtain a connection. We also use this method in ado.net, and the performance is very high. We believe that this method must first obtain a connection from the buffer pool, then execute the code in using, and finally dispose. But sometimes this method has low performance when the traffic is slightly higher. Why?
1 /// <summary> 2 /// returns a read/write client (the default) using the hosts defined in readwritehosts 3 // return the server address defined in readwritehosts by default for client connections that can be read and written. 4 /// </Summary> 5 /// <returns> </returns> 6 Public iredisclient getclient () 7 {8 lock (writeclients) 9 {10 assertvalidreadwritepool (); 11 12 redisclient inactiveclient; 13 while (inactiveclient = getinactivewriteclient () = NULL) 14 {15 if (PO Oltimeout. hasvalue) 16 {17 // wait for a connection, cry out if made to wait too long18 if (! Monitor. wait (writeclients, pooltimeout. value) 19 throw new timeoutexception (pooltimeouterror); 20} 21 else22 monitor. wait (writeclients, recheckpoolafterms); 23} 24 25 writepoolindex ++; 26 inactiveclient. active = true; 27 28 If (this. connecttimeout! = NULL) 29 {30 inactiveclient. connecttimeout = This. connecttimeout. value; 31} 32 33 If (this. socketsendtimeout. hasvalue) 34 {35 inactiveclient. sendtimeout = This. socketsendtimeout. value; 36} 37 If (this. socketreceivetimeout. hasvalue) 38 {39 inactiveclient. receivetimeout = This. socketreceivetimeout. value; 40} 41 if (this. idletimeoutsecs. hasvalue) 42 {43 inactiveclient. idletimeoutsecs = This. idletimeoutsecs. Value; 44} 45 46 inactiveclient. namespaceprefix = namespaceprefix; 47 48 // reset database to default if changed49 if (inactiveclient. DB! = DB) 50 {51 inactiveclient. changedb (db); 52} 53 54 return inactiveclient; 55} 56}
This is the implementation of getclient in the service-stack.redis, but we found a problem is that he only gets the connection from the main redis, it is impossible to return the slave readonly connection.
If the cache is set to 5, the performance of 500 requests at the same time will still be affected because the slave READ function is completely ignored.
To write data, call clientmanager. getclient () to obtain the redis instance of writehosts.
To read, call clientmanager. getreadonlyclient () to obtain the redis instance of readonlyhost only.
If you are in trouble, you can use clientmanager. getcacheclient () is used to obtain a connection. It calls getclient to obtain the connection while writing, and getreadonlyclient to obtain the connection when reading the connection. This allows read/write splitting, the master-slave replication feature of apsaradb for redis is used.
One of the reasons why the Service-stack.redis uses pooledredisclientmanager for slow speed