Redis Implement simple Queue _redis

Source: Internet
Author: User
Tags static class rabbitmq

In the work, often useful to the queue scene, the more common use of RABBITMQ these professional components, official website address is:http://www.rabbitmq.com, The important thing is that the official has. NET clients, but if not familiar with the RABBITMQ, it is recommended to use a Third-party packaged EASYNETQ,RABBITMQ more suitable for security, stability requirements, but sometimes we also have the requirements of this aspect is not very high scene, For example: The article reads the number, the real-time sex request is not very high place, therefore I thought of uses the Redis to do the queue.

The Redis list structure itself is a linked list (bidirectional linked list), so it conforms to our queue's FIFO requirements.

I use Stackexchange.redis this component to operate the Redis, used to use Service.Stack.Redis, and later this class library upgrade to the personal use of the number of restrictions, the need to pay to use to no call restrictions.

The operation Redis is simply encapsulated as follows:

public static class Redishelper
 {public
  static int i = 0;
  private static string redisconnectionstring = configurationmanager.appsettings["redisconnectionstring"]. ToString ();
 
  private static lazy<connectionmultiplexer> lazyconnection = new Lazy<connectionmultiplexer> (() =>
  {
   i++;
   Return Connectionmultiplexer.connect (redisconnectionstring);
  });
 
  public static Connectionmultiplexer Instance
  {
   get
   {return
    lazyconnection.value
   }
 }}

With this action class, we can manipulate the redis, and simply manipulate the list as follows:

<summary>
///Simple redis queue
///</summary> public
class Simpleredisqueue
{
 public void Leftpush (String key, String value)
 {
  var redis = RedisHelper.Instance.GetDatabase ();
  Redis. Listleftpush (key, value);
 
 
 public string Rightpop (string key)
 {
  var redis = RedisHelper.Instance.GetDatabase ();
  Return Redis. Listrightpop (key);
 }

The test code is as follows:

First you need a program that produces data

static void Main (string[] args) {System.Threading.Tasks.Task.Factory.StartNew () => {for (var i = 0; I & Lt 99999999; i++) {new WLX. SimpleUtil.Redis.SimpleRedisQueue ().
    Leftpush ("Test1", "A_" + i.tostring ());
 
 
   }
   }); System.Threading.Tasks.Task.Factory.StartNew (() => {for (var i = 0; i < 99999999; i++) {new WLX. SimpleUtil.Redis.SimpleRedisQueue ().
    Leftpush ("Test1", "b_" + i.tostring ());
   }
   }); System.Threading.Tasks.Task.Factory.StartNew (() => {for (var i = 0; i < 9999; i++) {new WLX. SimpleUtil.Redis.SimpleRedisQueue ().
    Leftpush ("Test1", "c_" + i.tostring ());
   }
   }); System.Threading.Tasks.Task.Factory.StartNew (() => {for (var i = 0; i < 99999999; i++) {new WLX. SimpleUtil.Redis.SimpleRedisQueue ().
    Leftpush ("Test1", "E_" + i.tostring ());
   }
   });
     System.Threading.Tasks.Task.Factory.StartNew (() => {for (var i = 0; i < 99999999; i++) {New WLX. SimpleUtil.Redis.SimpleRedisQueue ().
    Leftpush ("Test1", "f_" + i.tostring ());
   }
   });
  Console.readkey (); }

Then there is the program for the consumer queue:

static void Main (string[] args)
  {
   var queue = new Simpleredisqueue ();
   while (true)
   {
    var v = queue. Rightpop ("Test1");
    Console.WriteLine (v = null?) "Empty": v);
   }
  

Test Results screenshot

For applications where reliability and stability requirements are not high, simple and convenient implementations can be used Redis.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.