tomaslin / grails-cdn-resources

Plugin that plays together with grails resources plugin but loads resources from a CDN like Amazon CloudFront
Other
8 stars 5 forks source link

Allow multiple CDN urls #4

Open cmagnuson opened 12 years ago

cmagnuson commented 12 years ago

It would be helpful to allow a list of CDN urls to be entered, so that file requests can be paralleled over multiple hostnames.

grails.resources.cdn.url = ["http://resources1.xyz.com", "http://resources2.xyz.com", "http://resources3.xyz.com""]

cmagnuson commented 12 years ago

I've fixed this by looking up the url from the list based on the hashed value of the resource (this way each resource is served by a single URL, loading it again will not result in a cache miss).

if( !url ){ int cdnNum = getHashedResourceNum(resource.linkUrl, mergedConfig.url.size()); url = mergedConfig.url[cdnNum] }

private int getHashedResourceNum(String toHash, int maxNum){ if(toHash.contains('/')){ toHash = toHash.substring(toHash.lastIndexOf('/')); } return toHash.hashCode() % (maxNum+1); }