PHP缓冲ob_start和页面文件缓存

原创
2016-06-08 17:24:31 773浏览

在php中缓存分为很多种,像ob_start ob_get_flush 这些函数是缓存技术是减轻服务器压力的,但他不能真正的减少服务器负载,我们可以利用页面缓存或文件缓存来真正的实现缓存技术哦。

ob_start ob_get_flush 这些函数是缓存技术的一种,是减轻服务器压力的,直到项目开发用到才知道混淆了和缓存的概念,
这些像ob_start ob_get_flush这些函数都是为了在编程中字符串输出到客户端上去为了延长时间而用到的技术,延迟输出(字符串先发送到缓冲区需要时在输出到浏览器),是一种输出技巧。最常见的应用是静态化技术(可以实现静态缓存):


把要输出代码的先保存到缓存区在用ob_get_contents();取得内容写入文件

php ob_start 与 ob_end_flush() 是 php 的缓冲输出函数。

ob_start([string output_callback])- 打开输出缓冲区,所有的输出信息不在直接发送到浏览器,而是保存在输出缓冲区里面,可选得回调函数用于处理输出结果信息。

ob_end_flush - 结束(发送)输出缓冲区的内容,关闭输出缓冲区。

php 输出东西,会保存在一个 php 维护的内存里,称为 buffer 也行,缓存也行,都是一个意思。然后当这个 buffer 满了,php 会自动往 web server 发送这些数据。

也就是说每次 echo,并不一定会输出东西,而是保存在 buffer 里。

ob_start() 的意思,可以理解为(但是实际上和我下面的说法有区别),这个 buffer 由 ob_ 系列函数来来控制,也就是,PHP 不会维护自己的 buffer,不会自动把buffer 的内容自动发送到 web server,直到你 ob_end() 或者类似的 ob 操作。

ob_函数一般用来捕获当前的输出,跟效率是没什么关系的。至于为什么捕获输出,原因很多,例如我捕捉输出,缓存到一个文件里,下次请求就可以直接读这个 cache 文件的内容作为输出了。

代码如下 复制代码

ob_start();

2 内容

3 echo ob_get_contents() ;

例子

代码如下 复制代码
//打开缓冲区
ob_start();
?>
php页面的全部输出
//取得php页面输出的全部内容
$content = ob_get_contents();
//创建一个文件,并打开,准备写入
$fp = fopen(“1.html”, “w”);
//把php页面的内容全部写1.html
fwrite($fp, $content);
fclose($fp);
?>

而缓存是一个很大的概念,一般用来解决大型网站高负载问题,像下面雨中漫步讲的 就是一种缓存技术的实现,当然除了内存缓存还有文件、页面缓存,比如想uchome 中系统配置变量,一般不需要改变的设置生成文件是文件缓存,用到这些数据直接通过读取文件读取而不直接从数据库读取,这些都是为了避免再次查询数据减轻访问压力问题而设置的。

网上关于 PHP 缓存类的资料很多,不过这个类应该是我见过功能满足需求,但又无比简洁的一个。废话不多说,直接看代码吧!

使用说明:

1、实例化

代码如下 复制代码

$cache = new Cache();

2、设置缓存时间和缓存目录

$cache = new Cache(60, '/any_other_path/');第一个参数是缓存秒数,第二个参数是缓存路径,根据需要配置。
默认情况下,缓存时间是 3600 秒,缓存目录是 cache/

3、读取缓存

代码如下 复制代码

$value = $cache->get('data_key');4、写入缓存

$value = $cache->put('data_key', 'data_value');完整实例:

$cache = new Cache();

//从缓存从读取键值 $key 的数据
$values = $cache->get($key);

//如果没有缓存数据
if ($values == false) {
//insert code here...
//写入键值 $key 的数据
$cache->put($key, $values);
} else {
//insert code here...
}
Cache.class.php

class Cache {
private $cache_path;//path for the cache
private $cache_expire;//seconds that the cache expires

//cache constructor, optional expiring time and cache path
public function Cache($exp_time=3600,$path="cache/"){
$this->cache_expire=$exp_time;
$this->cache_path=$path;
}

//returns the filename for the cache
private function fileName($key){
return $this->cache_path.md5($key);
}

//creates new cache files with the given data, $key== name of the cache, data the info/values to store
public function put($key, $data){
$values = serialize($data);
$filename = $this->fileName($key);
$file = fopen($filename, 'w');
if ($file){//able to create the file
fwrite($file, $values);
fclose($file);
}
else return false;
}

//returns cache for the given key
public function get($key){
$filename = $this->fileName($key);
if (!file_exists($filename) || !is_readable($filename)){//can't read the cache
return false;
}
if ( time() cache_expire) ) {//cache for the key not expired
$file = fopen($filename, "r");// read data file
if ($file){//able to open the file
$data = fread($file, filesize($filename));
fclose($file);
return unserialize($data);//return the values
}
else return false;
}
else return false;//was expired you need to create new
}
}
?>

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。