使用mod_wsgi和Apache2 mem_cache缓存Django响应

时间:2013-03-10 02:55:37

标签: python django caching apache2 httpresponse

我已经按照以下文章尝试设置Apache2缓存,以便在Ubuntu 12.10上使用带有mod_wsgi的Django。我希望Apache为我缓存一些请求。

http://www.howtoforge.com/caching-with-apaches-mod_cache-on-ubuntu-10.04

从文章中我启用了模块并设置了以下php脚本来测试缓存。缓存工作正常 - 我只在5分钟后得到一个新的时间戳。

vi /var/www/cachetest.php

<?php
header("Cache-Control: must-revalidate, max-age=300");
header("Vary: Accept-Encoding");
echo time()."<br>";
?>

现在在我的django响应中,在以相同的方式设置适当的头之后,我返回一个HttpResponse对象:

# Create a Response Object with the content to return and set it's 
response = HttpResponse("%s"%(output_display))
response['Cache-Control'] = 'must-revalidate, max-age=20'
response['Vary'] = 'Accept-Encoding'
return response

使用Django请求进行缓存根本不起作用。我使用Firefox的LiveHeaders来检查HTTP响应头。

对于上面的示例链接和PHP脚本,标题如下所示:

http://localhost/cachetest.php

GET /cachetest.php HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:19.0) Gecko/20100101 Firefox/19.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Cache-Control: max-age=0

HTTP/1.1 200 OK
Date: Sun, 10 Mar 2013 02:29:32 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.4.6-1ubuntu1.1
Cache-Control: must-revalidate, max-age=300
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 34
Connection: close
Content-Type: text/html
----------------------------------------------------------

对于我的Django请求 - 缓存不起作用,它总是强制冗长的操作来完成响应 - 就像用F5重新加载上面的php请求一样。使用FireFox插件我似乎正在编写正确的标题:

http://localhost/testdjango/testdjango/

GET /testdjango/testdjango/ HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:19.0) Gecko/20100101 Firefox/19.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

HTTP/1.1 200 OK
Date: Sun, 10 Mar 2013 02:32:41 GMT
Server: Apache/2.2.22 (Ubuntu)
Vary: Accept-Encoding
Cache-Control: must-revalidate, max-age=20
Content-Encoding: gzip
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8
----------------------------------------------------------

我做错了什么?如何让django缓存像php脚本一样工作?谢谢!

0 个答案:

没有答案