PHPFAN10 Posted February 19, 2011 Share Posted February 19, 2011 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 https://forums.phpfreaks.com/topic/228214-cache-php-web-pages/ Share on other sites More sharing options...
Goldeneye Posted February 19, 2011 Share Posted February 19, 2011 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 https://forums.phpfreaks.com/topic/228214-cache-php-web-pages/#findComment-1176853 Share on other sites More sharing options...
PHPFAN10 Posted February 19, 2011 Author Share Posted February 19, 2011 Thanks, I have decided for now that i will leave it and instead start learning about pear etc. Thanks Link to comment https://forums.phpfreaks.com/topic/228214-cache-php-web-pages/#findComment-1176892 Share on other sites More sharing options...
joshbedo Posted February 20, 2011 Share Posted February 20, 2011 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 https://forums.phpfreaks.com/topic/228214-cache-php-web-pages/#findComment-1176999 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.