PHP Salt


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

Version: 2.33.2
Last Build: December 29, 2023 20:28pm (PST)

Main Menu

Home

Get PHP Salt


The Functions

array
db
file
graph
html
mail
math
misc
mobile
script
string
system
time
web
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";

$cache = array();
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:

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