php 文件缓存

1.文件缓存类

<?php
/**
 * 文件缓存
 */
class FileCache{
    private static $Dir = './'; // 缓存的目录
    private static $DirMode = 0777; // 文件夹权限 一般是在linux
 
    /**
     * 设置缓存目录
     */
    public function Dir( $dir ){
        if ( !is_dir( $dir ) ) {
            mkdir( $dir, self::$DirMode, true );
        }
        self::$Dir = $dir;
    }
 
    /**
     * 读取缓存文件
     */
    public function Read( $file ){
        //路径和文件名拼接起来
        $filename = self::$Dir . $file;
        if ( !file_exists( $filename ) ) {
            return false;
        }

        // 读取缓存文件
        if (!( $handle = @fopen( $filename, 'rb' ) ) ) {
            return false;
        }

        /*
         *  先读取第一行
         *  跳过写入缓存文件保护代码 <?php exit; ?>
         */ 
        fgets( $handle );
        // 取出序列化的缓存数据,并进行序列化恢复
        $data = unserialize( fgets( $handle ) );
        return $data;
    }
 
    /**
     * 写入缓存文件
     * $file 写入缓存的文件名
     * $data 需要进行缓存数据
     */
    public function Write( $file, $data = array() ){
        $filename = self::$Dir . $file;
        // 尝试打开 $filename,如果文件不存在则创建
        if ( $handle = @fopen( $filename, 'wb' ) ) {
            // 取得独占锁定 锁定文件我在操作时其他人不能进行操作
            @flock($handle, LOCK_EX);
            // 缓存文件建议保存为 .php 格式
            // 写入缓存文件保护代码
            // 防止恶意访问该文件
            fwrite( $handle, '<' . '?php exit; ?' . '>' );
            fwrite( $handle, "\n" );
            // 序列化待缓存的数据
            $data = serialize($data);
            // 写入缓存数据
            fwrite( $handle, $data );
            // 释放锁定
            @flock( $handle, LOCK_UN );
            fclose( $handle );
            return true;
        }
        return false;
    }
 
    /**
     * 清除缓存文件
     * array $fileArr 需要清除的缓存文件
     *  bool $filenameMode 完整文件名模式,为 true 时必须 $file 参数必须输入完整的文件名,用于清除不在同一文件夹下的缓存文件!
     *
     * 案例:
     * FileCache::Clear(array('file1'),'A.php');//清除单条数据
     * FileCache::Clear(array('file1', 'file2', 'file3'),'A.php');//清除多条数据,注意:必须在同一文件夹下
     * FileCache::Clear(array('file1', 'file2', 'file3'),'A.php', './cache/A.php');//完整文件名模式
     */
    public function Clear( $fileArr,$file = '', $filename = false ) {
        if ( !is_array( $fileArr ) || empty( $fileArr ) ||  empty($file) ) {
            return;
        }

        if(!$filename){
            //先把数据读出来
            $readArr = self::Read($file);
            // 数组函数 array_diff() 按值删除 
            $readArr = array_diff($readArr, $fileArr);
           // 修改完在写入
           self::Write('A.php', $readArr);
           
        }else{
            //判断文件是否存在 防止第二次删除时 函数报错
            if (!file_exists( $filename ) ) {
                return false;
            }else{
                unlink($filename); 
            }
        }
        
    }
 
}

2.使用文件缓存

<?php
include './file.class.php';
$data = array('撒地方哼哼唧唧','aserdstyugfti');
 
// 写入 ./cache/A.php
FileCache::Dir('./cache/');
// FileCache::Write('A.php', $data);
 
// 读取./cache/A.php
print_r(FileCache::Read('A.php'));

//删除
// $b = FileCache::Clear(['1234567'],'A.php','./cache/A.php');
$b = FileCache::Clear(['aserdstyugfti'],'A.php');
print_r(FileCache::Read('A.php'));
 
//文件锁详解
// https://www.cnblogs.com/chenwenbiao/archive/2011/08/01/2123905.html

// 如果你要在laravel使用redis
// https://blog.csdn.net/gu_wen_jie/article/details/79413365

// 懒加载
//课后想一下怎么设置缓存过期