中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

Redis學(xue)習筆(bi)記~實(shi)現消(xiao)息隊(dui)列比MSMQ更(geng)方便

回到目錄

什么(me)是(shi)隊(dui)列(lie):簡單(dan)的(de)說就是(shi)數據存(cun)儲(chu)(chu)(chu)到一個空間里(可(ke)(ke)以是(shi)內存(cun),也(ye)可(ke)(ke)以是(shi)物理(li)文(wen)件),先存(cun)儲(chu)(chu)(chu)的(de)數據對象,先被取出來,這與(yu)堆(dui)棧正好相反,消息(xi)隊(dui)列(lie)也(ye)是(shi)這樣,將可(ke)(ke)能出現高并發的(de)數據進行(xing)隊(dui)列(lie)存(cun)儲(chu)(chu)(chu),并按(an)著入隊(dui)的(de)順(shun)序依(yi)次處理(li),實現消息(xi)隊(dui)列(lie)的(de)工具有很(hen)多(duo),如微軟(ruan)的(de)MSMQ,及(ji)一些(xie)開(kai)源(yuan)的(de)KV存(cun)儲(chu)(chu)(chu)工具,今(jin)天主要介紹用Redis實現消息(xi)隊(dui)列(lie)。

這是我(wo)的(de)redis項目結構

image

redis服務(wu)有(you)一個console的程序,可以支持在windows和linux下運行。

我用(yong)MVC應用(yong)程(cheng)序來作這個例子,由(you)表(biao)單向(xiang)內存(cun)中(zhong)寫信息,然后每(mei)5秒中(zhong)從內存(cun)中(zhong)將消息取出來,看代(dai)碼

/// <summary>
  /// 消息對象(xiang)類型
  /// </summary>
  public class MessageQuene
  {
      static System.Timers.Timer timer = new System.Timers.Timer(5000);
      public static ChatModels CurrentChatModels = new ChatModels();
      static Redis.Utils.RedisClient redisClient;
      static MessageQuene()
      {
          redisClient = new Redis.Utils.RedisClient();
          timer.AutoReset = true;
          timer.Enabled = true;
          timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);//subscribe a event
          timer.Start();

      }
      private static void timer_Elapsed(object sender, ElapsedEventArgs e)
      {
          CurrentChatModels = (ChatModels)redisClient.LeftPop("MessageQuene");
      }
  }

前臺顯(xian)示的action

public ActionResult Index()
 {
     ViewData["pop"] = MessageQuene.CurrentChatModels == null ? "沒?有D記?錄?" : MessageQuene.CurrentChatModels.Chat;
     ViewData["MSMQ"] = redisClient.ListRange("MessageQuene") == null
       ? new List<ChatModels>()
       : redisClient.ListRange("MessageQuene").Cast<ChatModels>().ToList();

}

表(biao)單提交(jiao)的action

image

事件上,如果我們在項目中用到消息隊列時,可以直接使用ViewData["pop"]這(zhe)個(ge)對象(xiang),它(ta)就是當前取出的隊列元(yuan)素(su),我(wo)們可以對它(ta)進行數據操作等。

回到目錄

posted @ 2012-04-12 12:01  張占嶺  閱讀(21062)  評論(4)    收藏  舉報