New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to cache minified pages? #53
Comments
Hi @mkoistinen , |
Any luck on this? |
@mkoistinen I did a test and the cache worked. The order of the middleware:
|
Hmmm, I wish I could say the same for my tests. With the cache primed and HTML_MINIFY = False > ab -n 500 -n 50 http://blah.tld
...
min mean[+/-sd] median max
Total: 3 5 1.7 4 15
... With the cache 'primed' and HTML_MINIFY = True > ab -n 500 -n 50 http://blah.tld
...
min mean[+/-sd] median max
Total: 160 215 33.5 213 327
... And, since these tests are performed with Both tests run from the same server, which is different from the websever. These tests are repeatable and it appears to make no difference if the HtmlMin middleware is just after 'django.middleware.cache.UpdateCacheMiddleware' or just before 'django.middleware.cache.FetchFromCacheMiddleware'. If you're not seeing the same sort of results, then this is something unique to my setup. Here's my full list of middleware, in case you spot something interesting: MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
# This kills performance when enabled
'htmlmin.middleware.HtmlMinifyMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
'cms.middleware.language.LanguageCookieMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
'django.contrib.redirects.middleware.RedirectFallbackMiddleware',
) |
I just tried the same tests on another project which doesn't use any of the cms.* middleware, but is otherwise using a similar set of MW. Results were the same. When HTML_MINIFY = True, I get lack-lustre performance, when HTML_MINIFY = False, its super fast, when the cache is primed. I'd expect the performance to be slightly better not 43X worse. Can you share more details of your setup? |
my settings: MIDDLEWARE_CLASSES = ( CACHES = { |
I can confirm that using these settings significantly slows down server response when caching is enabled. |
Ok. I think I know the source of the problem. From the Django documentation:
I'm not sure how the response is saved in django caching middleware. But do you think this is possible to mark content as minified somehow and check in htmlmin middleware whether it was minified already. |
This is fixed by #61. Make sure the order of middlewares is correct, i.e. MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
'htmlmin.middleware.HtmlMinifyMiddleware',
// ... other middlewares
'django.middleware.cache.FetchFromCacheMiddleware',
) |
Hmmm, I wish I could agree with this assessment. Here's what I'm finding:
Note: This is on a single-core server (2GHz). Without caching at all, this number is about 40 Req. per sec.
My Middleware:
How is it that my results so are different? |
What cache backend are you using? |
Sorry, my mistake: You need to put HtmlMinifyMiddleware after FetchFromCacheMiddleware. This is an annoying Django wart. |
After moving the middleware: ab -n 1024 -c 16 http://www.website.com/ AWESOME ! Thanks so much! Oh, and just for completeness, here it is with my Varnish cache in front of everything: =) |
I've closed this ticket, but I'm still anxious to get the proper, "split into two halves" solution =) Thanks again. |
Hehe, Varnish rocks. Can you try https://github.com/moeffju/django-htmlmin@feature/split-middleware? That has the middleware split. Code: https://github.com/moeffju/django-htmlmin/tree/feature/split-middleware |
With the middleware split, I save about 40ms per request on my test app, and about 4 MB over 1000 requests over the wire (yes, it’s a complex app :D) |
Cf. #64. |
Can't make it work. Installed the repo, installed both MW as directed and I get nothing but 500s. Sadly, I have logging disabled on this project, so I'll have to test it on another to get a log trace. I'll come back shortly. |
OK, I had some issue with pip, it seems. I cleared out the old, then reinstalled from your clone. Seems to work! I couldn't see any significant difference in performance, but, then again, my app is already pretty lean. Thanks again! |
This doesn't appear to be in the master branch yet. Are there plans to do so? |
Hi, I'd love to use HtmlMinifyMiddleware, but I can't figure out where to put it in my settings.MIDDLEWARE so that it does its job, but is then also cached, so that we don't have to minify again until the page changes or the cache expires. As it is now, when I disable HtmlMinifyMiddleware, my server can handle >60X more requests per second, which suggests that, when it is enabled, HtmlMinifyMiddleware is doing its job for each request. I'd like it to do its job only when the page is rewritten to memcache via UpdateCacheMiddleware/FetchFromCacheMiddleware.
My settings look a bit like this:
My reasoning (which could easily be misguided), is that minification should be the last thing to happen to a response body before it gets stored in the cache, hence, it should be the last thing before UpdateCacheMiddleware on the way "out" as a response, therefore the first thing after UpdateCacheMiddleware in the MIDDLEWARE tuple.
I'm using Python 2.7 and Django 1.4, if that is relavant.
What am I doing wrong? Also, whatever the answer, it would make a good addition to the installation guide.
The text was updated successfully, but these errors were encountered: