---
title: "认识缓存之文件缓存"
author: "student_lp"
date: "2014-08-06T16:40:46Z"
tags: []
categories: ["互联网"]
slug: "untitled"
draft: false
lastmod: "2014-08-06T16:40:44Z"
id: 2100892
url: "https://student-lp.iteye.com/blog/2100892"
---

**一、文件缓存产生原因**

    文件缓存是把缓存数据存储到文件系统即硬盘文件中。与内存相比，硬盘属于比较慢的存储设备。那为什么还需要用到文件存储呢？原因如下：

- 磁盘容量大，可以存放足够多的数据。现在的常规磁盘已经进入TB级别，但内存还处于GB级别。磁盘价格远远低于内存价格，通常只有同样大小内存价格的百分之一到十分之一；
- 磁盘与内存相比更稳定更可靠，断电后数据不丢失，存储也比较简单可靠；
- 随着制造技术的进步，出现了固态硬盘（SSD），是硬盘的读取和写入速度得到极大的提高，能达到500Mb/s；
- 扩展容易。可以使用磁盘阵列、分布式处理等进行大规模的存储和管理。

**二、文件缓存机制**

     常用的文件缓存有两种类型，分别是：①站点配置信息或一些变量通过文件缓存，生成php文件，使用时直接加载即可；②将页面生成静态的HTML文件，用户访问时由服务器直接读取HTML文件返回即可。

**1、变量的php文件缓存**

     在一个大型站点中，我们用来配置站点系统的配置参数可能会有很多，而且为了方便管理或动态修改这些配置参数，常常将这些配置参数存放在数据库中。但是用户一旦访问站点，就需要从数据库中加载这些配置信息，当访问量特别大的时候，数据库的压力就会急剧上升，导致站点访问速度变慢，这个时候就需要优化，减少从数据库获取配置信息的次数，因此采用文件缓存就是一个不错的选择。

     通过页面更新站点配置信息的时候，在更新完数据库以后，删除原有的php缓存文件，重新从数据库中读取配置信息，生成出新的php配置信息文件，这样当用户访问站点的时候，php程序直接加载php缓存文件，获取缓存文件中的变量信息，就完成了配置信息的加载，从而节省了访问数据库的开销。并且站点的配置信息变动次数较少，所以采用这种方式缓存非常方便，也可以提升站点的访问速度。

**2、生成静态的HTML文件**

    当用户访问站点时，一些页面信息或者是页面中主要的内容一经发布，基本上很少会发生变动，例如：CMS类的资讯页面。这个时候如果把动态的php代码“编译”成静态的HTML文件，当下次读取时不用再“编译”直接读取静态文件。这样就可以极大的提升服务器的处理效率，提高访问速度。

    有人可能想问，有时候页面中的广告信息和其他的一些列表信息是动态变化的，页面静态化以后，这部分信息如何处理，在这里我提供几个建议希望大家看看:CSI、SSI和ESI。其实简单起见，我们直接通过ajax请求，完成这部分页面内容的加载即可（这其实就是CSI的一种实现方式）。

    现在的模板工具，例如：smarty等。都提供了这个功能，用户通过配置就能够非常容易的实现。在配合URL静态化，很多时候，用户请求，直接由nginx或Apache服务器进行文件流的读取就能够处理完成，因而无需php等脚本语言的相应。

    基本原理：

- 先根据配置文件判断是否要进行缓存，若不需要缓存，则直接include加载php文件。若需要缓存，则转到下一步。
- 判断对应的静态文件，此处即缓存文件是否存在，若不存在，则进行“编译”，将编译内容保存为静态的HTML文件。否则，转到下一步。
- 判断静态文件是否过期，若未过期则读取，否则重新编译。

**注意：**有关HTML静态化，我们在编程的过程中，多借助于模板文件了，一般模板工具都拥有这个功能，所以下面不在详解。如果需要了解，其本质就是将要输出的html内容写入到文件中。如果想要简单的实现，可以参考ob\_start();ob\_end\_clean();ob\_get\_contents();的使用。

**三、开源产品Secache**

    Secache是文件型缓存解决方案，其特点如下：

- 纯php实现，无需任何扩展，支持php4/5；
- 使用LRU算法自动清理过期内容；
- 最大支持1GB缓存文件；
- 使用HASH定位，读取迅速；

    在虚拟主机不支持Memcached等高速缓存的情况下，可以考虑采用Secache。下面是该工具使用示例：

```
require '../Secache/Secache.php';
$cache = new Secache;
$cache->workat('cachedata');

$key = md5('test');//必须自己做HASH，前4位是16进制0-f，最长32位
$value = '数据';//必须是字符串

$cache->store($key,$value);

if($cache->fetch($key,$return)){
   echo $return;
}else{
   echo "date get failed";
}
```

**四、自己编写文件缓存**

```
<?php 
class cache
{   
    private static $_instance = null;
 
    protected $_options = array(
        'cache_dir'        => "./",
        'file_name_prefix' => 'cache',
        'mode'            => '1', //mode 1 为serialize model 2为保存为可执行文件
    );  
     
    /**
     * 得到本类实例
     * 
     * @return Ambiguous
     */
    public static function getInstance()
    {
        if(self::$_instance === null)
        {
            self::$_instance = new self();
        }
        return self::$_instance;
    } 
     
    /**
     * 得到缓存信息
     * 
     * @param string $id
     * @return boolean|array
     */
    public static function get($id)
    {
        $instance = self::getInstance();
         
        //缓存文件不存在
        if(!$instance->has($id))
        {
            return false;
        }
         
        $file = $instance->_file($id);
         
        $data = $instance->_fileGetContents($file);
         
        if($data['expire'] == 0 || time() < $data['expire'])
        {
            return $data['contents'];
        }
        return false;
    }
     
    /**
     * 设置一个缓存
     * 
     * @param string $id   缓存id
     * @param array  $data 缓存内容
     * @param int    $cacheLife 缓存生命 默认为0无限生命
     */
    public static function set($id, $data, $cacheLife = 0)
    {
        $instance = self::getInstance();
         
        $time = time();
        $cache         = array();
        $cache['contents'] = $data;
        $cache['expire']   = $cacheLife === 0 ? 0 : $time + $cacheLife;
        $cache['mtime']    = $time;
         
        $file = $instance->_file($id);
         
        return $instance->_filePutContents($file, $cache);
    }
     
    /**
     * 清除一条缓存
     * 
     * @param string cache id    
     * @return void
     */  
    public static function delete($id)
    {
        $instance = self::getInstance();
         
        if(!$instance->has($id))
        {
            return false;
        }
        $file = $instance->_file($id);
        //删除该缓存
        return unlink($file);
    }
     
    /**
     * 判断缓存是否存在
     * 
     * @param string $id cache_id
     * @return boolean true 缓存存在 false 缓存不存在
     */
    public static function has($id)
    {
        $instance = self::getInstance();
        $file     = $instance->_file($id);
         
        if(!is_file($file))
        {
            return false;
        }
        return true;
    }
     
    /**
     * 通过缓存id得到缓存信息路径
     * @param string $id
     * @return string 缓存文件路径
     */
    protected function _file($id)
    {
        $instance  = self::getInstance();
        $fileNmae  = $instance->_idToFileName($id);
        return $instance->_options['cache_dir'] . $fileNmae;
    }   
     
    /**
     * 通过id得到缓存信息存储文件名
     * 
     * @param  $id
     * @return string 缓存文件名
     */
    protected function _idToFileName($id)
    {
        $instance  = self::getInstance();
        $prefix    = $instance->_options['file_name_prefix'];
        return $prefix . '---' . $id;
    }
     
    /**
     * 通过filename得到缓存id
     * 
     * @param  $id
     * @return string 缓存id
     */
    protected function _fileNameToId($fileName)
    {
        $instance  = self::getInstance();
        $prefix    = $instance->_options['file_name_prefix'];
        return preg_replace('/^' . $prefix . '---(.*)$/', '$1', $fileName);
    }
     
    /**
     * 把数据写入文件
     * 
     * @param string $file 文件名称
     * @param array  $contents 数据内容
     * @return bool 
     */
    protected function _filePutContents($file, $contents)
    {
        if($this->_options['mode'] == 1)
        {
            $contents = serialize($contents);
        }
        else
        {
            $time = time(); 
            $contents = "<?php\n".
                    " // mktime: ". $time. "\n".
                    " return ".
                    var_export($contents, true).
                    "\n?>";
        }
         
        $result = false;
        $f = @fopen($file, 'w');
        if ($f) {
            @flock($f, LOCK_EX);
            fseek($f, 0);
            ftruncate($f, 0);
            $tmp = @fwrite($f, $contents);
            if (!($tmp === false)) {
                $result = true;
            }
            @fclose($f);
        }
        @chmod($file,0777);
        return $result;             
    }
     
    /**
     * 从文件得到数据
     * 
     * @param  sring $file
     * @return boolean|array
     */
    protected function _fileGetContents($file)
    {
        if(!is_file($file))
        {
            return false;
        }
         
        if($this->_options['mode'] == 1)
        {
            $f = @fopen($file, 'r'); 
            @$data = fread($f,filesize($file));
            @fclose($f);
            return unserialize($data);
        }
        else
        {
            return include $file;
        }
    }
     
    /**
     * 构造函数
     */
    protected function __construct()
    {
     
    }
     
    /**
     * 设置缓存路径
     * 
     * @param string $path
     * @return self
     */
    public static function setCacheDir($path)
    {
        $instance  = self::getInstance();
        if (!is_dir($path)) {
            exit('file_cache: ' . $path.' 不是一个有效路径 ');
        }
        if (!is_writable($path)) {
            exit('file_cache: 路径 "'.$path.'" 不可写');
        }
     
        $path = rtrim($path,'/') . '/';
        $instance->_options['cache_dir'] = $path;
         
        return $instance;
    }
     
    /**
     * 设置缓存文件前缀
     * 
     * @param srting $prefix
     * @return self
     */
    public static function setCachePrefix($prefix)
    {
        $instance  = self::getInstance();
        $instance->_options['file_name_prefix'] = $prefix;
        return $instance;
    }
     
    /**
     * 设置缓存存储类型
     * 
     * @param int $mode
     * @return self
     */
    public static function setCacheMode($mode = 1)
    {
        $instance  = self::getInstance();
        if($mode == 1)
        {
            $instance->_options['mode'] = 1;
        }
        else
        {
            $instance->_options['mode'] = 2;
        }
         
        return $instance;
    }
     
    /**
     * 删除所有缓存
     * @return boolean
     */
    public static function flush()
    {
        $instance  = self::getInstance();
        $glob = @glob($instance->_options['cache_dir'] . $instance->_options['file_name_prefix'] . '--*');
         
        if(empty($glob))
        {
            return false;
        }
         
        foreach ($glob as $v)
        {
            $fileName = basename($v);
            $id =  $instance->_fileNameToId($fileName);
            $instance->delete($id);
        }
        return true;
    }
}
 
/* 初始化设置cache的配置信息什么的 */
cache::setCachePrefix('core'); //设置缓存文件前缀
cache::setCacheDir('./cache'); //设置存放缓存文件夹路径
 
//模式1 缓存存储方式
//a:3:{s:8:"contents";a:7:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:34;i:4;i:5;i:5;i:6;i:6;i:6;}s:6:"expire";i:0;s:5:"mtime";i:1318218422;}
//模式2 缓存存储方式
/*
 <?php
 // mktime: 1318224645
 return array (
  'contents' => 
  array (
    0 => 1,
    1 => 2,
    2 => 3,
    3 => 34,
    4 => 5,
    5 => 6,
    6 => 6,
  ),
  'expire' => 0,
  'mtime' => 1318224645,
)
?>
 * 
 * 
 */
cache::setCacheMode('2'); 
 
if(!$row = cache::get('zj2'))
{
     
    $array = array(1,2,3,34,5,6,6);
    $row = cache::set('zj2',$array);
}
// cache::flush(); 清空所有缓存
 
print_r($row);
```