C#用redis實現消息隊列.net core

在nuget中先安裝CSRedisCore組件

using CSRedis;
using System;

namespace ConsoleAppTestRedisQueue
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionStr = "192.168.31.42:6380,password=pisenmaster,defaultDatabase=5,poolsize=50,ssl=false,writeBuffer=10240";
            var redisClient = new CSRedisClient(connectionStr);
            //redisClient.Set("K100", "測試值",30);  
            for (int i = 0; i < 10; i++)
            {
                //消息插入
                redisClient.LPush("K200", "測試值" + i);
            }
          
            //消息取出,處理
            while (true)
            {
                string value = redisClient.RPopLPush("K200", "K200_destination");
                if (value != null)
                {
                    Console.WriteLine("正在處理消息:"+value);
                }
            }
            Console.ReadKey();
        }
    }
}

你可能感兴趣的:(C#,Redis)