Yup, it is a nice red Ferrari now — found the missing tire, we are rocking now
Tags:BLOG, vince's blogIn my previous post I spoke about how to fix the preloading in order to take advantage of WordPress and CDN for local AND remote serving. Please see
Primer needed: CDN is slower than my slowpoke for galleries – FIX IT NOW
Today, I tackled the final missing tire to get this Ferrari on the road. We’re going to get a custom cacheing system in place.
Let’s recap:
Server: optimized for Sql cache, APC cache, object-cache in WP, gzip of all acceptable file formats, and full hard-coded redirection to CDN for all images, js, css which work remotely.
Small change to the image upload directory in WordPress, and we used a tool called Auto Post Thumbnail (WP plugin) to ensure all our old thumbnails were now using the new CDN links.
Good to go from here forwards,… but we were missing a cacheing system.
We quickly built a system which would use Cron to create a static home page to our visitors. This simply updates the home page every 10 minutes, a very simple, elegant and straight forwards “keep it simple” solution.
KISS all the above, but we’re still missing a full cacheing system.
We needed a system to cache our over 2000 pages today, and get ready for massive growth in near future. We plan to have over 5000 items online by December ![]()
We looked at all the cacehing plugins for WordPress and found all lacking. For many various reasons either we could not get them to work, or they did not work fully, or simply mucked up our scripting and custom coding on our site. These , like Hyper-cache, W3TotalCache and WP-Super-Cache Quick Cache, are best used for sites on shared hosting, and they do a great job there. We have a dedicated host.
So to the cacheing strategy I found all/any of the above somewhat limiting. I needed finer grained cacheing, and I needed it to WORK FOR ALL files at all times.
I’m sure I spent over 40 hours tackling the various plugins, installing, testing, and re-installing. I did not find a single one (and I tested almost all available, including many not mentioned here) which did the job at 99%. I insist on 99%.
All the bells and whistles the plugins contained, we already had covered on our own server setups here, and the only piece missing now was the page cache itself. I tried to install the cache plugins and JUST USE THE PAGE CACHE… but the convoluted .htaccess rules again led me to problems.
So….. I sat back and bit the bullet. Something I should have done in the beginning. I just built it myself, as usual. (I spent less time creating my cacheing system and testing then trying out the plugins, by the way.
What I found was simple. Use some logic, define your needs, and then just use PHP to cache your files.
Here’s how it works.
Pages to be cached.
- Home page – Cron w/ 10 minute intervals, always cached, no live version. ONE of these
- Singel Posts — PHP cacheing, cached page created on demand, or I can batch create from a list. 2000 of these
- 404 error page: nice to keep it cached… One of these
- Static Pages: like about us… Green spot is… consignment info, etc.. no need for these to be dynamic : about 30 pages.
Timeouts: I requred that each of these groupings have differing times of renewal… for example
- Home page: should be renewed every 10 minutes.
- Single Posts: these can be renewed every 12-24 hours.
- 404 should be cached for longer period, like 6 days
- Static Pages, cache for 24-48 hours.
If one could count on browsers having javascript and browser cacheing enabled — well that would be simpler… you could count on the browser to cache it given specific directions from our server.
But you can’t.
So we take matters into our own hands. WE tell the browser how long to cache files for.. and then force-feed them cached files just in case they didn’t obey. A lesson learnt from raising three boys, I guess ![]()
The system also needed to remain dynamic, so if a user logged in, they would get live files AND we considered anyone who viewed more than a few pages a potential customer who should be given fresh pages to view. We did this with cookies.
WE set different values for different parts of the site according to our best guesstimate of utility. (working from memory, I think these are the values, or roundabout)
If a user is logged in… this totally overrides the cache. But if not (the lookey-loos and casual surfers), then
- Home page: needs 10 views to override cache – nothing to do on homepage. no live buy button.
- Single Posts: needs 4 views to ovferride cache — hmm might really be interested., and each page contains a live purchase option. (except the SOLDS).
- 404 page never overriden
- Static Pages, needs 6 views to override.
So there are the rules. here’s how we solved the issue.
Note: techy stuff below.
Step 1: this goes into the TOP of your file: best use is for your single.php file, for example.
you should add <?php to the beginning of these scripts, of course.
open here to view the script solution»The result: If a visitor logs in they get live pages, if they view more than 6 pages they get live pages,. If they leave after viewing 4 pages, the cookie, set for one hour starts the count again. The cached file remains the same for 24 hours.
Now the occasional visitor, the lookey-loos, and search engines all create no real strain on our server, they are all fed cached files. ![]()
Step 2: And for the footer you place this in your pages.
And for the footer you place this in your pages.»This is just to close off the operation… don’t ask me, I’m no PHP genius….. it just works.
The step at the top determines if you should send a cached file…. if not, the server spits out the composed page, and now saves it to a cached file. A simple operation.
CAVEAT: you don’t want to be using this for multi-page archives, tag archives, etc.
Besides: why would you wish to cache a multi-page archive or search result? just bad karma I think, asking for trouble and extra complexity for little advantage.
Using the above system, we’ve combined this with our CDN systems, optimized server, and reduced our page load to 2 seconds from OUR server, and about 2 seconds max from CDN service. That’s 3 second load for about 1.4 Megs of composed “live” pages. We have tested the scenario out where we utilize the CDN system to serve out the static cached pages, and it leads to a 2 second download for ALL parts.
Impressive, and definetly a round the lap in Ferrari-like time now.
If you require assistance in optimizing your websites, do give us a shout. Remember, you’re viewing this page from a home iMac sharing time with our office software…. can it get any better than that?
High-end results : inquire here… because We do Biz websites too
if ( !is_user_logged_in()) {
if (isset($_COOKIE["visit"])){ $visit= ++$_COOKIE["visit"];
} else { $visit= 1;}
setcookie(“visit”, $visit, time()+3600, “/”, “.greenspotantiques.com”, False, True);
if ($visit <=10 && !is_user_logged_in() ){
$cachefile = ‘/cache/’.basename($_SERVER['REQUEST_URI'] );
$cachetime = 24 * 60 * 60 ; /* 120 * 60; 24 hours */
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() – $cachetime < filemtime($cachefile))) {
header(‘Content-Type: text/html’);
header(‘HTTP/1.1 200 OK’);
readfile($cachefile);
echo ““;
exit; }
ob_start(); // start the output buffer
// Your normal PHP script and HTML content here
} } ?>
This script gets placed in the theme files sections you wish to cache. At the very top, BEFORE any html is output…. right at the top of the file, before any !DOC declarations.
To use this you simply change the “/cache” folder to one your webserver has access to on your own disk.
// BOTTOM of your script
$fp = fopen($cachefile, ‘w’); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser
?>
This goes AFTER everything else in your page. Absolutely the last lines of your file.





Visitors can save their WATCHED ITEMS 
liked and shared, thanks!
Good Day this is often a great article man. Thanks sadly i’m experiencing drawback with ur rss . Unable to subscribe to it. will anyone experiencing same rss feed problem? Anybody who can help please respond. Thanks
First report of problems with rss feed… anyone else?
I’ll look into it if I hear of any other problems.
thanks.
Vince.
This is a really great blog. Thx to the auther
Pretty audacious viewpoint that you currently have there. A whole lot of people would back away from going over things such as this, but I appreciate how daring you happen to be even if I don’t truly agree!