开发者

Parallelize downloads across hostnames and Django

开发者 https://www.devze.com 2023-03-05 01:17 出处:网络
I want to achieve this to speed up the load of my images. For those who don\'t know about this, please read here.

I want to achieve this to speed up the load of my images. For those who don't know about this, please read here.

Is there something for Django that can help me regarding this, in any way ? I'm thinking mostly how to automatize the URL creation for my static content. I'm follow the next pattern开发者_如何学Go:

<script src="{{STATIC_URL}}scripts/jquery.js" type="text/javascript"></script>

I first thought to sequentially set STATIC_URL to "http://cdnX.mydomain.com/", where X will be numbers from 1 to 4, but this will break the caching, because I have no guaranty that, for example, jquery will be always served by cdn2. Or am I wrong ?

Any ideas anyone ?


You probably want a custom template tag to take care of your looping, and probably store the information in the user's session. You could also use the cache backend for this, but that is up to you. It may look something like this:

LAST_CDN=4
cdn_format = "http://cdn%s.mydomain.com/%s"
@register.simple_tag(takes_context=True)
def cdn_url(context, url):
    request = context['request'] ## Assumes that request is in your context.
    current_cdn = request.session.get('current_cdn', 0)
    current_cdn += 1
    if current_cdn > LAST_CDN:
        current_cdn=1
    cdn_list = request.session.get('cdn_list', {})
    if cdn_list.get(url, None) is not None:
        return cdn_format % (cdn_list.get(url), url,)
    else:
        cdn_list[url] = current_cdn
        request.session['cdn_list'] = cdn_list
    return cdn_format % (current_cdn, url,)


Well, it's a bit obvious, but if you could always just "assign" resources to one domain or another, by setting up STATIC_URL_1, STATIC_URL_2, etc. and then picking one manually to use for each resource in your template.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号