server/onyx/require/cache.php

46 lines
1.2 KiB
PHP

<?php
class Cache
{
static function set($id,$var)
{
$file = '<?php $cache = '.var_export($var,TRUE).'; ?>';
$tmpfname = tempnam("/tmp", "cache");
if(file_put_contents($tmpfname, $file, LOCK_EX) !== FALSE)
rename($tmpfname, ONYX.'cache/'.md5($id).'.cache.php') or trigger_error('dossier cache inaccessible en écriture.',E_USER_ERROR);
else
trigger_error('impossible d\'?crire dans '.$tmpfname,E_USER_ERROR);
}
static function read($id)
{
if(!is_readable(ONYX.'cache/'.md5($id).'.cache.php')) return FALSE;
$tmpfname = tempnam("/tmp", "cache");
copy(ONYX.'cache/'.md5($id).'.cache.php', $tmpfname);
include($tmpfname);
unlink($tmpfname);
if(empty($cache)) return FALSE;
return $cache;
}
static function del($id)
{
if(!is_file(ONYX.'cache/'.md5($id).'.cache.php')) return FALSE;
return unlink(ONYX.'cache/'.md5($id).'.cache.php');
}
static function flush()
{
foreach(glob(ONYX.'cache/*.cache.php') as $file) unlink($file);
}
}
?>