dats cached result which is saving the resource & CPU usage, the cache is stored for 10000 secs by default & then again updated

$url = "http://example.com";
$file = file_get_content($url);
echo $file; //simple enough to understand
/*
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();
}
$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();
$url = "http://example.com";
$file = file_get_content($url);
echo $file; //simple enough to understand
/*
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();
}
$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();
<?php
echo '<div style="display:inline;float:right;"><object type="application/x-shockwave-flash" data="/dewplayer.swf?mp3=preview.php?url='.$preview.'" width="200" height="18" id="dewplayer"><param name="wmode" value="transparent" /><param name="movie" value="/dewplayer-vol.swf?mp3=preview.php?url='.$preview.'" /></object>
<a href="/initiate.php?url='.$link.'" title="Download Now"><img src="hide.png" /></a></div>';
?>
Comment