PHP Salt


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

Version: 2.20.2
Last Build: October 16, 2017 12:40pm (PST)

Main Menu

Home

Get PHP Salt


The Functions

array
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";
$res = $cache['data'];
}
}

return $res;
}



Changlelog:


See Also

  psCacheWrite()
And a shot out to:

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