<?php
class Cache
{
private $cacheFile = "";
private $settings = array();
private $changed = false;
private $disabled = false;
function __construct($cacheFile, $disable=false)
{
if ($cacheFile != "")
$this->cacheFile = $cacheFile;
if ($disable == true)
$this->disabled = true;
else
$this->Load();
}
private function Load()
{
$file = @file($this->cacheFile);
if (!is_array($file))
return false;
foreach($file as $key => $line)
{
$line = explode(":",$line);
$this->settings[$line[0]] = str_replace(":",":",eregi_replace("\n$","",$line[1]));
}
return true;
}
function Get($name)
{
if ($this->disabled == true)
return "";
if (key_exists($name, $this->settings))
return $this->settings[$name];
else
return false;
}
function Set($name,$value)
{
if ($this->disabled == true)
return;
$this->settings[$name] = $value;
$this->changed = true;
}
function ClearCache()
{
if ($this->disabled == true)
return;
$this->settings = array();
$this->Save();
}
function Save()
{
if ($this->disabled == true)
return;
if ($this->changed == false)
return; //no changes to cache
$file = fopen($this->cacheFile,"w")
or die("FATAL ERROR: Cannot write to cache file");
foreach($this->settings as $name => $value)
{
fwrite($file,$name.":".str_replace(":",":",$value)."\n");
}
fclose($file);
}
}
?>
|