Home > php教程 > php手册 > php中文件缓存实现程序代码

php中文件缓存实现程序代码

WBOY
Release: 2016-05-25 16:39:47
Original
1396 people have browsed it

php文件缓存就是指把缓存生成一个文件,这个文件可以是php,txt等等文件,当我下载访问时就来判断访问上次生成时间,如果超过了我们指定的时间再重新生成一次,否则就直接调用缓存文件,这样就可以减少了对mysql数据库的查询了.

php文件缓存原理

把需要缓存的数据通过serialize函数序列化后直接保存到文件,在需要使用缓存数据的时候,通过反序列化读入文件内容并复制给需要的变量,然后使用,不经常改动的数据可以写入缓存文件.

php文件缓存实例,代码如下:

index.php
<?php
define(&#39;CACHE_ROOT&#39;, &#39;./&#39;);
include_once (&#39;./cache.func.php&#39;);
$file = &#39;qzp&#39;;
$cacheFile = getCacheFileByID($file, &#39;info/&#39;);
print_R(readCache($cacheFile));
$info = array(
    &#39;username&#39; => &#39;qzp&#39;,
    &#39;area_name&#39; => &#39;admin&#39;,
    &#39;mp_name&#39; => &#39;1234&#39;,
    &#39;gh_name&#39; => &#39;5678&#39;
);
writeCache($cacheFile, $info);
cache . func . php文件function arrayeval($array, $level = 0) {
    $space = &#39;&#39;;
    for ($i = 0; $i <= $level; $i++) {
        $space.= "t";
    }
    $evaluate = "Arrayn$space(n";
    $comma = $space;
    foreach ($array as $key => $val) {
        $key = is_string($key) ? &#39;&#39;&#39;.addcslashes($key, &#39;&#39;&#39;) . &#39;&#39;&#39; : $key; 
        $val = !is_array($val) && (!preg_match("/^-?[1-9]d*$/", $val) || strlen($val) > 12) ? &#39;&#39;&#39; . addcslashes($val, &#39;&#39;&#39;).&#39;&#39;&#39; : $val;
        if (is_array($val)) {
            $evaluate.= "$comma$key => " . arrayeval($val, $level + 1);
        } else {
            $evaluate.= "$comma$key => $val";
        }
        $comma = ",n$space";
    }
    $evaluate.= "n$space)";
    return $evaluate;
}
function getCacheFileByID($id, $pre = &#39;info/&#39;, $md5 = true) {
    if ($md5) {
        $md5id = md5($id);
        return CACHE_ROOT . &#39;/&#39; . $pre . substr($md5id, 0, 2) . &#39;/&#39; . substr($md5id, 2, 2) . &#39;/&#39; . $id;
    } else {
        return CACHE_ROOT . &#39;/&#39; . $pre . $id;
    }
}
function readCache($filename, $time = 0) {
    if ($datas = @file_get_contents($filename)) {
        $datas = unserialize($datas);
        if ($time < 1 || (time() - $datas[&#39;time&#39;] < $time)) {
            return $datas[&#39;data&#39;];
        }
    }
    return false;
}
function writeCache($filename, $data) {
    makeDir(dirname($filename));
    if ($handle = fopen($filename, &#39;w+&#39;)) {
        $datas = array(
            &#39;data&#39; => $data,
            &#39;time&#39; => time()
        );
        flock($handle, LOCK_EX);
        $rs = fputs($handle, serialize($datas));
        flock($handle, LOCK_UN);
        fclose($handle);
        if ($rs !== false) {
            return true;
        }
    }
    return false;
}
function makeDir($path) {
    if (!file_exists($path)) {
        makeDir(dirname($path));
        if (!mkdir($path, 0777)) die(&#39;无法创建文件夹&#39; . $path);
    }
}
?>
Copy after login

把要缓存的文件序列化下存放起来,当使用的时候反序列化回来使用,使用对PHP模板数据进行缓存的工具smarty,smarty将HTML文件缓存成一个静态的HTML页,需要缓存的模板文件可以使用smarty.

例,代码如下:

<?php
//包含Smarty类库
require (&#39;libs/Smarty.class.php&#39;);
//创建Smarty类的对象
$smarty = new Smarty;
//启用缓存
$smarty->caching = true;
//指定缓存文件保存的目录
$smarty->cache_dir = "./cache/";
//也会把输出保存
$smarty->display(&#39;index.tpl&#39;)
Copy after login


教程地址:

欢迎转载!但请带上文章地址^^

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template