/*
* Запись кэш-файла
* @param string contents - содержание буфера
* @param string filename - имя файла, используемое при создании кэш-файла
* @return void
*/
function writeCache($content, $filename) {
if (!file_exists("/tmp/cache/".encode_path($filename))) {
if (!recursiveMkdir("/tmp/cache/".encode_path($filename))) {
}
}
$fp = fopen('/tmp/cache/' . encode_fullpath($filename), 'w');
fwrite($fp, $content);
fclose($fp);
}
/*
* Проверка кэш-файлов
* @param string filename - имя проверяемого кэш-файла
* @param int expiry - максимальный <возраст> файла в секундах
* @return mixed содержимое кэша или false
*/
function readCache($filename, $expiry) {
if (file_exists('/tmp/cache/' . encode_fullpath($filename))) {
if ((time() - $expiry) > filemtime('/tmp/cache/' . encode_fullpath($filename)))
return FALSE;
$cache = file('/tmp/cache/' . encode_fullpath($filename));
return implode('', $cache);
}
return FALSE;
}
function encode_fullpath($fname)
{
$s1=substr($fname,0,1);
$s2=substr($fname,-1);
return $s1."/".$s2."/".$fname;
}
function encode_path($fname)
{
$s1=substr($fname,0,1);
$s2=substr($fname,-1);
return $s1."/".$s2."/";
}
function recursiveMkdir($strPath, $mode = 0777) {
return is_dir($strPath) or ( recursiveMkdir(dirname($strPath), $mode) and @mkdir($strPath, $mode) );
}