课件。当散列键为GUID时,Redis HGET/HGETASYNC返回null。

时间:2021-11-24 03:17:37

I'm experiencing some issue and I believe it might be a bug in StackExchange.Redis library.

我正在经历一些问题,我相信这可能是StackExchange中的一个bug。复述,图书馆。

When I create a hash where keys are GUID, both .HashGet or HashGetAsync will return a null value, while when I try to get the same hash key using redis-cli I'm able to retrieve the hash value successfully.

当我创建一个散列,其中键是GUID时,. hashget或HashGetAsync将返回一个空值,而当我尝试使用redis-cli获取相同的散列键时,我能够成功地检索散列值。

1 个解决方案

#1


1  

From your comments, the guid here seems to be the hash field sub-key. Fundamentally, it works fine; see:

从您的注释中,guid似乎是散列字段的子键。从根本上说,它工作正常;看到的:

static void Main()
{
    int i = new Random().Next();
    Console.WriteLine("> {0}", i);
    Guid guid = Guid.NewGuid();
    using (var muxer = ConnectionMultiplexer.Connect("127.0.0.1:6379"))
    {

        var db = muxer.GetDatabase();
        db.KeyDelete("foo");
        db.HashSet("foo", guid.ToByteArray(), i);
    }
    using (var muxer = ConnectionMultiplexer.Connect("127.0.0.1:6379"))
    {
        var db = muxer.GetDatabase();
        var val = (int)db.HashGet("foo", guid.ToByteArray());
        Console.WriteLine("< {0}", val);
    }
}

which should show the same randomly generated number twice (once as it sets it, once as it fetches it).

它应该显示相同的随机生成的数字两次(一次是它设置的,一次是它获取它)。

I can only assume that this is an expectation failure; for example:

我只能假设这是一个期望失败;例如:

  • you are using strings in one place and binary in another (they will not be equal)
  • 你在一个地方使用字符串,在另一个地方使用二进制(它们不会相等)
  • you are using different formatting (case, spacing, hyphens, etc) rules in the two places (they will not be equal)
  • 在两个地方使用不同的格式(例如,案例、间隔、连字符等)规则(它们将不相等)
  • you are falling foul of the "crazy endianness" of Guid.ToByteArray(), which does not output what people usually expect.
  • 你正在与“疯狂的意外”指南(Guid.ToByteArray())相冲突,它没有输出人们通常期望的东西。

You can see this last scenario here:

你可以在这里看到最后一个场景:

Guid guid = new Guid("00112233-4455-6677-8899-AABBCCDDEEFF");
var arr = guid.ToByteArray();
for(int i = 0 ; i < arr.Length; i++)
{
    if (i == 4 || i == 6 || i == 8 || i == 10) Console.Write('-');
    Console.Write(arr[i].ToString("X2"));
}
Console.WriteLine();

which actually outputs:

实际输出:

33221100-5544-7766-8899-AABBCCDDEEFF

Note that the first, second and third groups are reversed. If this is the problem, then blame ToByteArray()! However, I strongly suspect the first two are more likely. To investigate, the best thing to do would be to run redis-cli in monitor mode while you compare what the two different systems are doing.

请注意,第一、第二和第三组是相反的。如果这是问题所在,那就责怪ToByteArray()吧!然而,我强烈怀疑前两种可能性更大。要进行调查,最好的方法是在您比较两种不同系统的情况时,在monitor模式下运行redis-cli。

#1


1  

From your comments, the guid here seems to be the hash field sub-key. Fundamentally, it works fine; see:

从您的注释中,guid似乎是散列字段的子键。从根本上说,它工作正常;看到的:

static void Main()
{
    int i = new Random().Next();
    Console.WriteLine("> {0}", i);
    Guid guid = Guid.NewGuid();
    using (var muxer = ConnectionMultiplexer.Connect("127.0.0.1:6379"))
    {

        var db = muxer.GetDatabase();
        db.KeyDelete("foo");
        db.HashSet("foo", guid.ToByteArray(), i);
    }
    using (var muxer = ConnectionMultiplexer.Connect("127.0.0.1:6379"))
    {
        var db = muxer.GetDatabase();
        var val = (int)db.HashGet("foo", guid.ToByteArray());
        Console.WriteLine("< {0}", val);
    }
}

which should show the same randomly generated number twice (once as it sets it, once as it fetches it).

它应该显示相同的随机生成的数字两次(一次是它设置的,一次是它获取它)。

I can only assume that this is an expectation failure; for example:

我只能假设这是一个期望失败;例如:

  • you are using strings in one place and binary in another (they will not be equal)
  • 你在一个地方使用字符串,在另一个地方使用二进制(它们不会相等)
  • you are using different formatting (case, spacing, hyphens, etc) rules in the two places (they will not be equal)
  • 在两个地方使用不同的格式(例如,案例、间隔、连字符等)规则(它们将不相等)
  • you are falling foul of the "crazy endianness" of Guid.ToByteArray(), which does not output what people usually expect.
  • 你正在与“疯狂的意外”指南(Guid.ToByteArray())相冲突,它没有输出人们通常期望的东西。

You can see this last scenario here:

你可以在这里看到最后一个场景:

Guid guid = new Guid("00112233-4455-6677-8899-AABBCCDDEEFF");
var arr = guid.ToByteArray();
for(int i = 0 ; i < arr.Length; i++)
{
    if (i == 4 || i == 6 || i == 8 || i == 10) Console.Write('-');
    Console.Write(arr[i].ToString("X2"));
}
Console.WriteLine();

which actually outputs:

实际输出:

33221100-5544-7766-8899-AABBCCDDEEFF

Note that the first, second and third groups are reversed. If this is the problem, then blame ToByteArray()! However, I strongly suspect the first two are more likely. To investigate, the best thing to do would be to run redis-cli in monitor mode while you compare what the two different systems are doing.

请注意,第一、第二和第三组是相反的。如果这是问题所在,那就责怪ToByteArray()吧!然而,我强烈怀疑前两种可能性更大。要进行调查,最好的方法是在您比较两种不同系统的情况时,在monitor模式下运行redis-cli。