PHP Salt


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

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

psFnCacheRead()
Caching for functions return data


Description:

These functions are used to cache function return 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:

Please see example usage, as this are used to create a function wrapper


Example:

$str = someFunc(5,6);


function someFunc($a,$b)
{
$r = myFunc($a,$b);
print "res: $r\r\n";
}




// we wrap our real function with this code
function myFunc($a,$b) // wrapper function
{
$cDir = "/tmp"; // cache dir
$uk = md5("{$a}{$b}"); // uniq key representing the variables (used for filename)
$cTime = 2; // cache time in seconds

$res = psFnCacheRead($cDir,$uk,$cTime);

if ($res == false) {
$res = psFnCacheWrite(myFunc_($a,$b),$cDir,$uk);
}

return $res;
}


// here is the real function
function myFunc_($a,$b)
{
return $a + $b;
}





Code:


function psFnCacheWrite($cData,$cDir,$uk)
{
$trace = debug_backtrace();
$fnName =$trace[1]['function'];

$cDir .= "/{$fnName}";
@mkdir($cDir,0755,true);

$cFile = "{$cDir}/{$uk}.ps";
$res = psCacheWrite($cData,$cFile);
return $cData;
}


function psFnCacheRead($cDir,$uk,$cTime)
{
$trace = debug_backtrace();
$fnName =$trace[1]['function'];
$cDir .= "/{$fnName}";
$cFile = "{$cDir}/{$uk}.ps";
$res = psCacheRead($cFile,$cTime);
return $res;
}




Changlelog:


And a shot out to:

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