WebsiteBaker Support (2.8.x) > Templates, Menus & Design
REVAMDS Page Cache for WebsiteBaker
Tez Oner:
Hey,
some code I want to share with the WB Community that will speed up
you WB website to the speed of an 'static website' by caching pages to
HTML, it's a part of the soon to be released REVAMDS Template Framework
for WB (revamds.com) but works also as a standalone script.
REVAMDS Page Cache 1.0 Beta (part of REVAMDS Template Framework 5 for WB)
Step 1 - Create a folder 'storage' in the root of WB (with proper rights).
Step 2 - In there, create a file 'start.php' with:
--- Code: ---<?php
$nonCached = array('314','555'); // pages not to be cached (contact, news etc.)
foreach($nonCached as $nonCachedPage)
{
if(PAGE_ID == $nonCachedPage)
{
}
else
{
$cachefile = WB_PATH.'/storage/'.sha1(PAGE_TITLE.'-'.PAGE_ID);
$cachetime = 30 * 60; // 30 minutes
// $cachetime = 0; // disable cache
if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile)))
{
echo "<!-- Cached by REVAMDS Page Cache at ".date('jS F Y H:i', filemtime($cachefile))." -->";
include($cachefile);
exit;
}
}
}
?>
--- End code ---
Step 3 - Also create a file stop.php with:
--- Code: ---<?php
foreach($nonCached as $nonCachedPage)
{
if(PAGE_ID == $nonCachedPage){
echo $output;
exit;
die;
}
}
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'))
ob_start("ob_gzhandler");
else ob_start();
echo $output;
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush();
?>
--- End code ---
Step 4 - Inlude files in the WB -> root -> index.php
(After line 63 (require(WB_PATH.'/....)
ADD:
--- Code: ---require(WB_PATH.'/storage/start.php');
--- End code ---
(At line 133 ($output..)
REPLACE $output with:
--- Code: ---require(WB_PATH.'/storage/stop.php');
--- End code ---
Step 5 - Reload your site and notice the double lightspeeeeeeeed of WB!
(the cache-files will be added in the 'storage'-folder, cache-lifetime can be modified in $cachetime)
Cheerz,
Tez Oner
quinto:
great ! i will try it asap :)
Ruud:
The speed is very nice, but there are some issues..
- The foreach in start.php does not function.
If the page is not the first one in the $nonCached array, it will become a cachable page. The second one is never tested.
- Any page request with parameters ($_GET or $_POST) should not be cached. (except when using shorturl)
- News and Bakery pages can be detected. No need to put them in the nonCached array manually.
- The inserted cache comment at the top could break correct browser rendering (IE could go into quirks mode)
A modified start.php below fixes these issues:
--- Code: (more testing needed) ---<?php
// create a cachefile id. Must be done first to supress errors in stop.php
$key = PAGE_TITLE.'-'.PAGE_ID;
if(isset($post_id)) $key .= $post_id; // add id of newspage
if(isset($item_id)) $key .= $item_id; // add id of bakery page
$cachefile = WB_PATH.'/storage/'.sha1($key);
// set noncachable pages
$nonCached = array(123,456); // page id's not to be cached
// return from this script is page should not be cached..
foreach($nonCached as $nonCachedPage)
{
if(PAGE_ID == $nonCachedPage) return;
}
if($_SERVER['REQUEST_METHOD'] == 'POST' ) return; // forms should not be cached
if(isset($_GET['_wb'])) unset($_GET['_wb']); // fix if shorturl is used
if(count($_GET)) return; // like in forms, "pervious/next" and search pages
// use the cache if it is available and not too old
$cachetime = 30 * 60; // 30 minutes
if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile)))
{
include($cachefile);
echo "<!-- Cached by REVAMDS Page Cache at ".date('jS F Y H:i', filemtime($cachefile))." -->";
exit;
}
?>
--- End code ---
Cheers,
Ruud
Tez Oner:
--- Quote from: Ruud on April 04, 2015, 12:08:17 AM ---The speed is very nice, but there are some issues..
- The foreach in start.php does not function.
If the page is not the first one in the $nonCached array, it will become a cachable page. The second one is never tested.
- Any page request with parameters ($_GET or $_POST) should not be cached. (except when using shorturl)
- News and Bakery pages can be detected. No need to put them in the nonCached array manually.
- The inserted cache comment at the top could break correct browser rendering (IE could go into quirks mode)
A modified start.php below fixes these issues:
--- Code: (more testing needed) ---<?php
// create a cachefile id. Must be done first to supress errors in stop.php
$key = PAGE_TITLE.'-'.PAGE_ID;
if(isset($post_id)) $key .= $post_id; // add id of newspage
if(isset($item_id)) $key .= $item_id; // add id of bakery page
$cachefile = WB_PATH.'/storage/'.sha1($key);
// set noncachable pages
$nonCached = array(123,456); // page id's not to be cached
// return from this script is page should not be cached..
foreach($nonCached as $nonCachedPage)
{
if(PAGE_ID == $nonCachedPage) return;
}
if($_SERVER['REQUEST_METHOD'] == 'POST' ) return; // forms should not be cached
if(isset($_GET['_wb'])) unset($_GET['_wb']); // fix if shorturl is used
if(count($_GET)) return; // like in forms, "pervious/next" and search pages
// use the cache if it is available and not too old
$cachetime = 30 * 60; // 30 minutes
if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile)))
{
include($cachefile);
echo "<!-- Cached by REVAMDS Page Cache at ".date('jS F Y H:i', filemtime($cachefile))." -->";
exit;
}
?>
--- End code ---
Cheers,
Ruud
--- End quote ---
Thanks and yeh tho WB is already fast, this speeds it even more ;)
What you mean with the 'for each...' it works properly but can be fine-tuned (?),
And thanks for the 'News and Bakery' addition, very nice, the comment can be indeed
on another place, I'll add you changes and will repost the final version soon.
(And this would be a nice feature for WB I guess, not hard to implement but very effective).
Cheerz,
Tez Oner
Tez Oner:
Hey Ruud,
implemented your code and works nice ;) especially not have to specify the 'posts'
page id's is constructive, tho the 'forms' pages (like contact) still don't get refreshed
when required fields aren't filled in. Any idea for a workaround?
Thanks in advance!
Cheerz,
Tez Oner
Navigation
[0] Message Index
[#] Next page
Go to full version