PHP Salt


A little dash of PHP... my collection of php functions

Version: 2.13.2
Last Build: October 09, 2016 21:15pm (PST)

psFileCache()
Returns a URL from cache or from web


Description:

This function is used to cache web requests.
This is usefull if you need to grab a url, and cache the data for a specific
amount of time, so as to reduce fetch time and/or bandwidth.



Usage:

$str = psFileCache($url, $cacheFileName [,cache_time]);
Default expire time is 10 seconds



Example:

	$str = psFileCache("http://www.google.com","/tmp/google.cache");




Code:

// returns a cache file, expires in seconds, default is 10
function psFileCache($wURL, $cFile, $cTime=10)
{
// does the cache file exists
$expired = "y";

if (file_exists($cFile) == true)
{
//print "** Exists **";
$cache = unserialize(psFileGetContents($cFile));
// is it expired?
$now = time();
$expTime = $cache['ts'] + $cTime;
if ( $expTime > $now )
{
$expired = "n";
}
}

//print "Expired ($cTime): $expired [$expTime / $now]
";

// at this point, if expired = y, we need fetch it
if ($expired == "y")
{
$cache['ts'] = time();
$cache['data'] = psFileGetContents($wURL);
file_put_contents($cFile,serialize($cache));
}

return $cache['data'];
}

Changlelog:


See Also

And a shot out to:

LinuxSimple - www.linuxsimple.com
PHP - php.net
Fedora Server - getfedora.com
Shameless ads to pay for site