<commit_before><?php 
/**
 * Created by PhpStorm.
 * User: qiming.c <<EMAIL>>
 * Date: 2016/4/13
 * Time: 20:46
 */

namespace Random\Cache;
use Random\Config;
use Random\IDataCache;

/**
 * 实现文件缓存类
 */

class FileCache implements IDataCache
{
    static $_dir;
    static $_prefix;

    public function __construct()
    {
        $dir   = Config::get('file_cache_dir');
        self::$_prefix = Config::get('cache_prefix');
        self::$_dir = BASE_ROOT.$dir.'/FileCache';
        if (!is_dir(self::$_dir)) {
            if( !mkdir(self::$_dir, 0777, true) ){
                trigger_error("创建".self::$_dir."文件夹失败");
            }
        }
    }

    /**
     * @param  string $key
     * @param  mixed $data
     * @param  int $lifetime
     * @return boolean
     * @todo 设置缓存
     */
    public function set($key, $data, $lifetime = 0)
    {
        $file = $this->getFileDir($key);
        $this->pushContents($file, $data, (int)($lifetime));
    }

    /**
     * @param  string $key
     * @return mixed 数据
     * @todo 得到缓存数据
     */
    public function get($key)
    {
        if (!$this->has($key)){
            return null;
        }
        $file = $this->getFileDir($key);  
        $data = $this->getContents($file);
        if (!empty($data)) {
            return $data;
        }
        return null;
    }

    /**
     * @todo 删除一条缓存
     */
    public function remove($key)
    {
        $file = $this->getFileDir($key);
        if ($this->has($key)) {
            return unlink($file);
        } else {
            return false;
        }
    }

    /**
     * @todo 清除目录下所有缓存
     */
    public function clear()
    {
        $files = scandir(self::$_dir);
        if ($files) {
            foreach ($files as $file) {
                if ($file != '.' && $file != '..' && is_file(self::$_dir.'/'.$file)){
                    unlink(self::$_dir.'/'.$file);   
                }
            }
        }
        return true;
    }

    /**
     * @todo 返回缓存是否存在
     */
    protected function has($key)
    {
        $file = $this->getFileDir($key);
        return is_file($file);
    }
    
    protected function getFileDir($key)
    {
        return self::$_dir.'/'.$this->key2FileName($key);
    }

    protected function key2FileName($key)
    {
        return md5(self::$_prefix."$key");
    }

    protected function getContents($file)
    {
        $contents = file_get_contents($file);

        if ($contents) {
            if ( function_exists('gzuncompress')){
                $contents = gzuncompress($contents);
            }
            $contents =  unserialize($contents);
            if ($contents['time'] == 0 || $contents['time'] > time()){
                return $contents['data'];
            } else {
                unlink($file);
                return null;
            }
        } else {
            return null;
        }
    }

    protected function pushContents($file, $data, $lifetime)
    {
        $contents['time'] = $lifetime == 0 ? 0 : time() + $lifetime;
        $contents['data'] = $data;
        $contents = serialize($contents);
        if (function_exists('gzcompress')) {
            $contents = gzcompress($contents);
        }
        $result = file_put_contents($file, $contents);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
}<commit_msg>增加写入文件缓存时权限判断<commit_after><?php 
/**
 * Created by PhpStorm.
 * User: qiming.c <<EMAIL>>
 * Date: 2016/4/13
 * Time: 20:46
 */

namespace Random\Cache;
use Random\Config;
use Random\IDataCache;
use Random\Exception;

/**
 * 实现文件缓存类
 */

class FileCache implements IDataCache
{
    static $_dir;
    static $_prefix;

    public function __construct()
    {
        $dir   = Config::get('file_cache_dir');
        self::$_prefix = Config::get('cache_prefix');
        self::$_dir = BASE_ROOT.$dir.'/FileCache';
        if (!is_dir(self::$_dir)) {
            if( !mkdir(self::$_dir, 0777, true) ){
                throw new Exception("创建".self::$_dir."文件夹失败");
            }
        }
    }

    /**
     * @param  string $key
     * @param  mixed $data
     * @param  int $lifetime
     * @return boolean
     * @todo 设置缓存
     */
    public function set($key, $data, $lifetime = 0)
    {
        $file = $this->getFileDir($key);
        $this->pushContents($file, $data, (int)($lifetime));
    }

    /**
     * @param  string $key
     * @return mixed 数据
     * @todo 得到缓存数据
     */
    public function get($key)
    {
        if (!$this->has($key)){
            return null;
        }
        $file = $this->getFileDir($key);  
        $data = $this->getContents($file);
        if (!empty($data)) {
            return $data;
        }
        return null;
    }

    /**
     * @todo 删除一条缓存
     */
    public function remove($key)
    {
        $file = $this->getFileDir($key);
        if ($this->has($key)) {
            return unlink($file);
        } else {
            return false;
        }
    }

    /**
     * @todo 清除目录下所有缓存
     */
    public function clear()
    {
        $files = scandir(self::$_dir);
        if ($files) {
            foreach ($files as $file) {
                if ($file != '.' && $file != '..' && is_file(self::$_dir.'/'.$file)){
                    unlink(self::$_dir.'/'.$file);   
                }
            }
        }
        return true;
    }

    /**
     * @todo 返回缓存是否存在
     */
    protected function has($key)
    {
        $file = $this->getFileDir($key);
        return is_file($file);
    }
    
    protected function getFileDir($key)
    {
        return self::$_dir.'/'.$this->key2FileName($key);
    }

    protected function key2FileName($key)
    {
        return md5(self::$_prefix."$key");
    }

    protected function getContents($file)
    {
        $contents = file_get_contents($file);

        if ($contents) {
            if ( function_exists('gzuncompress')){
                $contents = gzuncompress($contents);
            }
            $contents =  unserialize($contents);
            if ($contents['time'] == 0 || $contents['time'] > time()){
                return $contents['data'];
            } else {
                unlink($file);
                return null;
            }
        } else {
            return null;
        }
    }

    protected function pushContents($file, $data, $lifetime)
    {
        $contents['time'] = $lifetime == 0 ? 0 : time() + $lifetime;
        $contents['data'] = $data;
        $contents = serialize($contents);
        if (function_exists('gzcompress')) {
            $contents = gzcompress($contents);
        }
        if (is_dir(dirname($file)) && is_writable(dirname($file))) {
            $result = file_put_contents($file, $contents);
            chmod($file, 0775);
        } else {
            throw new Exception($file . '没有权限写入.');
        }
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
}
{'fraction_non_alphanumeric': 0.15994817160955946, 'fraction_numerical': 0.006910452058738843, 'mean_word_length': 1.6215094339622642, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5375728', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2222, 'n_words': 414}