c#之Redis隊列

來源:互聯網
上載者:User

標籤:tar   rabbit   for   分發   tor   .text   spi   1.3   參考   

摘要

這兩天一直在考慮redis隊列:一個生產者,多個消費者的情況,這裡弄了一個demo進行測試。

一個例子

關於如何引用Redisclient 可以參考之前的這篇文章:c#之Redis實踐list,hashtable

生產者一個線程,然後開啟多個線程用來消費資料。

代碼如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using NServiceKit.Redis;using NServiceKit.ServiceClient;using System.Threading;namespace RedisDemo{    class Program    {        static void Main(string[] args)        {            Thread thread = new Thread(run);            thread.Start();            Thread[] threads = new Thread[10];            for (int i = 0; i < threads.Length; i++)            {                threads[i] = new Thread(Pull);                threads[i].Start();            }            Console.Read();        }        private static void Pull()        {            IRedisClientFactory factory = RedisClientFactory.Instance;            using (IRedisClient client = factory.CreateRedisClient("192.168.1.37", 6379))            {                client.Password = "wolfy";                while (true)                {                    if (client.GetListCount("Myqueue") > 0)                    {                        string result = client.DequeueItemFromList("Myqueue");                        //如果擷取的內容為空白,將當前線程掛起1s                        if (string.IsNullOrEmpty(result))                        {                            Thread.SpinWait(1000);                        }                        else                        {                            Console.WriteLine("Threadid:" + Thread.CurrentThread.ManagedThreadId.ToString() + "\t" + result);                        }                    }                    else                    {                        //如果當前隊列為空白,掛起1s                        Thread.SpinWait(1000);                    }                }            }        }        private static void run()        {            IRedisClientFactory factory = RedisClientFactory.Instance;            using (IRedisClient client = factory.CreateRedisClient("192.168.1.37", 6379))            {                client.Password = "wolfy";                while (true)                {                    client.EnqueueItemOnList("Myqueue", DateTime.Now.ToString());                }            }        }    }}

測試

總結

關於隊列有考慮過rabbitmq,msmq等,考慮到公司有現成的redis伺服器,所以就考慮使用redis隊列。既然實現了一生產者,多個消費者,那麼接下來,想實現一種多隊列,然後設定隊列的容量,通過容量,生產者在入隊的時候,根據隊列是否滿,然後對資料進行分發的情況。

轉載:部落格地址:http://www.cnblogs.com/wolf-sun/

c#之Redis隊列

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.