- container info with blog skins?
- Just a question about b2evo's Tag URLs
#1 Jul 03, 2009 22:14
I hope I'm not to late to make my case this modification get included in the next stable release.
Now that we have a wonderful disk-caching system, I thought I would implement cache-control directives in order to further reduce bandwidth & cpu consumption.
The idea is that if a page is valid in the disk cache (its lifetime is lower than the maximun age ($max_age_seconds) b2evo can very well respond with a 304 Not modified HTTP header and so instructs the browser or proxy cache to present the user with the locally cached copy, instead of sending again the same many KB of data. If the age of the cached page is higher than $max_age_seconds, b2evo will regenerate the cache and respond with a normal 200 OK HTTP header and the newly minted data, with new Last-Modified info.
The relevant RFC is [url=http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html]RFC2616[/url].
So the changes, surprisingly, are very few. First in inc/_blog_main.inc.php:
As you can see here, what I do is to send a Last-Modified and Cache-Control HTTP headers when the cache page is generated, because the old cached page has expired (its age is greater than $max_age_seconds) or it didn't exist (it's the first time this page is viewed).
So far so good.
Now we go to inc/_core/model/_pagecache.class.php:
Here it is the meat, so to speak.
If the browser has already the page in its local cache and it knows the date of last modification (which we sent previously), the second time the browser will send a conditional GET with the If-Modified-Since header. In this case and if cached page is valid (ie, it hasn't been modified since we generated the b2evo cached version) instead of spitting it out, what we send is a 304 Not modified and immediately exit, so we don't send any data, only the header.
If this particular user is viewing this particular page for the first time (but another user already saw it and, consequently caused b2evo to generate a cached version), then we send the data as usual with the Last-modified header, so this user's browser can cache the page locally.
I have been testing this in my blog (shameless plug here: http://liberal-venezolano.net/) since early june and I can say bandwidth consumption has been reduced significantly, on the order of 40 to 50%. I would guess I have reduced a lot my CPU usage, but this I have not measured.
So, I hope others here take a look at this implementation and give their opinions.
Enjoy seeing your 304's soar in your apache logs :)