PHP Salt


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

Version: 2.25.1
Last Build: July 17, 2019 09:07am (PST)

Main Menu

Home

Get PHP Salt


The Functions

array
db
file
graph
html
mail
math
misc
mobile
script
string
system
time
psCacheRead()
Reads Caches data from file


Description:

These functions are used to cache data to a file
I use this when calling another function takes a long time, and has to parse
a lot of data, that seldom changes

Cache time is on the read function, this allow cache time changes without affecting
files already written.




Usage:

$mixed = psCacheRead($cFile, $cTime=3600)
$mixed = cache file data, or false if cache data expired or invalid
$cFile = Name of file to save, ex: /tmp/data.cache
$cTime = Cache time in seconds, default = 1hr



Example:

	$wFile = "test.cache";
$ary = psCacheRead($wFile);

if ($ary == false)
{
print"* Cache Expired\r\n";

$d['gallardo'] = "green";
$d['yellow'] = "yellow";

psCacheWrite($d,$wFile);
}





Code:

function psCacheRead($cFile, $cTime=3600)
{
//print"* psCacheRead()\r\n";
$res = false;

if (file_exists($cFile) == true)
{
//print"* psCacheRead(file_exists)\r\n";
$cache = unserialize(psFileGetContents($cFile));
// is it expired?
$now = time();
$expTime = $cache['ts'] + $cTime;
if ( $expTime > $now )
{
//print"* psCacheRead(cache_valid)\r\n";
if ( $cache['z'] < 1 )
{
$res = $cache['data'];
}
else
{
$res = unserialize(gzuncompress($cache['data']));
}

}
}

return $res;
}




Changlelog:

11/23/2018 - added support for compressed data


See Also

  psCacheWrite()
And a shot out to:

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