Consistent String#hash based only on the string's content

后端 未结 4 897
南方客
南方客 2020-12-15 16:06

GOAL: Map every URL handled by a server to 0, 1, 2, or 3, distributing as uniformly as possible.

While the documentation for ruby\'s String#hash met

4条回答
  •  既然无缘
    2020-12-15 16:36

    The easiest (and consistent) way may be this (and it's fast):

    "https://www.example.com/abc/def/123?hij=345".sum % 4
    

    That will always produce an integer 0 - 3, is quite fast, and should be fairly well distributed (though I haven't actually run tests on distribution).

提交回复
热议问题