Current time: 11-26-2020, 12:10 PM Hello There, Guest! (LoginRegister)

Post Reply 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ultimate Wordpress Caching (not for the non-programmers)
02-04-2012, 06:11 PM
Post: #6
RE: Ultimate Wordpress Caching (not for the non-programmers)
(02-03-2012 05:19 AM)jarrod1937 Wrote:  For #3, do you still check if the cache file exists or do you just serve it and assume it exists? Reason I ask, is if you do check if the file exists first, then technically you could just filemtime() and on getting false assume the file does not exists, otherwise it does. That would make it so you don't need a cron but still have the same overhead as before. If you do not check if it exists first, how do you handle it if it doesn't? Just curious, this is my first pass at my caching function, looking for ways to make it more efficient.
By the way, I'll probably post another version of the cache function soon. Learned recently that you can pass a callback function to ob_start. That will allow me to put all logic into a neat little function and just call ob_start('functionName'). Should make it easier to implement, as it could then be implemented as a simple include in the main index.php file.

I use this: it checks if file exists, if yes, feeds it out, if not, it uses standard wordpress to feed out the html file to visitor... then proceeds to save it AFTER visitor has received the page. the CRON simply subs in for php calls to check time (it deletes files older than 2 hours). so I set it to 2 hours and does in background. You could easily check if cache file exists, and if expired... but I kept it simple with Cron for speed.

======header section ====

global $cachefile;
$cachefile = '' ;
if ( !is_user_logged_in()) {

// Settings
$cachedir = '/cache/' ; // Directory to cache files in (keep outside web root)
$cacheext = 'htm'; // Extension to give cached files (usually cache, htm, txt)

// Script
$page = '' . $_SERVER['REQUEST_URI']; // Requested page
$cachefile = $cachedir . md5($page) . '.' . $cacheext; // Cache file to either load or create

// Show file from cache
if (file_exists($cachefile)) { @readfile($cachefile); exit(); }

// If we're still here, we need to generate a cache file

your html stuff here, including standard php calls

==== then FOOTER section // BOTTOM of your script after </html tag
global $cachefile;
if (!empty($cachefile)) {
$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

I've come to using this after gleaning from various sources, for example at
and here

overall it works very nicely for my site here

(serving wordpress site from office iMac, off DSL with upload speed of 616 kbps, images offloaded to web-hosting)
Find all posts by this user
Quote this message in a reply
Post Reply 

Messages In This Thread
RE: Ultimate Wordpress Caching (not for the non-programmers) - vincej - 02-04-2012 06:11 PM

Forum Jump:

User(s) browsing this thread: 1 Guest(s)