Redis Queue of C #

Source: Internet
Author: User

Summary

These two days have been thinking about Redis queue: A producer, multiple consumers, here's a demo to test.

An example

Refer to the previous article on how to refer to Redisclient: the Redis practice of C # list,hashtable

Producer a thread, and then turn on multiple threads to consume data.

The code is as follows:

UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;UsingSystem.Text;UsingSystem.Threading.Tasks;UsingNservicekit.redis;UsingNservicekit.serviceclient;UsingSystem.Threading;Namespaceredisdemo{Classprogram {Staticvoid Main (String[] args) {Thread thread =NewThread (run); Thread. Start (); thread[] Threads =New thread[10];for (int i =0; I < threads. Length; i++) {Threads[i] =NewThread (pull); Threads[i]. Start (); } console.read (); }PrivateStaticvoidPull () {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");//Suspends the current thread if the obtained content is empty 1sif (String. IsNullOrEmpty (Result)) {thread.spinwait (1000); }Else{Console.WriteLine ("Threadid:"+ Thread.CurrentThread.ManagedThreadId.ToString () +"\ t" +result); } }Else{//If the current queue is empty, suspend 1s thread.spinwait (1000); } } } }PrivateStaticvoid run () {iredisclientfactory factory = redisclientfactory.instance; using (iredisclient client = factory. Createredisclient ( "192.168.1.37  ", 6379 "wolfy" ; while (true "myqueue  "    

Test

Summarize

The queue has been considered rabbitmq,msmq and so on, considering that the company has a ready-made Redis server, so consider using Redis queues. Since the implementation of a producer, multiple consumers, then next, want to implement a multi-queue, and then set the capacity of the queue, through the capacity, the producer in the queue, depending on whether the queues are full, and then the data distribution situation.

Reprint: Blog Address: http://www.cnblogs.com/wolf-sun/

Redis Queue of C #

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.