PHP Classes

File: Cache.Class.php

Recommend this page to a friend!
  Classes of Sam Stevens   MySQL Abstractor   Cache.Class.php   Download  
File: Cache.Class.php
Role: Auxiliary script
Content type: text/plain
Description: Provides caching support to DBTAL
Class: MySQL Abstractor
MySQL database abstraction layer
Author: By
Last change:
Date: 17 years ago
Size: 1,466 bytes
 

Contents

Class file image Download
<?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("&#58;",":",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(":","&#58;",$value)."\n");
        }
       
fclose($file);
    }
   
}

?>