Home  >  Article  >  php教程  >  php 缓存技术

php 缓存技术

WBOY
WBOYOriginal
2016-05-25 16:42:381159browse

关于缓存技术不只在php有,很多系统都有,这是为了减轻服务器压力与数据库压力来做的,本文章从php缓存技术入门到利用缓存实例来告诉你如何应用php来实例文件缓存描述,

先看这个缓存类,代码如下:

<?php
class cache {
    /*
    class name: cache
    description: control to cache data,$cache_out_time is a array to save cache date time out.
    version: 1.0
    author: 老农 cjjer
    last modify:2006-2-26
    author url: http://www.phprm.com
*/
    private $cache_dir;
    private $expiretime = 180; //缓存的时间是 60 秒
    function __construct($cache_dirname) {
        if (!@is_dir($cache_dirname)) {
            if (!@mkdir($cache_dirname, 0777)) {
                $this->warn(&#39;缓存文件不存在而且不能创建,需要手动创建.&#39;);
                return false;
            }
        }
        $this->cache_dir = $cache_dirname;
    }
    function __destruct() {
        echo &#39;cache class bye.&#39;;
    }
    function get_url() {
        if (!isset($_server[&#39;request_uri&#39;])) {
            $url = $_server[&#39;request_uri&#39;];
        } else {
            $url = $_server[&#39;script_name&#39;];
            $url.= (!emptyempty($_server[&#39;query_string&#39;])) ? &#39;?&#39; . $_server[&#39;query_string&#39;] : &#39;&#39;;
        }
        return $url;
    }
    function warn($errorstring) {
        echo "<b><font color=&#39;red&#39;>发生错误:<pre class="brush:php;toolbar:false">" . $errorstring . "
";     }     function cache_page($pageurl, $pagedata) {         if (!$fso = fopen($pageurl, 'w')) {             $this->warns('无法打开缓存文件.'); //trigger_error             return false;         }         if (!flock($fso, lock_ex)) { //lock_nb,排它型锁定             $this->warns('无法锁定缓存文件.'); //trigger_error             return false;         }         if (!fwrite($fso, $pagedata)) { //写入字节流,serialize写入其他格式             $this->warns('无法写入缓存文件.'); //trigger_error             return false;         }         flock($fso, lock_un); //释放锁定         fclose($fso);         return true;     }     function display_cache($cachefile) {         if (!file_exists($cachefile)) {             $this->warn('无法读取缓存文件.'); //trigger_error             return false;         }         echo '读取缓存文件:' . $cachefile;         //return unserialize(file_get_contents($cachefile));         $fso = fopen($cachefile, 'r');         $data = fread($fso, filesize($cachefile));         fclose($fso);         return $data;     }     function readdata($cachefile = 'default_cache.txt') {         $cachefile = $this->cache_dir . "/" . $cachefile;         if (file_exists($cachefile) && filemtime($cachefile) > (time() - $this->expiretime)) {             $data = $this->display_cache($cachefile);         } else {             $data = "from here wo can get it from mysql教程 database,update time is " . date('l ds of f y h:i:s a') . ",过期时间是:" . date('l ds of f y h:i:s a', time() + $this->expiretime) . "----------";             $this->cache_page($cachefile, $data);         }         return $data;     } } ?>

下面我打断这个代码逐行解释.

程序透析:这个缓存类(类没什么好怕的.请继续看)名称是cache,有2个属性:

private $cache_dir;

private $expiretime=180;

$cache_dir是缓存文件所放的相对网站目录的父目录,$expiretime(注释一)是我们缓存的数据过期的时间,主要是这个思路:

当数据或者文件被加载的时候,先判断缓存文件存在不,返回false ,文件最后修改时间和缓存的时间和比当前时间大不,大的话说明缓存还没到期,小的话返回false,当返回false的时候,读取原始数据,写入缓存文件中,返回数据.,

接着看程序:

<?php
function __construct($cache_dirname) {
    if (!@is_dir($cache_dirname)) {
        if (!@mkdir($cache_dirname, 0777)) {
            $this->warn(&#39;缓存文件不存在而且不能创建,需要手动创建.&#39;);
            return false;
        }
    }
    $this->cache_dir = $cache_dirname;
}
?>

当类第一次被实例的时候构造默认函数带参数缓存文件名称,如文件不存在,创建一个有编辑权限的文件夹,创建失败的时候抛出异常.然后把cache类的 $cache_dir属性设置为这个文件夹名称,我们的所有缓存文件都是在这个文件夹下面的,代码如下:

<?php
function __destruct() {
    echo &#39;cache class by www.phprm.com.&#39;;
}
?>

这是class类的析构函数,为了演示,我们输出一个字符串表示我们释放cache类资源成功,代码如下:

<?php
function warn($errorstring) {
    echo "<b><font color=&#39;red&#39;>发生错误:<pre class="brush:php;toolbar:false">" . $errorstring . "
"; } ?>

这个方法输出错误信息,代码如下:

<?php
function get_url() {
    if (!isset($_server[&#39;request_uri&#39;])) {
        $url = $_server[&#39;request_uri&#39;];
    } else {
        $url = $_server[&#39;script_name&#39;];
        $url.= (!emptyempty($_server[&#39;query_string&#39;])) ? &#39;?&#39; . $_server[&#39;query_string&#39;] : &#39;&#39;;
    }
    return $url;
}
?>

这个方法返回当前url的信息,这是我看国外很多人的cms系统这样做,主要是缓存x.php?page=1,x.php?page=2,等这种文件的,这里列出是为了扩展的这个cache类功能的,代码如下:

<?php
function cache_page($pageurl, $pagedata) {
    if (!$fso = fopen($pageurl, &#39;w&#39;)) {
        $this->warns(&#39;无法打开缓存文件.&#39;); //trigger_error
        return false;
    }
    if (!flock($fso, lock_ex)) { //lock_nb,排它型锁定
        $this->warns(&#39;无法锁定缓存文件.&#39;); //trigger_error
        return false;
    }
    if (!fwrite($fso, $pagedata)) { //写入字节流,serialize写入其他格式
        $this->warns(&#39;无法写入缓存文件.&#39;); //trigger_error
        return false;
    }
    flock($fso, lock_un); //释放锁定
    fclose($fso);
    return true;
}
?>

cache_page方法分别传入的是缓存的文件名称和数据,这是把数据写到文件里的方法,先用fopen打开文件,然后调用句柄锁定这个文件,然后用fwrite写入文件,最后释放这个句柄,任何一步发生错误将抛出错误,您可能看到 这个注释写入字节流,serialize写入其他格式,顺便一提的是如果我们要把一个数组,(可以从mysql数据库里面select查询除了的结果)用serialize函数写入,用unserialize读取到原来的类型,代码如下:

<?php
function display_cache($cachefile) {
    if (!file_exists($cachefile)) {
        $this->warn(&#39;无法读取缓存文件.&#39;); //trigger_error
        return false;
    }
    echo &#39;读取缓存文件:&#39; . $cachefile;
    //return unserialize(file_get_contents($cachefile));
    $fso = fopen($cachefile, &#39;r&#39;);
    $data = fread($fso, filesize($cachefile));
    fclose($fso);
    return $data;
}
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn