55 lines
1.4 KiB
PHP

<?php
class SaleCache
{
private $cache_id = '';
private $smarty = null;
private $max_filemtime = 0;
private $template_path = '';
private $path_cache_file = '';
function __construct($smarty, $cache_prefix, $lifetime = 7200) // 2 Hours
{
$d = new DateTime();
if (isset($_SERVER['QUERY_STRING'])) {
$cache_prefix .= $_SERVER['QUERY_STRING'];
}
$this->cache_id = md5("private_sales_".$cache_prefix."_".$d->format('Ymd'));
$this->max_filemtime = $d->getTimestamp() - $lifetime;
$this->path_cache_file = __DIR__.'/../cache/'.$this->cache_id;
$this->smarty = $smarty;
}
function __destruct()
{
if (!empty($this->template_path && $this->smarty)) {
file_put_contents($this->path_cache_file, $this->smarty->fetch($this->template_path));
}
}
function isCached($template_path)
{
if (file_exists($this->path_cache_file) && filemtime($this->path_cache_file) > $this->max_filemtime) {
return true;
}
$this->template_path = $template_path;
return false;
}
function getCache()
{
return file_get_contents($this->path_cache_file);
}
static function clearAll()
{
foreach(glob(__DIR__.'/../cache/*') as $path_file) {
if (preg_match('/index\.php$/', $path_file)===1) {
continue;
}
@unlink($path_file);
}
}
}