Hoi,

Ik ben een beetje bezig geweest met maken van cache classe.
hebben jullie nog opmerkingen/ tips over deze classes?

CacheContoller:

<?php
class CacheController {
    private $sCacheDir;
    private $sCacheIndex;

    public function __construct($pDir = null) {
        $this->sCacheDir = ($pDir == null) ? './cache' : $pDir;
        $this->sCacheIndex = $this->sCacheDir.'/cache.index';
    }

    /**
     * Function readIndex.
     * This function read the index file. The function unserialize it and return the Array.
     * If there is no index file he return a blank Array
     */
    private function readIndex() {
        if(file_exists($this->sCacheIndex) && filesize($this->sCacheIndex) > 0) {
            $h=fopen($this->sCacheIndex,'r');
            $c=fread($h, filesize($this->sCacheIndex));
            fclose($h);
            $s = (strlen($c)>0)? unserialize($c) : array();
            return $s;
        }
        return array();
    }
    /**
     * Function set.
     * This function make the cache file.
     * The expire time is in seconds. if you want 1 hour, than you make param $pTime 3600.
     * @return boolean TRUE on success, or FALSE on error
     */
    public function set($pVar, $pId, $pTime, $pCategory = null) {
        $c = self::readIndex();
        $scalar = is_scalar($pVar);
        $c[md5($pId)] = array(
            'scalar'=>$scalar,
            'category'=>$pCategory,
            'expire_time'=>time() + $pTime
        );
        // make the cache file
        $cache_content = $scalar ? $pVar : serialize($pVar);
        $h=fopen($this->sCacheDir.'/'.md5($pId).'.cache','w');
        if(!fwrite($h,$cache_content)) return false;
        fclose($h);
        //build or make the index file.
        $c=serialize($c);
        $h=fopen($this->sCacheIndex, 'w');
        if(!fwrite($h,$c)) return false;
        fclose($h);

        return true;
    }
    /**
     * Function read.
     * This function reads the cache file if it exists.
     * @return contents of the cache file. if not exists or on failure, returns FALSE.
     */
    public function read($pKey) {
        if(!file_exists($this->sCacheDir.'/'.md5($pKey).'.cache') || filesize($this->sCacheDir.'/'.md5($pKey).'.cache') <= 0) return false;
        $c = self::readIndex();
        if (!isset($c[md5($pKey)])) return false;
        if ($c[md5($pKey)]['expire_time'] < time()) {
            $h = fopen($this->sCacheDir.'/'.md5($pKey).'.cache','r');
            $content = fread($h, filesize($this->sCacheDir.'/'.md5($pKey).'.cache'));
            fclose($h);
            return ($c[md5($pKey)]['scalar'])? $content : unserialize($content);
        }
        return false;
    }
}
?>

GarbageCollector:

<?php
class GarbageCollector{
    private $sCacheDir;
    private $sCacheIndex;

    public function __construct($pDir = null) {
        $this->sCacheDir = ($pDir == null) ? './cache' : $pDir;
        $this->sCacheIndex = $this->sCacheDir.'/cache.index';
    }

    /**
     * Function readIndex.
     * This function read the index file. The function unserialize it and return the Array.
     * If there is no index file he return a blank Array
     */
    private function readIndex() {
        if(file_exists($this->sCacheIndex) && filesize($this->sCacheIndex) > 0) {
            $h=fopen($this->sCacheIndex,'r');
            $c=fread($h, filesize($this->sCacheIndex));
            fclose($h);
            $s = (strlen($c)>0)? unserialize($c) : array();
            return $s;
        }
        return array();
    }

    /**
     * Function clearAllCache.
     * **WARNING** This function remove all the cache files in the directory.
     * @return boolean TRUE on success, or FALSE on error
     */
    public function clearAllCache() {
        $dirCache = opendir($this->sCacheDir);
        while(false !== ($file = readdir($dirCache))) {
            if($file != "." && $file != ".." && $this->sCacheDir.'/'.$file != $this->sCacheIndex) {
                chmod($this->sCacheDir.'/'.$file, 0777);
                if(!is_dir($this->sCacheDir.'/'.$file)) {
                    if(!unlink($this->sCacheDir.'/'.$file)) return false;
                }
            }
        }
        closedir($dirCache);
        $h = fopen($this->sCacheIndex, 'w');
        if(!fwrite($h,'')) return false;
        fclose($h);
        return true;
    }
    /**
     * Function clearCategory.
     * This function remove all the cache files with a specific category.
     * @return boolean TRUE on success, or FALSE on error
     */
    public function clearCategory($pCategory) {
        $c = parent::readIndex();
        foreach($c as $key=>$value) {
            if ($value['category'] == $pCategory) {
                 if(!unlink($this->sCacheDir.'/'.$key.'.cache')) return false;
                unset($c[$key]);
            }
        }
        $c=serialize($c);
        $h = fopen($this->sCacheIndex, 'w');
        if(!fwrite($h,$c)) return false;
        fclose($h);
        return true;
    }
    /**
     * Function clearOverTime.
     * This function remove all the cache files that are over time.
     * @return boolean TRUE on success, or FALSE on error
     */
    public function clearOverTime() {
        $c = parent::readIndex();
        foreach($c as $key=>$value) {
            if ($value['expire_time'] < time()) {
                if(!unlink($this->sCacheDir.'/'.$key.'.cache')) return false;
                unset($c[$key]);
            }
        }
        $c=serialize($c);
        $h = fopen($this->sCacheIndex, 'w');
        if(!fwrite($h,$c)) return false;
        fclose($h);
        return true;
    }
}
?>

De cacheContoller zorgt voor het aanmaken/updaten en lezen van de cache files.
De GabageCollector zorgt ervoor dat bepaalde cache files ook echt verwijderd worden van de server.

alvast bedankt.

Groetjes,
Roy

Reageren