WebsiteBaker Community Forum

WebsiteBaker Support (2.8.x) => Templates, Menus & Design => Topic started by: Tez Oner on April 03, 2015, 03:01:38 PM

Title: REVAMDS Page Cache for WebsiteBaker
Post by: Tez Oner on April 03, 2015, 03:01:38 PM
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: [Select]
<?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;
}
}
}

?>

Step 3 - Also create a file stop.php with:

Code: [Select]
<?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($fpob_get_contents()); 
fclose($fp);
ob_end_flush();

?>

Step 4 - Inlude files in the WB -> root -> index.php

(After line 63 (require(WB_PATH.'/....)
ADD:
Code: [Select]
require(WB_PATH.'/storage/start.php');
(At line 133 ($output..)
REPLACE $output with:
Code: [Select]
require(WB_PATH.'/storage/stop.php');
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
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: quinto on April 03, 2015, 09:56:46 PM
great ! i will try it asap :)
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: 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) [Select]
<?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;
}
?>

Cheers,
Ruud
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Tez Oner on April 04, 2015, 04:49:10 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) [Select]
<?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;
}
?>

Cheers,
Ruud

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
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Tez Oner on April 08, 2015, 01:07:29 AM
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
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Ruud on April 08, 2015, 02:04:17 PM
The standard form module ads some variables (even without the captcha enabled) to see if the post data was originally created by the formpage. The captcha does the same thing.
These variables are stored in session variables, and will not match (or even exist) if the form is cached.
So for these pages it will be necessary to add the pageid's in the script to "not cache" that page.

You could try a lot more by analizing the html (like any <form> tag in the page should not cache).
It will probably never be fail safe ;)

Attached is my current playground version.
I added a small thingy to differentiate loggedin users and anonymous visitors, and made some more minor changes..
Also the stop.php is much simpler (no more ob_* () functions).

Edit: removed download
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Ruud on April 08, 2015, 02:34:08 PM
You could try a lot more by analizing the html (like any <form> tag in the page should not cache).

This was easy to implement ;)
The current attached version will not cache any page that looks like a form.
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Tez Oner on April 08, 2015, 04:02:46 PM
Hey Ruud,

Code: [Select]
This was easy to implement Wink
The current attached version will not cache any page that looks like a form.

Thanks for the adds, gonna have a look at it later, but looks good :)


Cheerz,

Tez Oner
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: dbs on April 08, 2015, 05:26:29 PM
Hi, the message:
Not cached: Generation time: 0.43670988082886 seconds
is a info or a warning?

The page is fast enough and nothing must cached?
But a visitor could think there is a problem or safety-risk.  :-)
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Ruud on April 08, 2015, 05:31:40 PM
This is info when you are testing.
Code: (start.php) [Select]
<?php 
// when set, generation timing will be displayed.
$debug_cache true;

Set $debug_cache to false (start.php) to disable this information.
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Tez Oner on April 09, 2015, 01:17:10 AM
Hey Ruud,

works fine and code looks clean, but the 'checking' is to tight with this lines
and as I see it it's better to comment em out:

Code: [Select]
if(!stripos($output,'<form') === false) $donotCache = true;
if(!stripos($output,'<input') === false) $donotCache = true;

Got some live-site with a page-rating functionality which is also using a form,
but then none of the pages get cached ;) But noticing the parsing of the pages
is multiplied by 10 is nice to know ;)

Thanks, I'll post an 'official' version later and it's gonna be an feature in the
upcoming REVAMDS (will add some props to it ;)

Cheerz,

Tez Oner
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Ruud on April 09, 2015, 10:19:28 AM
works fine and code looks clean, but the 'checking' is to tight with this lines
and as I see it it's better to comment em out:
Ok, also a loginbox or searchbox on all pages would destroy the caching..  8-)
Another way to detect "form" pages would be to scan for the typical:
class="frm-nixhier"  and/or  name="captcha"

Code: [Select]
if(!stripos($output,'class="frm-nixhier"') === false) $donotCache = true;
if(!stripos($output,'name="captcha"') === false) $donotCache = true;
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Tez Oner on April 09, 2015, 07:38:50 PM
Code: [Select]
if(!stripos($output,'class="frm-nixhier"') === false) $donotCache = true;
if(!stripos($output,'name="captcha"') === false) $donotCache = true;
[/quote]

That could be good work-around gonna check it later.
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Tez Oner on December 03, 2015, 09:57:21 PM
Re-uploaded Download File.
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: johnbroeckaert on February 11, 2016, 12:13:39 PM
Hi,

I downloaded the file but it seems this file is invalid. Got an error: file not complete.  :-(
Is it an idea to reload the complete files (start/stop.php) and  a brief tutorial in a zip?

Regards,
John
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Gast on February 11, 2016, 01:23:42 PM
Unfortunately all attachements older than june 2015 are broken (since the upgrade of this forum software), maybe the file header are destroyed)
i can read the content of the ZIP's as a overview, but not a single file

i hope, this is the right zip

i use it in WB 2.8.4, it needs a call in the index.php of the wb-root and a folder "storage". put the files from the zip into this storage folder and be sure, that you have write permissions on the server

Code for index.php in wb-root
Code: [Select]
require(WB_PATH.'/storage/start.php');
P.S.: of course, it makes the cms very fast, but dont forget, that you see as a admin (or backend user) also the cache-version of a page, if this page has a cache version. sometimes i and some customers going crazy about this

P.S.: see also the array for non-cached pages in start.php, this is only a example
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: johnbroeckaert on February 11, 2016, 01:38:18 PM
Hello Revamds,

Thankyou This zip is OK! (Y)
I now can go and check it.
Thanks again
John
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: johnbroeckaert on February 11, 2016, 06:36:15 PM
Sorry Jacobi22 for caling you Rev....

Just one more thing. I stored the files in a map "storage" and added the code in the root indexfile. How can i see when the site is cached? There is an echo in start.php but...... I don't see the output.

Thanx again,
John
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Gast on February 11, 2016, 07:23:37 PM
for example, we think, you have a page called "start" and a page called "about me". Both pages have links in your site menu
if you call one of this page the first time, the file start.php read the complete HTML-Code from this page and build a file with sha1 encrypted file_name in the storage folder. like 80ae4431ac7f074d2c5 34e4337ba91cdb7925f c0
if you call the same page the next time, the start.php search for this file in the storage-folder and show this content instead of the "normal" content - the "normal" uncached cms-page build his content step by step, header, sections, footer and loads for every part the content from the database, in the cached version, everything is ready to show, nothing to load and because of that, it goes faster.

to shut off the cache mode, go into the file storage / start.php and search for this line here

$cachetime = 0;

these are the minutes, before the script build a new cached version of this pages
maybe it was set to 0, when i work the last time with this project
standard is 30, means: every 30 minutes after building the last cache version of this page, it builds a new version


the cached versions of a page has this entry as the last line in your source code, but if the cachetime set to 0, its doesnt set this line
Quote
<!-- Cached by REVAMDS Page Cache at ".date('jS F Y H:i', filemtime($cachefile))." -->

remember: if you are the admin and change something in the content of a page, you see also the cached version of this page, if this version not older then your defined $cachetime and then, you dont see your last changes
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: johnbroeckaert on February 11, 2016, 07:35:04 PM
Hi Jacobi22,

Yep that was the problem: $cachetime = 0;

Everything is working fine now

TNX again.
John
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Tez Oner on February 11, 2016, 09:46:29 PM
Thanks Jacobi for the support to the PageCache ;)

And yeh I'll have to publish an 'official' version, a workout
when developing / admin changes is to set the cache time to 0
(and to flush / empty the folder with cache files).

Plan is to make this automatically when Login ;) but that to
much for a 'standalone' script. Should be a module or something.
Anyone free to collaborate with me to this?

Cheerz,

Tez Oner
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Ruud on February 11, 2016, 10:22:57 PM
Plan is to make this automatically when Login ;)
....
Anyone free to collaborate with me to this?

If you want the admin just see the live pages always, add this as first line (after the <?php line) in start.php
Code: (untested) [Select]
if($wb->is_authenticated()) return;
This will ignore the caching completely.

Recreating the cache on every pageload when logged-in can be done in start.php directly after the line where $cachefile is declared (line 7).
Code: (untested) [Select]
if($wb->is_authenticated() && file_exists($cachefile)) unlink($cachefile);This will delete an existing cached version and recreate a new one.
Title: Re: REVAMDS Page Cache for WebsiteBaker
Post by: Tez Oner on February 11, 2016, 11:59:43 PM
Hey Ruud,

Thanks that could be a workaround, but I'm more thinking of a 'Flush Cache' feature
and an interface for the Cache.

Cheerz,

Tez Oner