Image sharding and how to avoid multiple items in browser cache
To improve page render speed I am looking at implementing image sharding which serves images from the multiple subdomains. How can I ensure that the same im开发者_JAVA百科age is retrieved from the same server?
I want to avoid a scenario like the one below where the same image is served from multiple domains: images1.domain.com/image1.gif images2.domain.com/image2.gif images2.domain.com/image3.gif images2.domain.com/image1.gif
We are running Java - is there any way to checksum/fingerprint the image-filename and based on that always assign it to the same server?
Use an MD5 hash of the relative path to your content, then modulo this by the number of sharding domains you have. This presupposes that MD5 has a fairly even distribution across the final bit of the output (if you have two shards). My experience here was that it did. Here's a link to another question answering how to generate MD5 hash.
A modulo of a checksum might also suffice and be computationally quicker.
精彩评论