How to create a group for a string / filename?
OK, I am a newbee to stackoverflow.
Developing an ecommerce 开发者_如何学Csite, I have as many as 50 unique images on the home page that needs to be loaded. I am using the Amazon CDN for my images and all these files are in a bucket with a unique domain.
I would like to have multiple domains mapped to this bucket. However for each image, I should be able to find at runtime, which domain it was servered last time so that the caching is most optimized. My idea is to have a function func(filename) which can return a value between 0-9 every-time for the same filename. This can be used for the domain name.
I do not want the func to be very heavy like a hash and in this case, I would want a collision rather than avoid it.
A simple method would be to use a intval(filename) and then use the least significant digit. However I am not sure this would be a good solution and if the spread would be balanced.
Any suggestions ?
How about something as simple as this:
function customHash($str) {
$hash = 0;
for ($i = 0; $i < strlen($str); $i++) {
$hash += ord($str[$i]);
}
return $hash % 10;
}
You can optimize this in many ways, like using iconv_strlen() to treat utf-8 strings right and instead of the whole length, $len = max(6, $strlen);
could be used (though the perfomance boost isn't really significant...).
精彩评论