<?php
 
 
 
class lib_cache {
 
 
private $cache_dir = "_cache/";
 
private $group_prefix = "@";
 
private $size = 0;
 
 
    public function load_page($name,$time=60,$group)
 
    {
 
        $name = md5($name);
 
        $dir = $this->addGroup($group);
 
 
        if(@filemtime($dir.$name)>time()-$time)
 
        {
 
            echo @file_get_contents($dir.$name);
 
            return TRUE;
 
        }
 
        else
 
        {
 
            if(!@ob_start("ob_gzhandler")) @ob_start();
 
            return FALSE;
 
        }
 
    }
 
 
    public function save_page($name,$group)
 
    {
 
        $name = md5($name);
 
        $dir = $this->addGroup($group);
 
 
        if(@file_exists($dir.$name))
 
        unlink($dir.$name);
 
 
            $page = @ob_get_contents();
 
            @file_put_contents($dir.$name, $page);
 
            @ob_get_clean($page);
 
    }
 
 
    public function load($name,$time=60,$group)
 
    {
 
        $name = md5($name);
 
        $dir = $this->addGroup($group);
 
 
        if(@filemtime($dir.$name) > time() - $time)
 
        {
 
            return unserialize(@file_get_contents($dir.$name));
 
        }
 
        else
 
        {
 
            return FALSE;
 
        }
 
    }
 
 
    public function save($name,$value,$group)
 
    {
 
        $name = md5($name);
 
        $dir = $this->addGroup($group);
 
 
        if(@file_exists($dir.$name))
 
        unlink($dir.$name);
 
 
            @file_put_contents($dir.$name, serialize($value));
 
    }
 
 
    private function addGroup($name)
 
    {
 
        if(!empty($name))
 
        {
 
            if(is_dir($this->cache_dir.$this->group_prefix.$name))
 
            {
 
                return $this->cache_dir.$this->group_prefix.$name."/";
 
            }
 
            else if(@mkdir($this->cache_dir.$this->group_prefix.$name))
 
            {
 
                return $this->cache_dir.$this->group_prefix.$name."/";
 
            }
 
            else
 
            {
 
                return $this->cache_dir;
 
            }
 
        }
 
        else
 
        {
 
            return $this->cache_dir;
 
        }
 
    }
 
 
    public function removeGroup($name)
 
    {
 
        foreach(glob($this->cache_dir.$this->group_prefix.$name."/*") as $file)
 
        {
 
                unlink($file);
 
        }
 
 
        @rmdir($this->cache_dir.$this->group_prefix.$name);
 
    }
 
 
    public function removeAllGroup()
 
    {
 
        foreach(glob($this->cache_dir.$this->group_prefix."*") as $dir)
 
        {
 
            foreach(glob($dir."/*") as $file)
 
            {
 
                    unlink($file);
 
            }
 
            @rmdir($dir);
 
        }
 
    }
 
 
    public function remove($name,$group)
 
    {
 
        @unlink($this->cache_dir.$group."/".md5($name));
 
    }
 
 
    public function removeAll()
 
    {
 
        $this->removeAllGroup();
 
 
            foreach(glob($this->cache_dir."*") as $file)
 
            {
 
                    unlink($file);
 
            }
 
    }
 
 
    public function checkCacheDir()
 
    {
 
        if(is_dir($this->cache_dir) && is_writable($this->cache_dir) && is_readable($this->cache_dir))
 
        {
 
            return TRUE;
 
        }
 
        else
 
        {
 
            return FALSE;
 
        }
 
    }
 
 
    public function sizeCacheDir()
 
    {
 
        foreach(glob($this->cache_dir."*") as $file)
 
        {
 
                    $this->size+=filesize($file);
 
        }
 
 
        foreach(glob($this->cache_dir.$this->group_prefix."*") as $dir)
 
        {
 
            foreach(glob($dir."/*") as $file)
 
            {
 
                    $this->size+=filesize($file);
 
            }
 
        }
 
 
        if($this->size <= pow(2,20))
 
        {
 
            return round($this->size/pow(2,10),2)."kB";
 
        }
 
        else
 
        {
 
            return round($this->size/pow(2,20),2)."MB";
 
        }
 
    }
 
 
};
 
 
?>
 
 |