一致的String#哈希仅基于字符串的内容 [英] Consistent String#hash based only on the string's content
问题描述
$ irb
ruby-1.9.2-p180 :001> foo.hash
=> 360517580588231756
ruby-1.9.2-p180:002> ^ D
$ irb
ruby-1.9.2-p180:001> foo.hash
=> -2716152678666510148
这意味着特定字符串的哈希值可能在所有服务器上不同。 Rails内部使用 String#hash
将URL路径映射到四个资产主机之一(如果应用程序的asset_host是 soso ),但由于跨机器的不一致性,此功能的效率远低于它。不同的服务器可能会将相同的URL映射到不同的资产主机,从而降低高速缓存的效率,混浊的天空,过早冷却茶杯,污染其他优秀程序员的声誉。你能否提出一个替代的散列函数,可以在一个典型的应用程序的URL空间中快速有效地散发散列,最好是一个产生Fixnum的散列函数,因为最后我想将它映射到四个资产主机之一中?
在ruby的摘要模块中有很多这样的功能: http://ruby-doc.org/stdlib/libdoc/digest/rdoc/index.html
简单的例子:
require'digest / sha1'
Digest :: SHA1.hexdigest some string)
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 method says it will "return a hash based on the string‘s length and content," this clearly isn't the whole story. A given string's hash is not consistent across invocations of the interpreter:
$ irb
ruby-1.9.2-p180 :001 > "foo".hash
=> 360517580588231756
ruby-1.9.2-p180 :002 > ^D
$ irb
ruby-1.9.2-p180 :001 > "foo".hash
=> -2716152678666510148
This means a particular string's hash value may differ across, say, servers. Rails uses String#hash
internally to map a URL path to one of four asset hosts (if the app's asset_host is so configured), but this feature is a lot less efficient than it could be because of the cross-machine inconsistencies; different servers may map the same URL to different asset hosts, reducing the effectiveness of caches, clouding skies, cooling cups of tea prematurely, besmirching the reputations of otherwise fine programmers.
Can you suggest an alternate hash function that could effectively and speedily distribute hashes across a typical app's URL space, preferably one that produces a Fixnum since, in the end, I'll want to map it into one of four asset hosts?
there are lot of such functionality in ruby's digest module: http://ruby-doc.org/stdlib/libdoc/digest/rdoc/index.html
simple example:
require 'digest/sha1'
Digest::SHA1.hexdigest("some string")
这篇关于一致的String#哈希仅基于字符串的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!