=> :!: :!: :!:
=> Go to the latest version of this hack: [url=http://forums.b2evolution.net/viewtopic.php?p=25724#25724]Simple Cache Hack v0.3[/url]
=> You can also read all the following posts in order to get extended information about this hack.
=> :!: :!: :!:
I've written a Simple Cache Hack for [url=http://b2evolution.net]b2evolution[/url] intended to reduce CPU usage on the server. Its principle is very simple:
a visitor requests a page to be displayed;
the server checks if the page has been cached;
if the requested page is in the cache, it reads it and sends its cached contents to the visitor;
if the requested page is not in the cache, then the page is first created using [url=http://b2evolution.net]b2evolution[/url] standard behaviour, then sent to the visitor for display and finally written to the cache.[/list:u]
In that context, I am going to try to make things easier to understand:
What is the cache?
The cache is a simple folder on your server's drive, anywhere your [url=http://www.php.net]PHP[/url] script can write.
What is a cached page?
A cached page is a simple text file written to your cache folder. Here, the name of the cached page file is built concatenating:
the "b2_evo_" prefix;
the MD5-encoded page URL (20 characters);
the MD5-encoded query string (20 characters).[/list:u]
Since a blog can have an infinite number of pages when including searches and other parameters, the cache needs to be deleted on a regular basis.
When is the cached page built?
A cached page is created when a visitor requests for a page that does not appear in the cache yet or the cached version of the page is outdated.
While Movable Type (a commercial blog system) builds static pages on page change, the Simple Cache Hack builds cached pages when these pages are requested (not in cache or outdated). The Simple Cache Hack [u]does not[/u] build cached pages on change. So, it is important to delete the given page's all cached versions on its change, or to delete the whole cache, or to use a high cache update frequency (but then, the cache becomes less interesting while more up to date).
How is a cached page built?
Say the server pretends to be a web browser (like your Internet Explorer or Firefox Mozilla "displaying" only raw (X)HTML and nothing else) and asks itself to "display" a given page. The page is then "displayed" using all the standard [url=http://b2evolution.net]b2evolution[/url] routines as it worked before the Simple Cache Hack has been installed.
Once the page is loaded, it writes its contents to the cache as a simple text file.
Why using a cache reduces CPU usage?
Displaying a fully dynamical page using [url=http://www.php.net]PHP[/url] and MySQL is resources consuming. [url=http://b2evolution.net]b2evolution[/url] [url=http://www.php.net]PHP[/url] scripts define the way the blog data (posts, comments, etc.) is retrieved from MySQL. MySQL needs several file accesses to retrieve most data. All these processes cost CPU time, memory and disk accesses.
Without a cache, each time you access to a given page, that page is built from scratch with all the needed CPU, memory and disk resources to execute dynamically.
Using a cache, the first time you access to a given page, that page is built from scratch, dynamically. Then, once built, it is written to the cache (as a simple text file on the disk). Finally, it is sent to the visitor. As you can see, the first access costs more with a cache system built than without. However, the next time the same page is requested (by the same visitor or by other visitors), the same page is no more built from scratch: it is read from the cached page, then sent to the visitor. That simple process is up to several times quicker than building the page from scratch (depending of your page contents and complexity).
A cache system is interesting when the same resource is requested several times.
On [url=http://blog.lesperlesduchat.com]my own blogs[/url], I benchmarked a cache-improved page load at 347 ms vs 1,611 ms for the same page load without using the Simple Cache System. I also noticed a cache efficiency of about 70-80% (70-80% of all the pages requests serve cached versions of the requested pages) and a cache update frequency of about 45-55% (45-55% of all the pages requests rebuild the page cache before or after serving the page). That means the Simple Cache System reduced my server's resources usage by 23 to 34%. Other parameters might improve those results (especially increasing the cache age tolerance).
What are the side effects of using a cache?
There are several side effects of using this Simple Cache Hack:
Since cached pages are static, they cannot change at each access. If you rebuild the cache at each page access, it is going to be (slightly) slower than without using a cache system.
If your pages have to implement dynamic content (like the visitor's IP, his/her location, his/her browser, a chatterbox built-in in the page, and so on), you should not use this cache system, since previously cached version of the current URL (+query string) is going to be displayed for everybody.
Since the same version of a given cached file is displayed to everybody, don't expect to see you identifier written into the page: you see what an anonymous visitor would see.
When you change anything to your blog (publish a comment, publish a post, etc.), you have to delete the associated pages or the whole cache.[/list:u]
However, when implemented on the blogs themselves, the system does not prevent from identifying himself. Once identified, go to your /admin/ folder directly, by typing the administration page address in your web browser address bar (add it to your favorites bookmarks before installing the hack).
How to keep dynamic content into cached pages?