【发布时间】:2012-11-05 01:28:49
【问题描述】:
我的网站使用来自单个域的各种资源,例如:
http://static.example.com/javascript/common.js
http://static.example.com/javascript/common.css
http://static.example.com/javascript/menu/menu.js
http://static.example.com/javascript/menu/menu.css
http://static.example.com/images/1804/logo/02000100.jpg
http://static.example.com/images/1804/headers/main/09400060.png
http://static.example.com/images/1804/headers/home/1101/06900200-01.jpg
http://static.example.com/images/1804/headers/home/1101/06900200-02.jpg
我需要一个非常简单的字符串散列函数,将这些 URL 映射到数字,数字是 0、1、2 和 3。算法应该是确定性和统一的。我已将问题标记为 PHP,但可以接受通用答案。
你可能已经猜到我为什么需要这个了;我打算将 URL 更改为,例如:
http://0.static.example.com/javascript/common.js
http://2.static.example.com/javascript/common.css
【问题讨论】:
-
我要说
strlen($str) % $num...它肯定很快:) -
是快速但不统一...查看包含单词 headers 的 url?我将在一个页面上有大约 15 个,因此 20 个 URL 中有 15 个将具有哈希 = 3。
-
是的,这无疑更适用于更像人类的图像名称:)