Use of redis in. net

Source: Internet
Author: User

Windows sever package: http://code.google.com/p/servicestack/wiki/RedisWindowsDownload

Windows is only used for testing, and its performance is not as high as that in Linux.

C # usage:

Referenced servicestackredis: https://github.com/mythz/ServiceStack.Redis

Static void main (string [] ARGs) {var redis = new redisclient ("127.0.0.1", 6379 ); // redis service IP address and port # region = insert = // var storemembers = new list <string> {"JJ", "Lihui", "cc"}; // storemembers. foreach (x => redis. additemtolist ("additemtolist", x); // Note: You can directly use the addrangetolist method to load a group of data, for example: // redis. addrangetolist ("additemtolist", storemembers); # endregion # region = get = // var members = redis. getallitemsfromlist ("additemtolist"); // members. foreach (S => console. writeline ("additemtolist:" + S); // console. writeline (redis. getitemfromlist ("additemtolist", 2); # endregion # region = Delete = // var list = redis. lists ["additemtolist"]; // list. clear (); // clear // list. remove ("two"); // remove the specified key value // list. removeat (2); // remove the data at the specified index position # endregion # region = Object = // redis. set ("userinfo", new userinfo () {username = "", age = 45}); // userinfo = redis. get <userinfo> ("userinfo"); // console. writeline (userinfo. username); // redis. set <int> ("my_age", 12); // or redis. set ("my_age", 12); // console. writeline (redis. get <int> ("my_age"); # endregion var SER = new objectserializer (); // located in namespace servicestack. redis. support; # region = serialization = // bool result = redis. set <byte []> ("userinfo", Ser. serialize (New userinfo () {username = "Zhang San", age = 12}); // userinfo = Ser. deserialize (redis. get <byte []> ("userinfo") as userinfo; // console. writeline (userinfo. username); # endregion // list is also supported // redis. set <byte []> ("userinfolist_serialize", Ser. serialize (userinfolist); # region = Server Load balancer = 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}); // obtain a link from the pool: Using (iredisclient redis2 = prcm. getclient () {redis2.set ("userinfolist", userinfolist); List <userinfo> userlist = redis2.get <list <userinfo> ("userinfolist") ;}# endregion console. readline ();} public static pooledredisclientmanager createmanager (string [] readwritehosts, string [] readonlyhosts) {// read/write splitting is supported, and the returned load is new pooledredisclientmanager (readwritehosts, new redisclientmanagerconfig {maxwritepoolsize = 5, // "write" link pool connection count maxreadpoolsize = 5, // "write" link pool connection count autostart = true ,});}

Collected from the Internet, only for your own data records.

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.