fantasia /core/classes/framework/cache/engine/file.php

Language PHP Lines 140
MD5 Hash c533133b55fa5da1490eda0bc3b0a2af Estimated Cost $260 (why?)
Repository https://github.com/GinoPane/fantasia.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php

class Cache_Engine_File implements Cache_Interface {  
  
    private $_cacheDir = null;
    
    /**
     * 
     * Creates new FileBasedCache instance
     * 
     * @param string $directory
     * @throws Exception
     */
    function __construct($directory = "")  
    {  
        if (!$directory)
        {
            $directory = Loader::getCacheDirectoryPath();
        }
        else
        {
            $directory = Loader::getDirectPath($directory);
        }
        
        if (!is_dir($directory) || !is_writable($directory))  
        {  
            throw new Exception("Directory \"{$directory}\" is not writeable or does not exist!"); 
        }
        else
        {
            $this->_cacheDir = $directory;
        }
    }  
  
    private function _cacheFileName($key)  
    {  
        return $this->_cacheDir . Cfg_App::DS . md5($key);  
    }  
  
    public function get($key)  
    {  
        $cachePath = $this->_cacheFileName($key);  
  
        $cache = null;

        if (is_readable($cachePath))  
        {
            $fp = fopen($cachePath, 'rb');

            if ($fp)  
            {  
                flock($fp, LOCK_SH);  

                $cache = '';  

                if (filesize($cachePath) > 0)  
                {  
                    $cache = @unserialize(fread($fp, filesize($cachePath)));  
                }  
                else  
                {  
                    $cache = null;  
                }  

                flock($fp, LOCK_UN);  
                fclose($fp);
            }
            else 
            {
                trigger_error("Can not open \"{$cachePath}\" for reading!", E_USER_WARNING);
            }
        }
        else
        {
            trigger_error("\"{$cachePath}\" is not readable!", E_USER_WARNING);
        }

        return $cache;  
    }
  
    public function set($key, $data)  
    {  
        $cachePath = $this->_cacheFileName($key);  

        $fp = fopen($cachePath, 'wb');
        
        if ($fp)  
        {  
            if (flock($fp, LOCK_EX))  
            {  
                fwrite($fp, serialize($data));  
                flock($fp, LOCK_UN);  
            }  
            else  
            {
                trigger_error("Can not get lock for \"{$cachePath}\"!", E_USER_WARNING);
                
                return false;  
            }
            
            fclose($fp);
            
            chmod($cachePath, 0777); 
        }
        else
        {
            trigger_error("Can not open \"{$cachePath}\" for writing!", E_USER_WARNING);
            
            return false;
        }
      
        return true;  
    }  
  
    public function clear($key)  
    {  
        $cachePath = $this->_cacheFileName($key);  
  
        if (file_exists($cachePath))  
        {  
            if (unlink($cachePath))
            {
                return true;
            }
            else
            {
                trigger_error("Can not delete \"{$cachePath}\" cache file!", E_USER_NOTICE);
 
                return false;
            }
        }
        else
        {
            trigger_error("Can not locate \"{$cachePath}\" cache file!", E_USER_NOTICE);
        }
  
        return false;  
    }  
}
Back to Top