Forum Moderators: coopster

Message Too Old, No Replies

PHP libraries to cache SQL queries

Can I insert PHP code within cached copies?

         

guarriman

8:23 am on Apr 10, 2007 (gmt 0)

10+ Year Member



Hi.

Using PHP 4, I'd like to cache webpages instead of using PHP each time visitors access them.

I've created a webtool showing personal profiles, and they use a lot of SQL queries within each page. I want to create a cached copy each time one user modifies their profile, and I've used the cool idea proposed on this WebmasterWorld thread:
[webmasterworld.com...]

It creates a cached copy of the webpage, and serves it when visitors access it.

Does anybody know any alternative PHP library to carry this out? I created it by myself, but would like to find one which is already developed.

I'm trying to integrate more features, like showing user's nickname if they are logged in. I can insert whithin cached copies some PHP code in order to make Apache interprete it, but I don't know if somebody made it before.

Thank you very much.

joelgreen

3:21 pm on Apr 10, 2007 (gmt 0)

10+ Year Member



Smarty template engine (smarty.php.net) supports caching.

dublinmike

8:22 pm on Apr 10, 2007 (gmt 0)

10+ Year Member



Smarty does sound ideal for what you need but in my opinion the simplest/best way to enable caching of pages is the following:

Create 2 files, cache_header.php and cache_footer.php. In cache_header.php, add the following:

$filename = "%%-".md5($_SERVER['REQUEST_URI'])."-%%.html";
$cachefile = "/my/cache/directory/".$filename;
$cachetime = 30 * 60;
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile))
{
include($cachefile);
exit;
}
ob_start();

Then in cache_footer.php, add the following:

$fp = fopen($cachefile, 'w+');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush();

It's really simple, just create the two files, include cache_header.php at the start of the page you would like to cache output from, include cache_footer.php at the end of the same page and you are done.

AlexK

3:38 am on Apr 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



require_once( 'Cache/Lite.php' );

The code has already been created for you. Works well on my site.