Location: PHPKode > projects > PHPLayouts > PHPLayouts/libs/Cache.php
<?php

/**
 * Cache Handler
 *
 * @author rodix53 <hide@address.com>
 * @version 0.9.1-beta
 * @package php-layouts
 * @license http://creativecommons.org/licenses/by-nc/2.0/fr/ Creative Commons BY-NC 2.0
 */
 
class Cache
{
    /**
     * The name of the directory where templates are located.
     * @var string
     */
    private $_cache_dir            =  null;
    
    private $_cache_activated      =  false;
    private $_cache_lifetime       =  3600;
	
	public function __construct($tpl_dir)
	{
        $this->layout_dir = $tpl_dir;
	}
    
    /**
     * Return if the cache handler is activated or not.
     * @return bool
     */
    public function isActivated()
    {
        return (bool) $this->_cache_activated;
    }
    
    /**
     * Enable the cache handler
     * @param int $lifetime The life time of a cache file
     */
    public function activate($lifetime = 3600)
    {
        $this->_cache_activated = true;
        $this->_cache_lifetime = intval($lifetime);
    }
    
    /**
     * Disable the cache handler
     */
    public function desactivate()
    {
        $this->_cache_activated = false;
    }
    
    /**
     * Set cache directory
     * @param string $cache_dir Folder of cache scores
     * @return bool
     */
    public function setCacheDir($cache_dir)
    {
        if (!is_dir($cache_dir))
		{
            Template::trigger_error('The cache directory provided do not exists', E_USER_WARNING);
            return false;
        }
        $this->_cache_dir = $cache_dir;
        return true;
    }
    
    /**
     * Get cache directory
     * @return string
     */
    public function getCacheDir()
    {
        return $this->_cache_dir;
    }
    
    /**
     * Check if the cache file need to be re-compiled or not.
     * @param string $filename Name of the cache file
     * @return bool
     */
    public function checkCache($filename)
    {
        $cache_file = $this->_cache_dir . md5($filename) . ".cache";
        
        // Si le fichier cache n'existe pas ou n'est pas lisible.
        if (!is_file($cache_file) || !is_readable($cache_file))
        {
            return false;
        }
        // Si le fichier de cache est toujours valide.
        else if (time() - filemtime($cache_file) >= $this->_cache_lifetime)
        {
            return false;
        }
        
        // Autrement, on affiche le cache ...
        return true;
    }
    
    /**
     * Create a new cache file
     * @param string $filename Name of the cache file
     * @param string $content  Content of the cache file
     */
    public function createCache($filename, $content)
    {
        $cache_file = $this->_cache_dir . md5($filename) . ".cache";
        $cache_handle = fopen($cache_file, 'w+');
        fwrite($cache_handle, $content);
        fclose($cache_handle);
    }
    
    /**
     * Delete a specified cache file
     * @param string $filename Name of the cache file
     * @return bool
     */
    public function deleteCache($filename)
    {
        $cache_file = $this->_cache_dir . md5($filename) . ".cache";
        return @unlink($cache_file);
    }
    
    /**
     * Clear all the cache
     */
    public function clearAllCache()
    {
        $dir = opendir($this->_cache_dir);
        while ($file = readdir($dir) != false)
        {
            if (!is_dir($file) && ($file != ".htaccess"))
                unlink($file);
        }
        closedir($dir);
    }
}
Return current item: PHPLayouts