server/onyx/require/cache.php

46 lines
1.2 KiB
PHP
Raw Normal View History

2013-10-09 13:40:23 +00:00
<?php
class Cache
{
2013-12-14 05:11:14 +00:00
2013-10-09 13:40:23 +00:00
static function set($id,$var)
{
$file = '<?php $cache = '.var_export($var,TRUE).'; ?>';
2013-12-14 05:11:14 +00:00
$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);
2013-10-09 13:40:23 +00:00
}
2013-12-14 05:11:14 +00:00
2013-10-09 13:40:23 +00:00
static function read($id)
{
if(!is_readable(ONYX.'cache/'.md5($id).'.cache.php')) return FALSE;
2013-12-14 05:11:14 +00:00
$tmpfname = tempnam("/tmp", "cache");
copy(ONYX.'cache/'.md5($id).'.cache.php', $tmpfname);
include($tmpfname);
unlink($tmpfname);
if(empty($cache)) return FALSE;
2013-10-09 13:40:23 +00:00
return $cache;
}
2013-12-14 05:11:14 +00:00
2013-10-09 13:40:23 +00:00
static function del($id)
{
if(!is_file(ONYX.'cache/'.md5($id).'.cache.php')) return FALSE;
return unlink(ONYX.'cache/'.md5($id).'.cache.php');
}
2013-12-14 05:11:14 +00:00
2013-10-09 13:40:23 +00:00
static function flush()
{
foreach(glob(ONYX.'cache/*.cache.php') as $file) unlink($file);
}
2013-12-14 05:11:14 +00:00
2013-10-09 13:40:23 +00:00
}
?>