C # write client types mainly include:
Servicestack. redis★Https://github.com/ServiceStack/ServiceStack.Redis
Booksleeve★Http://code.google.com/p/booksleeve/
Sider http://nuget.org/List/Packages/Sider
Teamdev redis client http://redis.codeplex.com/
Redis-sharp https://github.com/migueldeicaza/redis-sharp
The star number is a high-usage item. Reference: http://redis.io/clients
Taking servicestack. redis as an Example
Introduce it to the project and add the following namespace reference (this article only ):
Using servicestack. Common. extensions;
Using servicestack. redis;
Using servicestack. redis. Generic;
Using servicestack. text;
Using servicestack. redis. Support;
Note: servicestackredis encapsulates a large number of methods and objects. Here, we only extract representative content. For more information, see its official documentation.
Declare a client object:
Protected redisclient redis = new redisclient ("127.0.0.1", 6379); // redis service IP address and port
I. Basic Key/value pair operations:
1. Add/obtain:
List <string> storemembers = new list <string> ();
Storemembers. foreach (x => redis. additemtolist ("additemtolist", x ));
Note: You can also directly use the addrangetolist method to load a group of data, for example:
Redis. addrangetolist ("addarrangetolist", storemembers );
2. Get Data
VaR members = redis. getallitemsfromlist ("additemtolist ");
Members. foreach (S => response. Write ("<br/> additemtolist:" + s ));
3. obtain data at the specified index location
VaR item = redis. getitemfromlist ("addarrangetolist", 2 );
4. Remove:
VaR list = redis. Lists ["addarrangetolist"];
List. Clear (); // clear
List. Remove ("two"); // remove a specified key value
List. removeat (2); // remove data from the specified index location
Ii. storage objects:
Publicclass userinfo
{
Publiclong ID {set; get ;}
Publicstring username {Get; set ;}
Publicint age {Get; set ;}
}
1. General method (the underlying layer uses JSON serialization ):
Redis. Set <userinfo> ("userinfo", new userinfo () {username = "", age = 45 });
Userinfo = redis. Get <userinfo> ("userinfo ");
Note: Of course, the above method is also suitable for basic types, such:
Redis. Set <int> ("my_age", 12); // or redis. Set ("my_age", 12 );
Int age = redis. Get <int> ("my_age ");
2. Object serialization storage:
VaR SER = new objectserializer (); // located in namespace servicestack. redis. Support;
Bool result = redis. Set <byte []> ("userinfo", Ser. serialize (New userinfo () {username = "James", age = 12 }));
Userinfo = Ser. deserialize (redis. Get <byte []> ("userinfo") as userinfo;
// List supported
Redis. Set <byte []> ("userinfolist_serialize", Ser. serialize (userinfolist ));
List <userinfo> userlist = Ser. deserialize (redis. Get <byte []> ("userinfolist_serialize") as list <userinfo>;
It should be noted that JSON serialization is more efficient than Object serialization during testing.
3. Store table objects, such:
Using (VAR redisusers = redis. gettypedclient <userinfo> ())
{
Redisusers. Store (New userinfo {id = redisusers. getnextsequence (), username = "daizhj", age = 12 });
Redisusers. Store (New userinfo {id = redisusers. getnextsequence (), username = "daizhenjun", age = 13 });
VaR allusers = redisusers. getall (); // perform crud and other operations just like performing an ADO object.
Allusers. foreach (S => response. Write ("<br/> User:" + S. username + "Age:" + S. Age ));
}
4. Use the Client Connection Pool Mode to increase the link speed:
Publicstatic pooledredisclientmanager createmanager (string [] readwritehosts, string [] readonlyhosts)
{
// Supports read/write splitting and load balancing
Returnnew pooledredisclientmanager (readwritehosts, readonlyhosts, new redisclientmanagerconfig
{
Maxwritepoolsize = 5, // number of links in the "write" link pool
Maxreadpoolsize = 5, // number of links in the "write" link pool
Autostart = true,
});
}
Declare the link pool object (only one redis server is used here ):
Pooledredisclientmanager prcm = createmanager (newstring [] {"127.0.0.1: 6379"}, newstring [] {"127.0.0.1: 6379 "});
List <userinfo> userinfolist = new list <userinfo> ();
Userinfolist. Add (New userinfo () {username = "pool_daizhj", age = 1 });
Userinfolist. Add (New userinfo () {username = "pool_daizhj1", age = 2 });
Get a link from the pool:
Using (iredisclient redis = prcm. getclient ())
{
Redis. Set ("userinfolist", userinfolist );
List <userinfo> userlist = redis. Get <list <userinfo> ("userinfolist ");
}
Note:
1. In the first three methods, I tested locally and found that the access efficiency was high. The specific reason is still to be analyzed.
2. If you only want to use a persistent link instead of a link pool, you can directly declare the following object in static mode:
Protected static redisclient redis = new redisclient ("127.0.0.1", 6379 );
In this way, only one customer link is displayed on the redis server.
3. compared with the memcached test, memcached is nearly efficient in data storage (the first method in this article). memcached is faster (in milliseconds) than redis in data retrieval ), this is not as mentioned in some online articles. In actual development and production environments, the background and results should prevail.