. NET platform under Redis use (ii) "Stackexchange.redis learning" __.net

Source: Internet
Author: User
Tags redis
thought → idea → action → habit → personality → Destiny Program.cs Content:
Using Newtonsoft.json;
Using Stackexchange.redis;
Using System;
Using System.Data;
Using System.IO;

Using System.Runtime.Serialization.Formatters.Binary; Namespace Stackexchangeredis {class Program {static void Main (string[] args) {Conne
            Ctionmultiplexer Redis = Connectionmultiplexer.connect ("localhost:6379,password=123456"); Idatabase db = Redis.
            Getdatabase ();
            Long Count = 10000 * 10;

            DateTime start = DateTime.Now; #region Write method db for all Redis data.
            Stringset ("Key_test", "Shaocan"); Db.
            HashSet ("UserInfo", "name", "Shaocan"); Db.
            Setadd ("Set_test", "user1"); Db.
            Setadd ("Set_test", "user2"); Db.
            Sortedsetadd ("Sset_test", "user1", DateTime.Now.Ticks); Db.
            Sortedsetadd ("Sset_test", "User2", DateTime.Now.Ticks); Db.
            Listleftpush ("List_test", "user1");

            #endregion start = DateTime.Now; /* BinaryformAT///binary format for (int i = 0; i < count; i++) {User user = new use
                r {Id = i, Name = "Younameit" + I, age = 20};
                string key = "MyObject" + i;

                byte[] bytes; using (var stream = new MemoryStream ()) {new BinaryFormatter ().
                    Serialize (stream, user); bytes = stream.
                ToArray (); }//Set value//set key to save string value.
                If the key already has a value, it is overwritten regardless of its type. Db.
            Stringset (key, bytes); //Read 10w data for (int i = 0; i < count; i++) {string key = "MyOb
                Ject "+ i;
                User user = null; byte[] bytes = (byte[]) db.

                Stringget (key);
                    if (bytes!= null) {using (var stream = new MemoryStream (bytes))
               {//binary streams, deserializing         user = (user) new BinaryFormatter ().
                    Deserialize (stream); } Console.WriteLine (user.
            Name); } System.Console.WriteLine (String. Format ("Binary Format {0} items takes {1} seconds", Count, (Datetime.now-start).
            TotalSeconds));

            start = DateTime.Now; /* 100000 */for (int i = 0; i < count; i++) {User user = new User {Id = i, N
                Ame = "Geek Shrine" + I, age = 20};

                String json = jsonconvert.serializeobject (user);
                String key = "json" + i; Db.
            Stringset (key, JSON);  //Read 10W data for (int i = 0; i < count; i++) {string key = "JSON"
                + i; String json = db.
                Stringget (key);
                User user = (user) Jsonconvert.deserializeobject (JSON, typeof (user)); Console.WriteLine (user.
            Name);
         }   System.Console.WriteLine (String. Format ("JSON Format {0} items takes {1} seconds", Count, (Datetime.now-start).
            TotalSeconds));


            start = DateTime.Now;
            The serialized dataset is JSON.
            http://www.newtonsoft.com/json/help/html/SerializeDataSet.htm DataSet = new DataSet ("DataSet");
            Dataset.namespace = "NETFramework";
            DataTable table = new DataTable ();
            DataColumn idcolumn = new DataColumn ("id", typeof (int));

            Idcolumn.autoincrement = true;
            DataColumn itemcolumn = new DataColumn ("item"); Table.
            Columns.Add (Idcolumn); Table.
            Columns.Add (Itemcolumn);

            DATASET.TABLES.ADD (table); for (int i = 0; i < 2; i++) {DataRow newrow = table.
                NewRow ();
                newrow["item"] = "[Test]" + i; Table.
            Rows.Add (NewRow);

            } dataset.acceptchanges (); String _json = JsonconvErt.
            SerializeObject (DataSet, formatting.indented); Sets the DataSet1 value db.

            Stringset ("DataSet1", _json);
            DataSet ds = (DataSet) Jsonconvert.deserializeobject (_json, typeof (DataSet)); Console.WriteLine (ds. Tables[0]. rows[0]["Item"].

            ToString ());
        System.Console.ReadLine ();
        } [Serializable] public class User {public long Id {get; set;}
        public string Name {get; set;}
    public int Age {get; set;} }
}
run the results as shown in figure:

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.