c# - BookSleeve BlockingRemoveLeft 只返回一些插入列表的项目

标签 c# redis booksleeve

我正在尝试根据 BookSleeve 组合一个 super 简单的排队示例。

到目前为止,这是我在队列消费者方面所拥有的:

using System;
using System.Text;
using BookSleeve;

static class Program
{
    static void Main()
    {
        using (var redis = new RedisConnection("127.0.0.1"))
        {
            redis.Open();
            while (true)
            {
                // BRPOP the queue
                var t = redis.Lists.BlockingRemoveFirst(0, new[] { "test" }, 0);
                t.Wait();
                var val = Encoding.UTF8.GetString(t.Result.Item2);
                Console.WriteLine("Got: {0}", val);
            }
        }
    }
}

我在 LINQPad 中运行以下内容作为制作人:

using(var redis = new RedisConnection("localhost")) 
{
    redis.Open();
    foreach(var i in Enumerable.Range(0, 10)) 
    {
        // LPUSH
        redis.Lists.AddFirst(0, "test", "foo" + i)
            // Call wait to keep things in order
            .Wait();
    }   

    Thread.Sleep(500); // Let Redis do it's thing
    Console.WriteLine("queue length: " + redis.Lists.GetLength(0, "test").Result);
}

虽然我得到了一些非常奇怪的结果:

第一次运行:

Got: foo2
Got: foo5
Got: foo7
Got: foo9

第二次运行:

Got: foo1
Got: foo4
Got: foo7

第三轮:

Got: foo0
Got: foo3
Got: foo6
Got: foo8

此外,每次运行后,LINQPad 输出:queue length: 0,使用实际的 redis 客户端运行 LLEN test 返回 (integer) 0.

我忍不住觉得我在这里遗漏了一些东西,很可能与异步的东西有关,但我就是看不到它。

我的redis版本是2.8.3,BookSleeve版本是1.3.41。

这里的实际问题是:为什么 BookSleeve 只返回发送到 redis 列表的消息的一个子集?

最佳答案

我根本无法让它出错。您绝对确定您只有 1 个消费者吗?请注意,order 不可预测的原因是因为您将它用作堆栈而不是队列 - 要获得可靠的顺序,您应该添加到开头并从结尾删除,或者添加到结束并从头开始删除。如果您从头开始添加和删除,它是随机顺序。

但是,它对我来说效果很好。控制台输出:

Got: foo0
Got: foo1
Got: foo3
Got: foo5
Got: foo6
Got: foo8
Got: foo9
Got: foo7
Got: foo4
Got: foo2
queue length: 0

我建议在 monitor 模式下使用 redis-cli - 对我来说,我得到以下输出:

redis 127.0.0.1:6379> monitor
OK
1389454168.068869 [0 127.0.0.1:4957] "INFO"
1389454168.068869 [0 127.0.0.1:4957] "CONFIG" "GET" "timeout"
1389454168.068869 [0 127.0.0.1:4957] "DEL" "test"
1389454168.129869 [0 127.0.0.1:4958] "INFO"
1389454168.129869 [0 127.0.0.1:4958] "CONFIG" "GET" "timeout"
1389454168.136869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.139872 [0 127.0.0.1:4959] "INFO"
1389454168.139872 [0 127.0.0.1:4959] "CONFIG" "GET" "timeout"
1389454168.139872 [0 127.0.0.1:4959] "LPUSH" "test" "foo0"
1389454168.142869 [0 127.0.0.1:4959] "LPUSH" "test" "foo1"
1389454168.142869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.142869 [0 127.0.0.1:4959] "LPUSH" "test" "foo2"
1389454168.143871 [0 127.0.0.1:4959] "LPUSH" "test" "foo3"
1389454168.143871 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.143871 [0 127.0.0.1:4959] "LPUSH" "test" "foo4"
1389454168.143871 [0 127.0.0.1:4959] "LPUSH" "test" "foo5"
1389454168.144870 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.144870 [0 127.0.0.1:4959] "LPUSH" "test" "foo6"
1389454168.144870 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.144870 [0 127.0.0.1:4959] "LPUSH" "test" "foo7"
1389454168.145869 [0 127.0.0.1:4959] "LPUSH" "test" "foo8"
1389454168.145869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.145869 [0 127.0.0.1:4959] "LPUSH" "test" "foo9"
1389454168.145869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.146869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.146869 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.147870 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.147870 [0 127.0.0.1:4958] "BLPOP" "test" "0"
1389454168.648594 [0 127.0.0.1:4959] "LLEN" "test"

此处连接 4957 只是删除 key (以确保已知状态); 4958是消费者,4959是生产者。

作为引用,我使用的是 2.6.12 Windows build在上面的测试中(因为我在我的 Windows 笔记本电脑上,没有时间设置 linux VM)- 但是:我希望在官方 linux 构建上也是如此。

再说一遍:您确定(通过redis-cli)这里只涉及 2 个连接吗?

如果我更改为 AddLast:

Got: foo0
Got: foo1
Got: foo2
Got: foo3
Got: foo4
Got: foo5
Got: foo6
Got: foo7
Got: foo8
Got: foo9
queue length: 0

关于c# - BookSleeve BlockingRemoveLeft 只返回一些插入列表的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21059099/

相关文章:

php - 如果将键值设置为 NULL,PHP-Redis 在 PHP 5.6 中崩溃是已知错误吗?

java - 如何使用 Spring-data-Redis 实现 Redis Multi-Exec

c# - PDF 文件是否包含 iref 流?

c# - 如何从动态变量中检索值

spring-boot - 用于 Spring boot redis 集成测试的可靠库

booksleeve - 使用 BookSleeve 保存对象集合

c# - Redis Booksleeve、HGETALL 和将字节数组转换为长

c# - 如何让 Web Api 将 Json.net 序列化字符串对象正确发送回客户端?

c# - 数组可以容纳的最大大小是多少?

c# - 如何在 VSTO 中使用 Find.HitHighlight 突出显示相同的范围?