C# Redis消息队列例子

 1     class Program
 2     {
 3 
 4         //版本2:使用Redis的客户端管理器(对象池)
 5         public static IRedisClientsManager redisClientManager = new PooledRedisClientManager(new string[]
 6         {
 7       //如果是Redis集群则配置多个{IP地址:端口号}即可
 8       //例如: "10.0.0.1:6379","10.0.0.2:6379","10.0.0.3:6379"
 9       "127.0.0.1:6379"
10         });
11         //从池中获取Redis客户端实例
12         public static IRedisClient redisClient = redisClientManager.GetClient();
13         static void Main(string[] args)
14         {
15             //  redisClient.Password = "123";
16             redisClient.EnqueueItemOnList("test", "Hello World!");
17             redisClient.EnqueueItemOnList("test", "Hello World2!");
18 
19             Timer t = new Timer((o) =>
20             {
21                 var value = redisClient.DequeueItemFromList("test");
22                 if (string.IsNullOrWhiteSpace(value))
23                 {
24                     Console.WriteLine("队列中数据不存在!");
25                 }
26                 else
27                 {
28                     Console.WriteLine(value);
29                 }
30           
31             }, null, 5000, 5000);
32             
33 
34             Console.Read();
35 
36         }
37     }

运行结果

 备注:Redis驱动版本:4.0.50.0

原文地址:https://www.cnblogs.com/gaobing/p/5075028.html