C#/.NET 系统优化专题(redis第六篇 数据结构【List】)

RedisListService 类请参考第一篇的封装

redis 数据结构List的使用

using (RedisListService service = new RedisListService())
{
    service.FlushAll();

    service.Add("article", "XT1234");
    service.Add("article", "kevin");
    service.Add("article", "大叔");
    service.Add("article", "C卡");
    service.Add("article", "触不到的线");
    service.Add("article", "程序错误");

    var result1 = service.Get("article");
    var result2 = service.Get("article", 0, 3);
    可以按照添加顺序自动排序;而且可以分页获取

    //栈 
    service.FlushAll();

    service.Add("article", "eleven1234");
    service.Add("article", "kevin");
    service.Add("article", "大叔");
    service.Add("article", "C卡");
    service.Add("article", "触不到的线");
    service.Add("article", "程序错误");

    for (int i = 0; i < 5; i++)
    {
        Console.WriteLine(service.PopItemFromList("article"));
        var result1 = service.Get("article");
    }

    // 队列:生产者消费者模型
    service.FlushAll();
    service.RPush("article", "XT1234");
    service.RPush("article", "kevin");
    service.RPush("article", "大叔");
    service.RPush("article", "C卡");
    service.RPush("article", "触不到的线");
    service.RPush("article", "程序错误");

    for (int i = 0; i < 5; i++)
    {
        Console.WriteLine(service.PopItemFromList("article"));
        var result1 = service.Get("article");
    }
    //分布式缓存,多服务器都可以访问到,多个生产者,多个消费者,任何产品只被消费一次
}          

案例

#region 生产者消费者
using (RedisListService service = new RedisListService())
{
    List<string> stringList = new List<string>();
    for (int i = 0; i < 10; i++)
    {
        stringList.Add(string.Format($"放入任务{i}"));
    }

    service.Add("test", "这是一个学生Add1");
    service.Add("test", "这是一个学生Add2");
    service.Add("test", "这是一个学生Add3");

    service.LPush("test", "这是一个学生LPush1");
    service.LPush("test", "这是一个学生LPush2");
    service.LPush("test", "这是一个学生LPush3");
    service.LPush("test", "这是一个学生LPush4");
    service.LPush("test", "这是一个学生LPush5");
    service.LPush("test", "这是一个学生LPush6");

    service.RPush("test", "这是一个学生RPush1");
    service.RPush("test", "这是一个学生RPush2");
    service.RPush("test", "这是一个学生RPush3");
    service.RPush("test", "这是一个学生RPush4");
    service.RPush("test", "这是一个学生RPush5");
    service.RPush("test", "这是一个学生RPush6");
    service.Add("task", stringList);

    Console.WriteLine(service.Count("test"));
    Console.WriteLine(service.Count("task"));
    var list = service.Get("test");
    list = service.Get("task", 2, 4);

    Action act = new Action(() =>
    {
        while (true)
        {
            Console.WriteLine("************请输入数据**************");
            string testTask = Console.ReadLine();
            service.LPush("test", testTask);
        }
    });
    act.EndInvoke(act.BeginInvoke(null, null));
}
#endregion
//取数据
public static void Show()
{
    string path = AppDomain.CurrentDomain.BaseDirectory;
    string tag = path.Split('/', '\\').Last(s => !string.IsNullOrEmpty(s));
    Console.WriteLine($"这里是 {tag} 启动了。。");
    using (RedisListService service = new RedisListService())
    {
        Action act = new Action(() =>
        {
            while (true)
            {
                var result = service.BlockingPopItemFromLists(new string[] { "test", "task" }, TimeSpan.FromHours(3));
                Thread.Sleep(100);
                Console.WriteLine($"这里是 {tag} 队列获取的消息 {result.Id} {result.Item}");
            }
        });
        act.EndInvoke(act.BeginInvoke(null, null));
    }
}
#region 发布订阅:观察者,一个数据源,多个接受者,只要订阅了就可以收到的,能被多个数据源共享
Task.Run(() =>
{
    using (RedisListService service = new RedisListService())
    {
        service.Subscribe("XT", (c, message, iRedisSubscription) =>
        {
            Console.WriteLine($"注册{1}{c}:{message},Dosomething else");
            if (message.Equals("exit"))
                iRedisSubscription.UnSubscribeFromChannels("XT");
        });//blocking
    }
});
Task.Run(() =>
{
    using (RedisListService service = new RedisListService())
    {
        service.Subscribe("XT", (c, message, iRedisSubscription) =>
        {
            Console.WriteLine($"注册{2}{c}:{message},Dosomething else");
            if (message.Equals("exit"))
                iRedisSubscription.UnSubscribeFromChannels("XT");
        });//blocking
    }
});
Task.Run(() =>
{
    using (RedisListService service = new RedisListService())
    {
        service.Subscribe("Twelve", (c, message, iRedisSubscription) =>
        {
            Console.WriteLine($"注册{3}{c}:{message},Dosomething else");
            if (message.Equals("exit"))
                iRedisSubscription.UnSubscribeFromChannels("Twelve");
        });//blocking
    }
});
using (RedisListService service = new RedisListService())
{
    Thread.Sleep(1000);

    service.Publish("XT", "Eleven123");
    service.Publish("XT", "Eleven234");
    service.Publish("XT", "Eleven345");
    service.Publish("XT", "Eleven456");

    service.Publish("Twelve", "Twelve123");
    service.Publish("Twelve", "Twelve234");
    service.Publish("Twelve", "Twelve345");
    service.Publish("Twelve", "Twelve456");
    Console.WriteLine("**********************************************");

    service.Publish("XT", "exit");

    service.Publish("XT", "123Eleven");
    service.Publish("XT", "234Eleven");
    service.Publish("XT", "345Eleven");
    service.Publish("XT", "456Eleven");

    service.Publish("Twelve", "exit");
    service.Publish("Twelve", "123Twelve");
    service.Publish("Twelve", "234Twelve");
    service.Publish("Twelve", "345Twelve");
    service.Publish("Twelve", "456Twelve");
}
#endregion

你可能感兴趣的:(.Net通用工具)