Django缓存子域 [英] Django caching for subdomains

查看:150
本文介绍了Django缓存子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django中通过中间件黑客在django中使用子域,与 here

I'm using subdomains in django for user pages via a middleware hack in a similar way to what is described here:

现在,对于未登录用户的所有页面,我都启用了默认的django缓存。我不得不为用户页面隐式禁用缓存,因为它们像这些页面一样处理这些页面,例如。 filmaster.com和michuk.filmaster.com是与django相同的页面。

Now, I have the default django cache turned on for all pages for not-logged-in users. I had to disable the cache implicitly for user pages as it treated those pages just as if they were the / pages, e.g. filmaster.com and michuk.filmaster.com is the same page to django.

你知道任何好的和简单的方法来强制django了解缓存的子域?或者你建议我只是明确地缓存每个子域视图?

Do you know of any nice and easy way to force django to understand subdomains for caching? Or do you suggest I just cache each of the subdomain views explicitly?

更新:实际上看到解决方案,这并不完全如此。我们不重定向我们希望网址留在子域中,所以我们所做的只是直接从中间件调用视图。

Update: actually looked into that solution and it's not exactly how we do it. We do not redirect. We want the url to stay in subdomain, so what we do is just call the views directly from the middleware.

您可以在这里查看hacky实现的详细信息:musielak.eu/public/film20/film20/core/middleware.py [更新:404页面不发现](用户:justlookingaround,pass:film @ ster - 是的,我们是开源)。这里是一个jira修复黑客:jira.filmaster.org/browse/FLM-54(但是这不是完全相关的问题 - 只是为了确保你不认为我们支持笨蛋编码:P) p>

You can see the details of the hacky implementation here: musielak.eu/public/film20/film20/core/middleware.py [Update:404 page not found] (user: justlookingaround, pass:film@ster -- yes, we're open source). And here is a jira for fixing the hack: jira.filmaster.org/browse/FLM-54 (but that's not entirely relevant to the problem - it's just to make sure you don't think we support crappy coding :P)

推荐答案

好的,这是一个我们实际使用的修复。它不幸涉及黑客Django代码,特别是django / trunk / django / utils / cache.py
中的_generate_cache_header_key方法我们所做的只是检查HTTP主机中是否有任何子域,如果是,则提取从它的子域并将其附加到缓存密钥。
我们也可以简单地附加主机,这将非常相似,但是在RAM中占用一些更宝贵的位。

OK, here is a fix that we actually used. It does unfortunately involve hacking Django code, in particular the _generate_cache_header_key method in django/trunk/django/utils/cache.py What we do is simply check if there is any subdomain in the HTTP host and if so, extract the subdomain from it and append it to the cache key. We could also simply append the host, which would work very much the same, but taking some more precious bits in RAM.

这是jira: a href =http://jira.filmaster.org/browse/FLM-84 =nofollow noreferrer> http://jira.filmaster.org/browse/FLM-84
这里是使用的代码。使用您自己的风险!

Here is the jira: http://jira.filmaster.org/browse/FLM-84 And here is the code used. Use at your own risk!

def _generate_cache_header_key(key_prefix, request):
    """
       Returns a cache key for the header cache.
       With Filmaster hack for handling subdomain caching: http://jira.filmaster.org/browse/FLM-84
    """
    subdomain = None
    path = request.path

    # TODO: this is not a decent implementation, it will work only on domains with one dot it them
    # To fix it, we'd need to pass a param to the request object before CacheMiddleware
    # this current domain name in it, and use that domain name in the regexp below
    m = re.match(r"([^\.]+)\.[^\.]+\.[^\.]+", request.META['HTTP_HOST'])
    if m!=None:
        subdomain = m.group(1)

    if subdomain != None:
        path = subdomain + "___" + path
    path = md5_constructor(iri_to_uri(path))
    return 'views.decorators.cache.cache_header.%s.%s' % (key_prefix, path.hexdigest()) 

这篇关于Django缓存子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆