smart wrote:
Bro can teach me how to add cache option in any simple grabber script ?
Hi bro, follow this,it's an easy thing...
let's say you're trying to grab a webpage, w/ the below code
$url = "http://example.com";
$file = file_get_content($url);
echo $file; //simple enough to understand
Now, if 10 users requests the above script url, let's say http://some.com/grab.php...the function file_get_contents will have fetch the same page 10 times,which is obviously wastage of resource & CPU.
But you can make use of a cool php function ob_start(); which will start output buffering
/*
create a variable for the file dat will be used to show cached content
*/
$cache_filename = 'cache/'.md5($_SERVER['REQUEST_URI']);
/*
$_SERVER['REQUEST_URI'] will make an unique identity for each url w/ query strings
i.e http://some.com/file.php?foo=bar&bar=foo
*/
/*
Let's make a function,to overcome repeating codes in every page
*/
function load_cache($cache_time, $cache_filename)
{
/*check if,the cache file already exists, & the filesize is more than the given value
why filesize?
Incase the function you're using to fetch the page(may be file_get_contents/cURL or something else) returns false(i.e the requested server is unresponsive),the cache file will remain blank,
& if you dont use the filesize param, the blank page will be shown to the visitors...
*/
if (file_exists($cache_filename) && filesize($cache_filename) > 10) //
{
// the time the file was created
$cache_created = filemtime($cache_filename);
}
else
{
// if the file was not found
$cache_created = 0;
}
if ((time() - $cache_created) < $cache_time)
{
return TRUE;
}
else
{
return FALSE;
}
//now use the function
if(load_cache(1000, $cache_filename))
{
//the first param,i.e cache time,1000 here,cuz i wana update the cache every 1000 secs
//if it returns true,it'll mean the file exist, & we can show it to the visitor
require_once $cache_filename;
}
else
{
$url = "http://example.com";
$file = file_get_content($url);
//start output_buffering, which will prevent outputting directly to the browser
ob_start();
echo $file;
/*
//you can put anything here which you want to cache
echo "blah blah";
*/
$cached_content = ob_get_contents(); // this will save the buffer in a variable,
//if you don't understand,it's same like collecting rainwater in a tub :D
//Now put the cached contents in a file which will be served to the next user,requesting the same URL
file_put_contents($cache_filename, $cached_content);
//flush the current buffer output , you may also use the buffer dat you save i.e $cached_content
ob_end_flush();
}
Now w/o the comments,clean example
$cache_filename = 'cache/'.md5($_SERVER['REQUEST_URI']);
if(load_cache(1000, $cache_filename))
{
require_once $cache_filename;
}
else
{
$url = "http://example.com";
$file = file_get_content($url);
ob_start();
echo $file;
$cached_content = ob_get_contents();
file_put_contents($cache_filename, $cached_content);
ob_end_flush();
dat's all..
list of the functions used which will help you to understand better
PHP: ob_start - Manual
PHP: ob_end_flush - Manual
PHP: ob_get_contents - Manual
PS: i learnt it from someone's script when I used to be dead noob but modifed it later to make it work better...