Jump to content

Cache PHP Web Pages


PHPFAN10

Recommended Posts

Hi,

 

I am looking for reliable, effective code to cache one or two of my website webpages. I know there are packages like the pear cache lite but is far beyond my needs and complex in terms of never used pear before etc.

 

I am looking for a good cache class perhaps or a link to a good cache script. Does anyone have any code they would like to share or know of any good cache classes etc?

 

I want to keep it relatively simple if possible.

 

Thanks for any suggestions, help.

PHPLOVER

Link to comment
Share on other sites

A fairly simple way would be to use the $_SESSION array. This would ensure that each user would not see a page generated by another user which is useful if have a User-account system.

 

$_SESSION['cache'] = array(
                  'PAGE_NAME' => array(
                            'html' => 'HTML_HERE', 
                            'time' => 'UNIX_TIMESTAMP'
                    )
          );

 

'PAGE_NAME' is just the name of the page the cache is for. So say you had three pages (index.php, about.php, contact.php); each page would have its own element in the $_SESSION['cache'] array:

$_SESSION['cache']['index.php'];
$_SESSION['cache']['about.php']
$_SESSION['cache']['contact.php']

 

$_SESSION['cache']['PAGE_NAME']['time'] would contain the timestamp (from time()) of when the page is generated. This way, you can check on each page load if the cached content is more than X seconds, minutes, or hours old and if it is, then regenerate the HTML.

Link to comment
Share on other sites

hey i was messing around with your idea and started building a class i was wondering if you knew how i could check if current time is greater than or equal to the cached time plus so many minutes

<?php
session_start();
class cache{

private $page;
public $time;
public $currentTime;

public function cachePage($pagename, $time)
{
		$_SESSION['cache'] = array(
				  $pagename => array(
							'html' => $pagename. '.html', 
							'time' => $time
					)
		 );

}
public function getCurrentTime()
{
	$this->currentTime =  date("H:i:s");   
}
public function destroy()
{
	if(isset($_SESSION['cache']))
	{
		unset($_SESSION['cache']);
	}
}
}
//end of class

$cache = new cache;
if(!isset($_SESSION['cache']))
{
	//$cache->getTime();
	$cache->time =  date("H:i:s");
	$cache->cachePage('index', $cache->time);
}
if(isset($_SESSION['cache']))
{
	$cache->currentTime = date("H:i:s");
                //heres where i need help
	if(strtotime($cache->currentTime) >= strtotime("3+ minutes", $cache->time))
	{
		$cache->destroy();
		$cache->time =  date("H:i:s");
		$cache->cachePage('index', $cache->time);
	}
}
print_r($_SESSION['cache']);
echo strtotime($cache->currentTime). '<br/>';
//echo strtotime("3+ minutes", $cache->time);
//$cache->destroy();
//echo $cache->time;
?>
?>

 

was trying to use strtotime("3+ minutes", $this->time) in the if statement but it still wasnt working maybe it was the way i was doing it?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.