PHP简单的缓存文件类详解
PHP由于它的强大和可伸缩性,近几年来得到长足的发展,PHP相比传统的ASP网站,在速度上有绝对的优势,想mssql转6万条数据PHP如需要40秒,ASP不下2分钟.但是,由于网站的数据越来越多,我们渴求能更快速的调用数据,不必要每次都从数据库掉,我们可以从其他的地方,比方一个文件,或者某个内存地址,这就是PHP的缓存技术,也就是Cache技术。
分析深入
一般来说,缓存的目的是把数据放在一个地方让访问的更快点,毫无疑问,内存是最快的,但是,几百M的数据能往内存放么?这不现实,当然,有的时候临时放如服务器缓存,如ob_start()这个缓存页面开启的话在发送文件头之前页面内容都被缓存在内存中,知道等页面输出自动清楚或者等待 ob_get_contents的返回,或者被ob_end_clean显示的清除,这在静态页面的生成中能很好的利用,在模板中能得到很好的体现。
另外,在ASP中有一对象application,可以保存公用的参数,这也算点缓存,但在PHP,我至今没看到开发者产出这种对象,的确,没必要.ASP.NET的页面缓存技术就用的是viewstate,而cache就是文件关联,(不一定准确),文件被修改,更新缓存,文件没被修改而且不超时(注释1),就读取缓存,返回结果,就是这个思路,看看这个源码:
class cache{
private $cache_dir;
private $expireTime=180;//缓存的时间是 60 秒
function __construct($cache_dirname){
if(!@is_dir($cache_dirname)){
if(!@mkdir($cache_dirname,0777)){
$this->warn(缓存文件不存在而且不能创建,需要手动创建.);
return false;
}
}
$this->cache_dir = $cache_dirname;
}
function __destruct(){
echo Cache class bye.;
}
function get_url() {
if (!isset($_SERVER[REQUEST_URI])) {
$url = $_SERVER[REQUEST_URI];
}else{
$url = $_SERVER[SCRIPT_NAME];
$url .= (!emptyempty($_SERVER[QUERY_STRING])) ? ? . $_SERVER[QUERY_STRING] : ;
}
return $url;
}
function warn($errorstring){
echo "发生错误:".$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;
}

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 English version
Recommended: Win version, supports code prompts!

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.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
