<?php
class fcache{
    
    /**
     * 获取缓存数据
     *
     * @param 文件 $f
     * @param 序列 $s
     */
    public function get($f,$s=NULL){
        $_p = SITE_DIR.'/365fcache/'.$f.'.php';
        if ( !file_exists($_p) ) {
        	return NULL;
        }
		
        include($_p);
        
        $end_time = intval($fcache['end_time']);
        if ( $end_time < time() ) {
        	return NULL;
        }
        if (!$s) {
        	return $fcache['data'];
        }
        $rt = $fcache['data'];
        $exp = explode('.',$s);
        foreach ($exp as $key){
            if ($rt[$key]) {
            	$rt = $rt[$key];
            	continue;
            }
            return NULL;
        }
        return $rt;
    }
    
    /**
     * 添加修改缓存数据
     *
     * @param 文件 $f
     * @param 序列 $s
     * @param 数据 $d
     */
    public function set($f,$s,$d,$time=86400){
        $_p = SITE_DIR.'/365fcache/'.$f.'.php';
        
        if ( file_exists($_p) ) {
        	require($_p);
        }else {
            tool::mkdirs(dirname($_p));
            $fcache = array(
                'end_time' => time() + $time,
                'data' => array(),
            );
        }
        
        $fcache['end_time'] = time() + $time;
        if ($time==0) { //假设永久缓存为一年
        	$fcache['end_time'] = time() + 86400*365;
        }
        
        if ( !$s ) {
        	$fcache['data'] = $d;
        }else {
            $exp = explode('.',$s);
            $pointer = &$fcache['data'];
            foreach ($exp as $n => $key){
                if( !$pointer[$key] ){
                    $pointer[$key] = array();
                }
                $pointer = &$pointer[$key];
            }
            $pointer = $d;
        }
        $str = tool::arrayToCode($fcache);
        $str = "<?php\n\$fcache = ".$str."\n?>";
        
        if( !file_put_contents($_p, $str) ){
            return false;
        }
        return true;
    }

    /**
     * 添加修改缓存数据
     *
     * @param 文件 $f
     * @param 数组 $arr
	 *     $arr = array(
	 *        'token' => '12345',
	 *        'name'  => '张三',
	 *        '0.ag_name' => '李四',
	 *     );
     * @param 时间 $time
     */
    public function sets($f,$arr,$time=86400){
        $_p = SITE_DIR.'/365fcache/'.$f.'.php';
        
        if ( file_exists($_p) ) {
        	require($_p);
        }else {
            $fcache = array(
                'end_time' => time() + $time,
                'data' => array(),
            );
        }
        
        $fcache['end_time'] = time() + $time;
        if ($time==0) { //假设永久缓存为一年
        	$fcache['end_time'] = time() + 86400*365;
        }
        
		/**
		* $arr = array(
		*    'token' => '12345',
		*    'name'  => '张三',
		*    '0.ag_name' => '李四',
		* );
		**/
		foreach($arr as $s => $d){
			$exp = explode('.',$s);
			$pointer = &$fcache['data'];
			foreach ($exp as $n => $key){
				if( !$pointer[$key] ){
					$pointer[$key] = array();
				}
				$pointer = &$pointer[$key];
			}
			$pointer = $d;
		}

        $str = tool::arrayToCode($fcache);
        $str = "<?php\n\$fcache = ".$str."\n?>";
        
        if( !file_put_contents($_p, $str) ){
            return false;
        }
        return true;
    }
    
    /**
     * 删除缓存文件
     *
     * @param string $f
     */
    public function del($f){
        $_p = SITE_DIR.'/365fcache/'.$f.'.php';
        if (file_exists($_p)) {
        	unlink($_p);
        }
    }
}
?>