php文件缓存就是指把缓存生成一个文件,这个文件可以是php,txt等等文件,当我下载访问时就来判断访问上次生成时间,如果超过了我们指定的时间再重新生成一次,否则就直接调用缓存文件,这样就可以减少了对mysql数据库的查询了.
php文件缓存原理
把需要缓存的数据通过serialize函数序列化后直接保存到文件,在需要使用缓存数据的时候,通过反序列化读入文件内容并复制给需要的变量,然后使用,不经常改动的数据可以写入缓存文件.
php文件缓存实例,代码如下:
index.php <?php define('CACHE_ROOT', './'); include_once ('./cache.func.php'); $file = 'qzp'; $cacheFile = getCacheFileByID($file, 'info/'); print_R(readCache($cacheFile)); $info = array( 'username' => 'qzp', 'area_name' => 'admin', 'mp_name' => '1234', 'gh_name' => '5678' ); writeCache($cacheFile, $info); cache . func . php文件function arrayeval($array, $level = 0) { $space = ''; for ($i = 0; $i <= $level; $i++) { $space.= "t"; } $evaluate = "Arrayn$space(n"; $comma = $space; foreach ($array as $key => $val) { $key = is_string($key) ? '''.addcslashes($key, ''') . ''' : $key; $val = !is_array($val) && (!preg_match("/^-?[1-9]d*$/", $val) || strlen($val) > 12) ? ''' . addcslashes($val, ''').''' : $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 = 'info/', $md5 = true) { if ($md5) { $md5id = md5($id); return CACHE_ROOT . '/' . $pre . substr($md5id, 0, 2) . '/' . substr($md5id, 2, 2) . '/' . $id; } else { return CACHE_ROOT . '/' . $pre . $id; } } function readCache($filename, $time = 0) { if ($datas = @file_get_contents($filename)) { $datas = unserialize($datas); if ($time < 1 || (time() - $datas['time'] < $time)) { return $datas['data']; } } return false; } function writeCache($filename, $data) { makeDir(dirname($filename)); if ($handle = fopen($filename, 'w+')) { $datas = array( 'data' => $data, 'time' => 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('无法创建文件夹' . $path); } } ?>
把要缓存的文件序列化下存放起来,当使用的时候反序列化回来使用,使用对PHP模板数据进行缓存的工具smarty,smarty将HTML文件缓存成一个静态的HTML页,需要缓存的模板文件可以使用smarty.
例,代码如下:
<?php //包含Smarty类库 require ('libs/Smarty.class.php'); //创建Smarty类的对象 $smarty = new Smarty; //启用缓存 $smarty->caching = true; //指定缓存文件保存的目录 $smarty->cache_dir = "./cache/"; //也会把输出保存 $smarty->display('index.tpl')
教程地址:
欢迎转载!但请带上文章地址^^

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
