NP_Cache v1.2 - mix static and dynamic content
I have upgraded my "Plugin-Of-The-Year Winner" (as moraes denoted it), aka NP_Cache, to fully support partial caching. This gives you the ability to cache (statically) most of your page, while still allowing dynamic content plugins like NP_WIE, NP_Views, comment form, and few others to work as they should.
And not only this! Partial cache subscribes to pre-edit / delete / add item and comment events, add / delete category events, and blog events, and clears the item, index, and archive pages as necessary.
NP_Cache is a plugin that creates static pages on request, and it does it fully automatically. What does this mean? If page, or part of it, is requested for the first time, it will capture the output, save it into a static XHTML file, and when it is requested for the second time, it will output this static page in microseconds, without doing tens of SQL queries. It will speed-up your system 10 to 500-times.
NP_Cache allows you to specify which pages should be completely static (i.e. they will be completely saved, with no dynamic content, and refreshed only after a specified cache lifeTime, or, of course, after an event is triggered, like adding an item or a comment), or use special keywords to cache parts of pages that will be cached.
Completely static, fully cached pages, are simple. You don’t need to make any modifications to your skins. Just install NP_Cache.
Now, partial caching is a bit different. You will have to specify which parts of your skins shall be cached, and which not, which shall remain dynamic .
First, you will have to make one small mod (addition) to Nucleus core files. Open file called SKIN.php, and find following lines:
case 'skintype': $condition = ($name == $this->skintype); break;
Right after these lines add the following code:
case 'notincache': global $np_cache_active; $condition = $np_cache_active; break;
You’re done! After this, NP_Cache plugin, and Nuclues skin parser, will use conditions in skins to parse and/or cache parts of skins as required.
Now, this is how you specify that you want to cache part of your skin:
<%Cache(start,name-of-part1)%> <%if(notincache)%> ... cached skin part 1 ... <%endif%> <%Cache(stop)%>
What does this mean? <%Cache(start,name-of-part1)%> will start the cache. If you have more than one part to cache within one skin, you will have to specify unique skin parts, names. They can be numbers, or letters, but this name has to be unique within one skin (other skins, or other blogs, can use the same name without any problem).
The condition <%if(notincache)%> tells Nucleus parser, that following sections should be processed only if the file is not in cache (NP_Cache plugin). If it is, it will jump to <%endif%> tag instead, thus completely ignoring what is inside (NP_Cache plugin will output a static, cached, XHTML page instead).
Finally, you will have to call <%Cache(stop)%> to stop caching of certain part of your skin and save it to static pages repository.
Now, you wonder, is it possible to cache multiple parts of your skins? E.g. cache everything except a comment form? Here’s an example:
<%Cache(start,1)%> <%if(notincache)%> ... your cached skin header ... <%endif%> <%Cache(stop)%> <%commentform%> <!-- not cached --> <%Cache(start,2)%> <%if(notincache)%> ... your cached skin footer ... <%endif%> <%Cache(stop)%>
There are two more parameters you can supply to <%Cache()%> : it is lifeTime of a particular cache, and „stats“ parameter, for statistics. With lifeTime parameter, you can fine-tune your skins and caches as you like. Usage looks like this:
<%Cache(start,1,1000)%> <!-- in cache for 1000 minutes --> <%if(notincache)%> ... XHTML ... <%endif%> <%Cache(stats)%> <!-- cache statistics --> <%Cache(stop)%> <!-- stop cache --> <!-- dynamic content now.. --> <%WIE%>
Statistics is displayed by using <%Cache(stats)%>.
Download at: https://myego.cz/other/NP_Cache.zip.
I have added one more option to NP_Cache version 1.2, which makes static pages dynamic as well, even without modifying core. ;)
Now you can use <!– DYNAMIC_CONTENT_START –> and <!– DYNAMIC_CONTENT_STOP –> keywords in your skins (NOT templates) to specify which parts of your skins will by dynamic, while all the rest will be static.
So, you can define all skins as completely static, except e.g. call to <%commentform%>, which will be surrounded by DYNAMIC_CONTENT_START and DYNAMIC_CONTENT_STOP keywords. In this case, article will be completely cached, and only comment form will be parsed.
Example (keywords have to be entered exactly like this, including exactly one space between comment tags and the keyword):
... <!-- DYNAMIC_CONTENT_START --> <%commentform%> <!-- DYNAMIC_CONTENT_STOP --> ...
When using NP_Cache, please uninstall NP_GZip plugin, it is NOT compatible with NP_Cache (as NP_Cache fully takes over GZip functionality).