.NET平台下Redis使用(二)【StackExchange.Redis学习】

思想→观念→行动→习惯→个性→命运

Program.cs内容:


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)
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost:6379,password=123456");
            IDatabase db = redis.GetDatabase();
            long count = 10000 * 10;
            DateTime start = DateTime.Now;

            #region 所有Redis的数据写入方法
            db.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 */
            //二进制格式
            for (int i = 0; i < count; i++)
            {
                User user = new User { 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键保存字符串值。如果key已经拥有一个值,它被覆盖,而不管其类型。
                db.StringSet(key, bytes);
            }


            //读取10w条数据
            for (int i = 0; i < count; i++)
            {
                string key = "myObject" + i;
                User user = null;
                byte[] bytes = (byte[])db.StringGet(key);

                if (bytes != null)
                { 
                    using (var stream = new MemoryStream(bytes))
                    {
                        //二进制流,反序列化
                        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, Name = "极客神殿" + i, Age = 20 };
                string json = JsonConvert.SerializeObject(user);

                string key = "json" + i;
                db.StringSet(key, json);
            }

            //读取10W条数据
            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;


            //序列化DataSet为JSON。
            //http://www.newtonsoft.com/json/help/html/SerializeDataSet.htm
            DataSet 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"] = "[测试] " + i;
                table.Rows.Add(newRow);
            }

            dataSet.AcceptChanges();

            string _json = JsonConvert.SerializeObject(dataSet, Formatting.Indented);
            //设置dataset1值
            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; }
    }
}

运行结果如图:

这里写图片描述


这里写图片描述


这里写图片描述


这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值