Introduction to using servicestackredis to link to redis [go to]

Source: Internet
Author: User
Tags object serialization
Note: For details about how to configure redis in Windows and Linux, refer to this article: discuz! Redis Architecture Design in NT

Currently, there are some C # client tools linked to apsaradb for redis on the Internet. Here we will introduce Servicestackredis, Link address:
Https://github.com/mythz/ServiceStack.Redis

The source code package or DLL file in the link below is introduced to the project, and the following namespace reference is added (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 ("10.0.4.227", 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:
Public class userinfo
{
Public long ID {set; get ;}
Public String username {Get; set ;}
Public int 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:

Public static pooledredisclientmanager createmanager (string [] readwritehosts, string [] readonlyhosts)
{
// Supports read/write splitting and load balancing
Return new 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 (New String [] {"10.0.4.210: 6379"}, new string [] {"10.0.4.210: 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 ("10.0.4.227", 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.


Test code download link:

/Files/daizhj/redis.sample.rar

Introduction to using servicestackredis to link to redis [go to]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.