NoSql学习之路一redis做消息队列

来源:互联网 发布:软件质量分析师 编辑:程序博客网 时间:2024/04/30 07:17

nosql最近很火,我也研究研究,呵呵,今天来说说redis,简单的那些增删改查 就不说了,网上太多了,我研究了一下,redis做消息队列

首先是用的redis的发布订阅做的消息队列实现消息队列,

测试是在本机做的,redis在虚拟机2003的系统,386M内存,本机是8.1的系统,6g内存

我是用了500个线程模拟并发每个线程发布500条数据,在发布消息的时候,服务器的内存占用并不高,cpu占有率挺高的

这是每个线程用的时间,都差不多3 4秒


另一端用sqlserver保存消息,结果插到24万左右的时候报错了,原来是日志文件太大了,超过1G了,而数据库文件只有50M,时间大概用了20多分钟吧,

今天又用mongodb试了一下,效率比sqlserver高很多,只用了56秒就ok,对了我保存消息的时候,只用一个线程

下面上代码

发布端

namespace publish{    class Program    {        static void Main(string[] args)        {            for (int b = 0; b < 500; b++)            {                Thread thread = new Thread(new ThreadStart(() =>                   {                       RedisClient redisClient = RedisClientFactory.Instance.CreateRedisClient("192.168.189.134", 6379);                       Stopwatch stopwatch1 = new Stopwatch();                       stopwatch1.Start();                       for (int i = 0; i < 500; i++)                       {                           UserInfo userInfo = new UserInfo();                           userInfo.Age = 1000;                           userInfo.Name = "XX";                           userInfo.I = i;                           userInfo.ThreadId = Thread.CurrentThread.ManagedThreadId;                           redisClient.Publish("aa", Encoding.Default.GetBytes("XX在广播 频道:aa线程:"+Thread.CurrentThread.ManagedThreadId.ToString()));                       }                       stopwatch1.Stop();                       Console.WriteLine("aa用时:" + stopwatch1.ElapsedMilliseconds + "毫秒");                   }));                thread.Start();            }            Console.ReadKey();        }    }    class UserInfo    {        public string Name { get; set; }        public int Age { get; set; }        public int ThreadId { get; set; }        public int I { get; set; }    }}

接受端,

 class Program    {        static void Main(string[] args)        {            RedisClient Redis = RedisClientFactory.Instance.CreateRedisClient("192.168.189.134", 6379);//redis服务IP和端口            Redis.Subscribe(new string[] { "it", "aa" });            Thread thread = new Thread(() =>            {                Recode recode = new Recode();                while (true)                {                    byte[][] bb =                         Redis.ReceiveMessages();                    Mongo.Insert(new r()                    {                        message1 = Encoding.Default.GetString(bb[0]),                        message2 = Encoding.Default.GetString(bb[1]),                        message3 = Encoding.Default.GetString(bb[2]),                        date=DateTime.Now                    });                }            });            thread.Start();            Console.WriteLine("OK");            Console.ReadKey();        }

sqlserver保存方法

 public void Insert(string message1, string message2, string message3)        {            using (SqlConnection connection = new SqlConnection("server=localhost;database=ThreeLayer;Integrated Security=true"))            {                connection.Open();                using (SqlCommand command = new SqlCommand("insert into redis(Date,Message1 , Message2, Message3) values(@d,@m1,@m2,@m3)", connection))                {                    command.Parameters.Add(new SqlParameter("@d", DateTime.Now));                    command.Parameters.Add(new SqlParameter("@m1", message1));                    command.Parameters.Add(new SqlParameter("@m2", message2));                    command.Parameters.Add(new SqlParameter("@m3", message3));                    command.ExecuteScalar();                }            }        }

mongodb保存

  public static void Insert(r r)        {            string connStr = ConfigurationManager.AppSettings["MongoServerSettings"];//获取连接字符串            MongoServer _server = MongoServer.Create(connStr);//创建mongodb服务对应的对象            MongoDatabase _db = _server.GetDatabase("test2");//获取数据库,如果没有,会自动创建一个            var collectionName = typeof(r).Name;//指定集合的名字             var collection = _db.GetCollection<r>(collectionName);//获取集合,如果集合不存在,那么直接创建一个            collection.Insert(r);//将数据插入到 集合里面去}

今天到此结束,欢迎小伙伴,提意见


3 0
原创粉丝点击